A microscopic theory of the electrical conductivity $sigma(omega)$ within the t-J model is developed. An exact representation for $sigma(omega)$ is obtained using the memory-function technique for the relaxation function in terms of the Hubbard operators, and the generalized Drude law is derived. The relaxation rate due to the decay of charge excitations into particle-hole pairs assisted by antiferromagnetic spin fluctuations is calculated in the mode-coupling approximation. Using results for the spectral function of spin excitations calculated previously, the relaxation rate and the optical and dc conductivities are calculated in a broad region of doping and temperatures. The reasonable agreement of the theory with experimental data for cuprates proves the important role of spin-fluctuation scattering in the charge dynamics.