Condition for sustained energy transfer from electron to light wave in an undulator: The light wave has to slip forward by per half-period of the electron trajectory.
An electron beam moving on a straight line cannot transfer energy to a light wave. The reason why is the transverse polarization of electromagnetic waves: The electric field is perpendicular to the direction of motion, and the force between electron and light wave is orthogonal to the electron velocity, which implies that no work is done on the electron. In order to facilitate energy exchange, the electrons must be given a velocity component in the transverse direction. This is what happens in the undulator.
The transverse component of the electron velocity and the electric vector of the light wave must point in the same direction to get an energy transfer from the electron to the light wave. Now a problem arises. The light wave, traveling with the speed of light, will obviously slip forward with respect to the electrons; firstly, because the electrons are massive particles
and thus slower than light, and secondly, because they travel on a slalom orbit which is longer than the straight path of the photons. The question is then: How is it possible at all to achieve a steady energy transfer from the electron beam to the light wave along the entire undulator? The answer is that the light wave has to slip by the right amount, and this proper slippage is only possible for a certain wavelength. The transverse velocity and the electromagnetic field of the light wave remain parallel if the light wave slips by half an optical wavelength in a half-period of the electron trajectory.
This condition allows us to compute the proper light wavelength (see “The physics of free-electron lasers”):

The calculation shows that the proper light wavelength is identical with the wavelength of undulator radiation in the forward direction. This equality is the physical basis of the self-amplified spontaneous emission mechanism: Spontaneous undulator radiation can serve as seed radiation for a high-gain FEL. The formula displays one of the great advantages
of the FEL; in contrast to conventional lasers the wavelength of an FEL can be varied at will, simply by changing the electron energy.
Now the second problem arises: The electron bunch is far longer than the light wavelength. Generally the electrons will be distributed uniformly along the bunch axis, and although there are many electrons fulfilling the condition that their transverse velocity is almost parallel to the pulsating electromagnetic field and which thus transfer energy to the light wave, there will be equally many anti-parallel electrons and they will withdraw energy from the light wave. How can one then achieve an overall amplification of the light wave? The net energy exchange
between electron bunch and light wave is in fact zero if the electron energy is equal to the resonance energy, where the electrons emit undulator radiation of exactly the incident wavelength. However, there will be light amplification if the electron energy is above the resonance energy, but light attenuation if the energy is below that value.