index.js 1.92 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;

//구글 로그인
passport.use(new GoogleStrategy({
  clientID: 'ClientID',
  clientSecret:'ClientSecret',
  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) { 
  googleLogin = true;
  req.session.googleToken = req.user.accessToken;
  if(!req.session.kakaoToken) return res.redirect('/');
  else return res.redirect('/calendar');
});

//카카오 로그인
const kakaoKey = {
  clientID: "Kakao Rest Api ID",
  callbackURL: "/oauth/callback"
};

passport.use(
  "kakao-login",
  new KakaoStrategy(kakaoKey, (accessToken, refreshToken, profile, done) => {
    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) {
    kakaoLogin = true;  
    req.session.kakaoToken = req.user.accessToken;
      if(!req.session.googleToken) return res.redirect('/');
      else return res.redirect('/calendar');
  });


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

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


module.exports = router;