기본 콘텐츠로 건너뛰기

node.js 구글 계정 연동




이제 node.js에 코드를 추가를 해보자
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
var everyauth = require('everyauth');
var check_auth = function (req, res, next) {
    if (!req.loggedIn) {
       res.redirect('/auth/google'); 
    }
    console.log('asd');
    next();
};
everyauth.everymodule.findUserById(function (id, callback) {
    callback(null, usersById[id]);
});
everyauth.google
    .appId(' App ID'// 위 사진처럼 개발자 콘솔에 나타난 Client ID를 입력
    .appSecret('App SECRET'// client 보안비밀 입력
    .scope('https://www.googleapis.com/auth/userinfo.profile')
    .handleAuthCallbackError(function (req, res) {
    
})
    .findOrCreateUser(function (session, accessToken, extra, googleUser) {
    googleUser.refreshToken = extra.refresh_token;
    googleUser.expiresIn = extra.expires_in;
    
    return usersByGoogleId[googleUser.id] || (usersByGoogleId[googleUser.id] = addUser('google', googleUser));
    })
  
    .redirectPath('/');
cs
1
2
3
var stylus = require('stylus');
app.use(stylus.middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
cs

댓글