Skip to content
Snippets Groups Projects
authController.js 1.66 KiB
Newer Older
  • Learn to ignore specific revisions
  • import express from 'express';
    import moment from 'moment';
    
    Gwangbin's avatar
    Gwangbin committed
    import userService from '../user/userService.js';
    
    
    
    export const router = express.Router();
    
    const sessionTime = 60; // 세션시간(임시)
    
    router.post('/login', async (req, res) => {
    
    Gwangbin's avatar
    Gwangbin committed
      /*
      TODO: 토큰의 무결성 체크
      토큰이 이상이 없다면, 로그인/회원가입 로직을 수행 후 jwt 쿠키를 보낸다.
      */
      const expires = moment().add(sessionTime.toString(), 'm').toDate();
    
    Gwangbin's avatar
    Gwangbin committed
      // 정보가 없다면 회원 가입 (강제?)
    
      const user = await userService.existsByEmail(req.body.email);
    
    Gwangbin's avatar
    Gwangbin committed
      if (!user) { // 유저가 없다면 회원 가입 후 세션 생성
        let userProfilePicture = req.body.picture || null;
    
        await userService.createUser({
          nickname: req.body.name,
          email: req.body.email,
          google: {
            id: req.body.sub,
            profileUrl: userProfilePicture,
          },
        });
    
    Gwangbin's avatar
    Gwangbin committed
        console.log('new user saved!')
      }
      console.log('login done')
    
      req.session.sessionid = req.body; //프론트에서 건네받은 JWT로 세션 생성
      res.cookie('name', JSON.stringify({ name: req.body.name, email: req.body.email, id: req.body.sub }), { expires }); //사용자 이름 쿠키
    
    Gwangbin's avatar
    Gwangbin committed
      res.send(req.body.name); // 이름 보내서 뭐하게?
    
    
    });
    
    router.get("/logout", (req, res) => {
    
    Gwangbin's avatar
    Gwangbin committed
      res.clearCookie('name');
      if (req.session.sessionid) {
        req.session.destroy((err) => {
          if (err) {
            console.log(err)
            return;
          }
        });
        res.send(req.body.name);
      } else {
        res.send(req.body.name);
      }
    
    Gwangbin's avatar
    Gwangbin committed
    router.get("/session", (req, res) => {
    
    Gwangbin's avatar
    Gwangbin committed
      if (req.session.sessionid) {
        res.send(true);
      }
    
    Gwangbin's avatar
    Gwangbin committed
        res.send(false);
    
      }
    });
    
    export default router;