Naver login

2021. 6. 24. 13:27Node.js

네이버 개발자 센터에서 앱등록을 먼저 해준다.

https://developers.naver.com/apps/#/register?api=nvlogin

 

npm i passport

npm i passport-naver-v2

const express = require('express');
const app = express();
const passport = require('passport');
const NaverStrategy = require('passport-naver-v2').Strategy

passport.use(
    new NaverStrategy(
        {
            clientID: 'L1z90s4KG0wApBXPBOUg',
            clientSecret: '7ohkg7VPWA',
            callbackURL: `http://localhost:3030/api/naver/login/callback`, // 애플리케이션을 등록할 때 입력했던 callbackURL 을 입력해준다.
        },
        (accessToken, refreshToken, profile, done) => {
            // 이곳에서 사용자 계정 관련된 작업을 한다.

            done(null, profile._json);
            console.log(profile);
        }
    )
)

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

app.use(passport.initialize());

app.get('/', (req, res) => {
    res.send('Hello World');
});


app.get('/api/naver/login', passport.authenticate('naver'), (req, res) => {
    res.send(req.user);
});


app.listen(3030, () => {
    console.log('server is running at port 3030..');
});


app.get('/api/naver/login/callback', passport.authenticate('naver'), (req, res) => {
    res.send('result :' + JSON.stringify({ state: req.query.state, user: req.user }))
});

https://github.com/parkoon/passport-naver-v2/blob/master/sample/index.js

 

parkoon/passport-naver-v2

Naver oauth2 passport login. Contribute to parkoon/passport-naver-v2 development by creating an account on GitHub.

github.com

 

 

localhost:3030/naver/login
callback으로 유저 정보가 들어온다.

'Node.js' 카테고리의 다른 글

0708 구글 애널리틱스  (0) 2021.07.08
0630 firebase + google analytics  (0) 2021.06.30
GPGS Achievement  (0) 2021.06.23
0617 GPGS  (0) 2021.06.17
0616 Sequelize  (0) 2021.06.16