Let $f: X \rightarrow Y$ and $g: Y \rightarrow X$
If $f \circ g = id_y$, is $f$ injective?
$f \circ g = id_y$
$\equiv g^{-1} \circ g = id_y \Rightarrow $g is leftinverse and by definition injective
$f \circ g = id_y$
$\equiv f \circ f^{-1}=id_y \Rightarrow$ f is rightinverse and by definition surjective
So $f$ is surjective and not injective
Question: Is that or do I miss something here?
Let $X=\{1,2\}$ and $Y=\{0\}$. Let $g:Y\rightarrow X$, $g(0)=1$ and $f:X\rightarrow Y$, $f(x)=0$ for $x=1,2$. Note that $f$ is not injective. Now we have
$$f\circ g(0)=0$$
and since $0$ is the only element of $Y$ we have $f\circ g= id_Y$.
This is an example of two functions $f$ and $g$ where $f$ is not injective but nontheless $f\circ g=id_Y$.
Your proof was not correct. You proved that $f$ had to be surjective (which is correct), but that was not the question The question was "does it have to be injective?" The answer is no and the easiest way to show that is to just give a counterexample.