login.js 4.28 KB
var express = require('express');
var fs = require('fs');
var router = express.Router();
var mysql      = require('mysql');
const passport = require('passport');
var googlestrategy = require('passport-google-oauth20').Strategy,
KakaoStrategy = require('passport-kakao').Strategy;
var naverstrategy = require('passport-naver').Strategy;

var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'soap',
    password : '111111',
    port     : 3306,
    database : 'ytmt'
});

passport.serializeUser(function(user, done) {
    done(null, user);
})

passport.deserializeUser(function(obj, done) {
    done(null, obj);
})
//var googleCredit = require('/config/google.json');

passport.use(new KakaoStrategy({
    clientID : 'bd2e610396fb7bbb84cf91a786b3cc72',
    callbackURL :'/auth/login/kakao/callback',
   clientSecret : 'eUtJGtlLoCZJufevp3LKfDP0KOtZUV7R'
},
function(accessToken, refreshToken,params, profile, done){
    // 사용자 정보는 profile에
    loginByThirdparty(accessToken, refreshToken, profile);

    console.log("(!)로그인 : " + profile._json.id+"("+profile._json.properties.nickname +")");
    //return done(null,profile)
    return done(null, {
        'user_id': profile._json.id,
        'nickname': profile._json.properties.nickname
    });
}
));

function loginByThirdparty(accessToken, refreshToken, profile) {
    //예전 코드는 MySQL 버젼이 맞지 않음
  //  var sql = 'INSERT INTO `user`(id) VALUES(?) ON DUPLICATE KEY(PRIMARY) UPDATE id=(?);'
    var sql = "INSERT INTO `user` (id) VALUES (?) ON DUPLICATE KEY UPDATE id=id";
    var kid=[profile._json.id];
    connection.query(sql,kid,function(err,result){
        if (err) {
            console.log("로그인 쿼리중 에러 : " + err);
        } else {
            console.log("로그인 DB처리 완료!");
        }
    });
}

router.get('/auth/login/kakao',
   // passport.authenticate('kakao',{state: "myStateValue"})
   passport.authenticate('kakao')
);

router.get('/auth/login/kakao/callback',
    passport.authenticate('kakao', {
        //session: false,
        successRedirect: '/adduser',
        failureRedirect: '/'
    })
);

router.get('/auth/logout/kakao',function (req,res) {
    req.logout();
    res.redirect('/');
})

passport.use(new naverstrategy({
    clientID : "TfrHnvzNcOTnzT6v6glC",
    clientSecret : "YI1CmmOqTy",
    callbackURL : "/auth/login/naver/callback"
}, function(accessToken, refreshToken, profile, done) {
    // console.log("Hello world");
    // 사용자 정보는 profile에
    loginByThirdparty(accessToken, refreshToken, profile);

    console.log("(!)로그인 : " + profile._json.id);
    //return done(null,profile)
    return done(null, {
        'user_id': profile._json.id,
        //'nickname': profile._json.properties.nickname
    });
}))

router.get('/auth/login/naver',
    passport.authenticate('naver',{
        scope: ['profile']
    })
)

router.get('/auth/login/naver/callback', passport.authenticate('naver', {
    failureRedirect: '/',
    successRedirect: '/adduser'
})
)

passport.use(new googlestrategy( {
    clientID : "214039775018-qhv7ha12dd410qfstc4n3nlhbqmblhd2.apps.googleusercontent.com",
    clientSecret: "2DINnGZ--TaTG3bhZfDkAhN0",
    callbackURL : "http://localhost:3000/auth/login/google/callback"
}, function (accessToken, refreshToken, profile, done) {
    //console.log("Hello world");
    // User.findOrCreate({
    //     googleId: profile.id
    // }, function (err, user) {
    //     return done(err, user);
    // });
    loginByThirdparty(accessToken, refreshToken, profile);

    console.log("(!)로그인 : " + profile._json.id);
    //return done(null,profile)
    return done(null, {
        'user_id': profile._json.id,
        //'nickname': profile._json.properties.nickname
    });
}
));

router.get('/auth/login/google', 
    passport.authenticate('google',{
        scope: ['https://www.googleapis.com/auth/plus.login']
    })
);

router.get('/auth/login/google/callback', passport.authenticate('google', {
    failureRedirect: '/',
    successRedirect: '/adduser'
}));

router.get('/', function(req, res) {
    fs.readFile('./views/login.ejs', function(err, data) {
        if(err) {console.log(err);}
        else {
            res.writeHead(200, {'Content-Type' : 'text/html'});
            res.end(data);
        } 
    });
});


module.exports = router;