Prove that if $\sum c_n e^{inx}$ converges in $L^2$ to $f$ then $c_n$ are the Fourier coefficients. The full problem is this:

Let $f:[-\pi,\pi]\rightarrow\mathbb{C}$ be Riemann integrable. Let $\{c_n\}_{n\in\mathbb{Z}}\subset\mathbb{C}$. Prove that if $s_N=\sum_{n=-N}^Nc_ne^{inx}$ converges in $L^2$ to $f(x)$ then $c_n=\frac{1}{2\pi}\int_{-\pi}^\pi f(x)e^{-inx}dx$.

I have no idea where to start on this. Any hint at all would help immensely!
Thank you!
 A: Write an error function as follows:
$$\begin{align}E(c) &= \int_{-\pi}^{\pi} dx \: \left |f(x) - \sum_{n=-N}^N c_n e^{i n x} \right |^2\\ &=  \int_{-\pi}^{\pi} dx \: \left (f(x) - \sum_{n=-N}^N c_n e^{i n x} \right ) \left(f^*(x) - \sum_{n=-N}^N c_n^* e^{-i n x} \right )\\ &= \int_{-\pi}^{\pi} dx \: \left (|f(x)|^2 - f(x)\sum_{n=-N}^N c_n^* e^{-i n x} - f^*(x) \sum_{n=-N}^N c_n e^{i n x} + \sum_{n=-N}^N\: \sum_{n'=-N}^N c_n c_n^* e^{i (n-n') x} \right )\end{align}$$
where $c$ is the vector of coefficients $[c_n]_{n=-N}^N$.  Note that, when the sum converges to $f$ in an $L^2$ sense, then 
$$\lim_{N \rightarrow \infty} E(c) = 0$$
To find a critical coefficient $c_k$ that minimizes $E$, we solve
$$\frac{\partial E}{\partial c_k^*} = 0$$
Doing this, we obtain
$$\sum_{n=-N}^N c_n \int_{-\pi}^{\pi} dx \: e^{i (n-k) x} = \int_{-\pi}^{\pi} dx \: f(x) e^{-i k x}$$
Using the sifting property of the integral on the LHS, we deduce that
$$c_k \cdot 2 \pi = \int_{-\pi}^{\pi} dx \: f(x) e^{-i k x}$$
which is a Fourier coefficient.
A: $L^2([-\pi,\pi])$ is a Hilbert space for the hermitian product $$\langle f,g \rangle = \frac{1}{2\pi}\int_{-\pi}^\pi \overline{f(x)}g(x)\,dx$$
Furthermore, the family of functions $e_n\colon x \mapsto e^{inx}$, $n \in \Bbb Z$ is orthonormal with respect to $\langle,\rangle$. (In fact, it is a Hilbert basis.) This yields
$$
c_n = \frac{1}{2\pi}\int_{-\pi}^\pi s_N(x)e^{-inx}\,dx = \langle e_n,s_N\rangle \xrightarrow[N\to\infty]{} \langle e_n,f\rangle
$$
Indeed,
$$
|\langle e_n,f\rangle - \langle e_n,s_N\rangle| = |\langle e_n,f-s_N\rangle| \leq \|e_n\|_{L^2}\|f-s_N\|_{L^2}.
$$
