반응형
Google Cloud Platform
하나의 계정으로 모든 Google 서비스를 Google Cloud Platform을 사용하려면 로그인하세요.
accounts.google.com
여기서 사용자 인증 정보를 만들고
클라이언트 ID, Secret을 발급받는다.
리디렉션 URL 설정
구글 Stragety를 설정.
config/passport/google.js
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var config = require('../config');
module.exports = function(app, passport) {
return new GoogleStrategy({
clientID: config.google.clientID,
clientSecret: config.google.clientSecret,
callbackURL: config.google.callbackURL
}, function(accessToken, refreshToken, profile, done) {
console.log('passport의 google 호출됨.');
console.dir(profile);
console.log('///////////////////');
var options = {
criteria: { 'google.id': profile.id }
};
var database = app.get('database');
database.userModel.findOne(options, function (err, user) {
if (err) {console.log(err.stack); return done(err);}
if (!user) {
var user = new database.userModel({
name: profile.displayName,
// email: profile.emails[0].value,
id : profile.id,
provider: 'google',
google: profile._json
});
user.save(function (err) {
if (err) console.log(err);
return done(err, user);
});
} else {
return done(err, user);
}
});
});
};
config/config.js
module.exports = {
...
google: {
clientID: 'your ID',
clientSecret: 'your Secret',
callbackURL: '/auth/google/callback'
}
}
라우팅 설정
routes/user_passport.js
//구글 패스포트
router.route('/auth/google').get(passport.authenticate('google',{
scope : 'profile'
}));
router.route('/auth/google/callback').get(passport.authenticate('google',{
successRedirect : '/profile',
failureRedirect : '/'
}));
반응형
'IT > web' 카테고리의 다른 글
Node.js] 게시판 만들기2 // 글 리스트 보기, 조회수 조회 (0) | 2020.02.12 |
---|---|
Node.js] 게시판 만들기1 // 글 작성, 작성된 글 보기 (0) | 2020.02.11 |
Node.js] 플래쉬 메시지 (0) | 2020.02.08 |
Node.js] 라우팅 개념 다시 한번 (0) | 2020.02.08 |
Node.js] 사용자 인증 // 패스포트 모듈 (0) | 2020.02.07 |
댓글