login.js
4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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;