Recent Posts
Recent Comments
Link
| ์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
|---|---|---|---|---|---|---|
| 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 | 31 |
Tags
- ๋ ผ๋ธ๋กํน
- ์ฟ ๋ฒ๋คํฐ์ค
- MySQL
- @scheduler
- oauth
- spring scheduler
- ์๊ณ ๋ฆฌ์ฆ
- ํฌ๋ก์ค์ฌ์ดํธ ์คํฌ๋ฆฝํธ
- db์ ๋ต
- Java
- React
- Jenkins
- SpringBoot
- ์๋ฐ
- GoogleLogin
- ์ฝํ
- ์ค๋ธ์
- db์ํคํ ์ณ
- ๋์์ธํจํด
- dockerhub
- ์ฝ๋ฉํ ์คํธ
- Personal Access Token
- java์ฑํ
- injection๊ณต๊ฒฉ
- spring
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- docker
- CICD
- node.js mysql ์ฐ๋
- Node.js
Archives
- Today
- Total
<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 ๋ก๊ทธ์ธ ๊ตฌํ ์์ ์ ๋๋ค.
์ฌ์ ์ค๋น
- Google Cloud Console์์ ํ๋ก์ ํธ ์์ฑ
- Google Cloud Console์ ์ ์.
- ์ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ฑฐ๋ ๊ธฐ์กด ํ๋ก์ ํธ๋ฅผ ์ ํ.
- OAuth ๋์ ํ๋ฉด ์ค์ :
- ์ฌ์ฉ์ ์ ํ ์ ํ(์ธ๋ถ/๋ด๋ถ).
- ํ์ ์ ๋ณด ์ ๋ ฅ(์ ํ๋ฆฌ์ผ์ด์ ์ด๋ฆ, ์ด๋ฉ์ผ ๋ฑ).
- OAuth 2.0 ํด๋ผ์ด์ธํธ ID ์์ฑ:
- ์ ํ๋ฆฌ์ผ์ด์ ์ ํ: ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ํ.
- ์น์ธ๋ ๋ฆฌ๋๋ ์ URI ์ถ๊ฐ: ์) http://localhost:3000/auth/google/callback
- ํด๋ผ์ด์ธํธ ID์ ํด๋ผ์ด์ธํธ ๋น๋ฐ ํค(Client Secret)๋ฅผ ๋ณต์ฌ.
- ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์น
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");
});
๊ฒฐ๊ณผ
- ๋ธ๋ผ์ฐ์ ์์ http://localhost:3000์ ์ ์.
- "Login with Google" ๋ฒํผ ํด๋ฆญ.
- Google ๋ก๊ทธ์ธ ํ๋ฉด์ด ๋ํ๋๋ฉฐ, ๋ก๊ทธ์ธ ํ ๊ถํ ํ์ฉ.
- ์ฑ๊ณตํ๋ฉด ํ๋กํ ํ์ด์ง์ ๋ฆฌ๋๋ ์ ๋์ด ์ฌ์ฉ์ ์ ๋ณด ํ์.
์ฃผ์ ์ฌํญ
- ๋ฆฌ๋๋ ์ URI๊ฐ Google Cloud Console์ ๋ฑ๋ก๋ URI์ ๋์ผํด์ผ ํฉ๋๋ค.
- ํ๋ก๋์ ํ๊ฒฝ์์๋ HTTPS๋ฅผ ์ฌ์ฉํ๊ณ , ์ธ์ ์ ๋ํ ๋ณด์์ ๊ฐํํด์ผ ํฉ๋๋ค.
'๐ฑ JavaScript > ใ คNode' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Node.js] OAuth2 ๋ก๊ทธ์ธ - 1 (0) | 2024.11.18 |
|---|---|
| [Node.js] bcrypt ๋ก ํ์๊ฐ์ ๊ตฌํํ๊ธฐ (0) | 2024.10.15 |
| [Node.js] Express ์๋ฒ๋ฅผ ๋ง๋ค์ด ๋ณด์ (0) | 2024.09.24 |
| [Node.js] Node.js(Express) โ๏ธ MySQL ๋ฐ์ดํฐ๋ฒ ์ด์ค๐ฆ ์ฐ๋ํ๊ธฐ (0) | 2024.09.23 |