๊ด€๋ฆฌ ๋ฉ”๋‰ด

<Hello Hosung๐Ÿ˜Ž/>

[Node.js] OAuth2 ๋กœ๊ทธ์ธ - 2(Google Login) ๋ณธ๋ฌธ

๐Ÿ“ฑ JavaScript/ใ…คNode

[Node.js] OAuth2 ๋กœ๊ทธ์ธ - 2(Google Login)

์ขŒ์ถฉ์šฐ๋Œ ๋ฐฑ์—”๋“œ ๊ฐœ๋ฐœ์ž ์ผ๊ธฐ๐Ÿง 2024. 11. 19. 09:02

 

 

๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด, OAuth 2.0 ํ”„๋กœํ† ์ฝœ์„ ์‚ฌ์šฉํ•˜์—ฌ Google API์™€ ์—ฐ๋™ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜๋Š” Node.js + Express ๊ธฐ๋ฐ˜์˜ ๊ฐ„๋‹จํ•œ Google ๋กœ๊ทธ์ธ ๊ตฌํ˜„ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.


์‚ฌ์ „ ์ค€๋น„

  1. Google Cloud Console์—์„œ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
    • Google Cloud Console์— ์ ‘์†.
    • ์ƒˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ๊ธฐ์กด ํ”„๋กœ์ ํŠธ๋ฅผ ์„ ํƒ.
    • OAuth ๋™์˜ ํ™”๋ฉด ์„ค์ •:
      • ์‚ฌ์šฉ์ž ์œ ํ˜• ์„ ํƒ(์™ธ๋ถ€/๋‚ด๋ถ€).
      • ํ•„์ˆ˜ ์ •๋ณด ์ž…๋ ฅ(์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„, ์ด๋ฉ”์ผ ๋“ฑ).
    • OAuth 2.0 ํด๋ผ์ด์–ธํŠธ ID ์ƒ์„ฑ:
      • ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์œ ํ˜•: ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ ํƒ.
      • ์Šน์ธ๋œ ๋ฆฌ๋””๋ ‰์…˜ URI ์ถ”๊ฐ€: ์˜ˆ) http://localhost:3000/auth/google/callback
    • ํด๋ผ์ด์–ธํŠธ ID์™€ ํด๋ผ์ด์–ธํŠธ ๋น„๋ฐ€ ํ‚ค(Client Secret)๋ฅผ ๋ณต์‚ฌ.
  2. ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜
npm install express passport passport-google-oauth20 express-session

์ฝ”๋“œ ์ž‘์„ฑ

1. ํ”„๋กœ์ ํŠธ ๊ตฌ์กฐ

project/
โ”‚
โ”œโ”€โ”€ app.js            # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์—”ํŠธ๋ฆฌ ํŒŒ์ผ
โ”œโ”€โ”€ package.json      # Node.js ์ข…์†์„ฑ
โ””โ”€โ”€ config.js         # Google OAuth ํด๋ผ์ด์–ธํŠธ ์„ค์ • ํŒŒ์ผ

 

2. config.js

Google OAuth ํด๋ผ์ด์–ธํŠธ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

module.exports = {
  google: {
    clientID: "YOUR_CLIENT_ID", // Google Cloud Console์—์„œ ๋ฐœ๊ธ‰๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ ID
    clientSecret: "YOUR_CLIENT_SECRET", // Google Cloud Console์—์„œ ๋ฐœ๊ธ‰๋ฐ›์€ ํด๋ผ์ด์–ธํŠธ ๋น„๋ฐ€ ํ‚ค
    callbackURL: "http://localhost:3000/auth/google/callback" // ๋ฆฌ๋””๋ ‰์…˜ URI
  }
};

 

3. app.js

Express์™€ Passport.js๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Google ๋กœ๊ทธ์ธ ์ฒ˜๋ฆฌ.

const express = require("express");
const passport = require("passport");
const session = require("express-session");
const { Strategy: GoogleStrategy } = require("passport-google-oauth20");
const { google } = require("./config");

const app = express();

// Session ์„ค์ •
app.use(session({ secret: "secret", resave: false, saveUninitialized: true }));

// Passport ์ดˆ๊ธฐํ™”
app.use(passport.initialize());
app.use(passport.session());

// Google OAuth2 Strategy ์„ค์ •
passport.use(
  new GoogleStrategy(
    {
      clientID: google.clientID,
      clientSecret: google.clientSecret,
      callbackURL: google.callbackURL
    },
    (accessToken, refreshToken, profile, done) => {
      // ๋กœ๊ทธ์ธ ์„ฑ๊ณต ์‹œ ์ฝœ๋ฐฑ
      console.log("Google profile:", profile);
      return done(null, profile); // ํ”„๋กœํ•„ ์ •๋ณด ์ €์žฅ
    }
  )
);

// ์„ธ์…˜์— ์‚ฌ์šฉ์ž ์ •๋ณด ์ €์žฅ
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));

// ๋ผ์šฐํŒ…
app.get("/", (req, res) => {
  res.send(`
    <h1>Google Login Example</h1>
    <a href="/auth/google">Login with Google</a>
  `);
});

// Google ๋กœ๊ทธ์ธ ์‹œ์ž‘
app.get(
  "/auth/google",
  passport.authenticate("google", { scope: ["profile", "email"] })
);

// Google ๋กœ๊ทธ์ธ ์ฝœ๋ฐฑ ์ฒ˜๋ฆฌ
app.get(
  "/auth/google/callback",
  passport.authenticate("google", { failureRedirect: "/" }),
  (req, res) => {
    // ๋กœ๊ทธ์ธ ์„ฑ๊ณต
    res.redirect("/profile");
  }
);

// ํ”„๋กœํ•„ ํŽ˜์ด์ง€
app.get("/profile", (req, res) => {
  if (!req.isAuthenticated()) {
    return res.redirect("/");
  }
  res.send(`
    <h1>Welcome, ${req.user.displayName}</h1>
    <p>Email: ${req.user.emails[0].value}</p>
    <a href="/logout">Logout</a>
  `);
});

// ๋กœ๊ทธ์•„์›ƒ
app.get("/logout", (req, res) => {
  req.logout();
  res.redirect("/");
});

// ์„œ๋ฒ„ ์‹คํ–‰
app.listen(3000, () => {
  console.log("Server started on http://localhost:3000");
});

๊ฒฐ๊ณผ

  1. ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:3000์— ์ ‘์†.
  2. "Login with Google" ๋ฒ„ํŠผ ํด๋ฆญ.
  3. Google ๋กœ๊ทธ์ธ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚˜๋ฉฐ, ๋กœ๊ทธ์ธ ํ›„ ๊ถŒํ•œ ํ—ˆ์šฉ.
  4. ์„ฑ๊ณตํ•˜๋ฉด ํ”„๋กœํ•„ ํŽ˜์ด์ง€์— ๋ฆฌ๋””๋ ‰์…˜๋˜์–ด ์‚ฌ์šฉ์ž ์ •๋ณด ํ‘œ์‹œ.

์ฃผ์˜ ์‚ฌํ•ญ

  • ๋ฆฌ๋””๋ ‰์…˜ URI๊ฐ€ Google Cloud Console์— ๋“ฑ๋ก๋œ URI์™€ ๋™์ผํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  • ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” HTTPS๋ฅผ ์‚ฌ์šฉํ•˜๊ณ , ์„ธ์…˜์— ๋Œ€ํ•œ ๋ณด์•ˆ์„ ๊ฐ•ํ™”ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.