ysheokorea / passportJS-project

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passport 사용법

Refercence
https://www.passportjs.org/



OAuth 2.0 동작원리

  1. 메인 페이지(client)는 인증제공자에게 Authorization Code 요청
  2. Auth Code를 서버로 전송
  3. 서버는 인증제공자로 부터 Access Token 발급
  4. client에게 access token 제공
  5. client는 access token으로 인증 성공



프로젝트 목차

  1. passport-google
  2. passport-kakao
  3. passport-github
  4. passport-local



사용법

  1. passport 모듈 설치
npm install passport passport-google passport-local passport-kakao
  1. CLIENT_ID / CLIENT_SECRET 발급

  2. REACT 설정



코드 설명

passport/index.js

passport.use(
  new GoogleStrategy(
    {
      clientID: process.env.GOOGLE_CLIENT_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
      callbackURL: "/auth/google/callback",
    },
    function (accessToken, refreshToken, profile, done) {
      done(null, profile);
    }
  )
);

routes/auth.js

router.get("/google", passport.authenticate("google", { scope: ["profile"] }));

router.get(
  "/google/callback",
  passport.authenticate("google", {
    successRedirect: CLIENT_URL,
    failureRedirect: "/login/failed",
  })
);

About


Languages

Language:JavaScript 88.9%Language:HTML 7.2%Language:CSS 3.9%