Google OAuth 흐름

Google OAuth login 로직 구현
app.get("/auth/google", passport.authenticate("google"));
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const googleClientID = 메모장에 적어둔 clientId
const googleClientSecret = 메모장에 적어둔 client비밀번호
const googleStrategyConfig = new GoogleStrategy(
{
clientID: googleCliendID,
clientSecret: googleCliendSecret,
callbackURL: "/auth/google/callback",
scope: ["email", "profile"],
},
(accessToken, refreshToken, profile, done) => {
// 리디렉션 된 callbackURL 코드가 실행되는 곳
// 3번
console.log(profile);
}
);
// passport.use 무슨 전략을 사용할지(google
passport.use("google", googleStrategyConfig);
// app.js
app.get(
"/auth/google/callback",
passport.authenticate("google", {
successReturnToOrRedirect: "/",
failureRedirect: "/login",
})
);
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const googleStrategyConfig = new GoogleStrategy(
{
clientID: googleCliendID,
clientSecret: googleCliendSecret,
callbackURL: "/auth/google/callback",
scope: ["email", "profile"],
},
async (accessToken, refreshToken, profile, done) => {
// 리디렉션 된 callbackURL 코드가 실행되는 곳
// 3번
console.log(profile);
try {
const user = await User.findOne({ googleId: profile.id });
if (user) {
// done(null은 에러는 없고) user정보를 전달해준다.
return done(null, user);
} else {
const newUser = new User({
email: profile.emails[0].value,
googleId: profile.id,
});
await newUser.save();
return done(null, user);
}
} catch (err) {
console.log(err);
}
}
);
passport.use("google", googleStrategyConfig);