진행중인 다이어리&스케줄러 프로젝트에서 자동 로그인 구현 과정을 기록해보았다.
프론트 : 로그인 시 localStroge에 저장해둔 id(token)를 parameter로 보냄
const token = localStorage.getItem("TOKEN");
if (token) {
const response = await axios.get(`http://localhost:4000/login/${token}`);
setData(response.data.user.data);
setIsLogin(true);
}
백엔드 : 파라미터로 받은 id를 통해 mongoDB에서 user를 찾아서 프론트로 보내줌
export const getLogin = async (req, res) => {
const { id } = req.params;
const user = await User.findById(id);
res.send({ user });
};