index.js 2.31 KB
var express = require('express');
var router = express.Router();
const KakaoStrategy = require("passport-kakao").Strategy;
var passport = require('passport');
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var googleLogin = false;
var kakaoLogin =false;
passport.use(new GoogleStrategy({
  clientID:'978869138601-u3euf0c04sbdor68r30m599gilvjn91e.apps.googleusercontent.com',
  clientSecret:'9ERYhDZJK-CrX3moES2z11ol',
  callbackURL:'http://localhost:3000/auth/google/callback',
  scope:['openid','email','https://www.googleapis.com/auth/calendar.readonly',
  'https://www.googleapis.com/auth/calendar',
  'https://www.googleapis.com/auth/calendar.events']
},
function(accessToken,refreshToken,params,profile,done){
  profile.accessToken = accessToken;
  return done(null,profile);
}));

router.get('/auth/google',passport.authenticate('google',{ session:false}));
router.get('/auth/google/callback',passport.authenticate('google',{
  failureRedirect:'/'
}),function(req, res) { 
  req.session.googleToken = req.user.accessToken;
  googleLogin = true;
  if(!req.session.kakaoToken) return res.redirect('/');
  else return res.redirect('/calendar');
});

const kakaoKey = {
  clientID: "cf6ae2286a1b022bafa454588f32878b",
  callbackURL: "/oauth/callback"
};

passport.use(
  "kakao-login",
  new KakaoStrategy(kakaoKey, (accessToken, refreshToken, profile, done) => {
    console.log(profile);
    profile.accessToken = accessToken;
    return done(null,profile);
  })
);
router.get("/kakao", passport.authenticate("kakao-login"));
router.get(
  "/oauth/callback",
  passport.authenticate("kakao-login",{failureRedirect:'/'}),function(req, res) {
      req.session.kakaoToken = req.user.accessToken;
      kakaoLogin = true;
      if(!req.session.googleToken) return res.redirect('/');
      else return res.redirect('/calendar');
  });


router.get('/',function(req,res,next){
  res.render('index',{title:'Reminder-Talk',google:googleLogin,kakao:kakaoLogin});
});

router.get('/logout', function(req, res) { 
  req.logout(); 
  console.log(req.session);
  req.session = null; 
  res.redirect('/'); 
});


module.exports = router;

//db에 id 별 이벤트 저장하고 calendar page 에서 출력해주자!
//문제점 : 비동기식 진행이어서 getAllcalendarID 함수 실행 후 바로 list 출력 안됨. console.log 먼저 출력됨