-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat-#429: Added Signin with Google button #454
Merged
krishnaacharyaa
merged 5 commits into
krishnaacharyaa:main
from
siddharthisrani:bugfix/429-Want-to-Add-signin-with-google-button
Jul 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ba42d33
feat-#429: Added Google sign-in button to login page
siddharthisrani 558ffed
refactor-#429: Used environment variables for URLs in Google OAuth an…
siddharthisrani 3da3bd2
Merge branch 'main' into bugfix/429-Want-to-Add-signin-with-google-bu…
siddharthisrani 72e43f3
refactor-#429: Removed token saving in localstorage for Google OAuth
siddharthisrani 35b7810
refactor-#429: Removed token saving in localstorage for Google OAuth …
siddharthisrani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import passport from 'passport'; | ||
import { Strategy as GoogleStrategy } from 'passport-google-oauth20'; | ||
import User from '../models/user.js'; | ||
|
||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID, | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET, | ||
callbackURL: 'http://localhost:8080/api/auth/google/callback', | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
try { | ||
let user = await User.findOne({ googleId: profile.id }); | ||
|
||
if (!user) { | ||
const email = profile.emails && profile.emails[0] ? profile.emails[0].value : ''; | ||
let fullName = profile.displayName || ''; | ||
if (fullName.length > 15) { | ||
fullName = fullName.slice(0, 15); // Ensure fullName is less than 15 characters | ||
} | ||
const userName = email.split('@')[0] || fullName.replace(/\s+/g, '').toLowerCase(); | ||
|
||
user = new User({ | ||
googleId: profile.id, | ||
email, | ||
fullName, | ||
userName, | ||
avatar: profile.photos && profile.photos[0] ? profile.photos[0].value : '', | ||
}); | ||
|
||
await user.save(); | ||
} | ||
|
||
done(null, user); | ||
} catch (err) { | ||
done(err, null); | ||
} | ||
} | ||
) | ||
); | ||
|
||
passport.serializeUser((user, done) => done(null, user.id)); | ||
|
||
passport.deserializeUser(async (id, done) => { | ||
try { | ||
const user = await User.findById(id); | ||
done(null, user); | ||
} catch (err) { | ||
done(err, null); | ||
} | ||
}); | ||
|
||
export default passport; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { Router } from 'express'; | ||
import { authMiddleware } from '../middlewares/auth-middleware.js'; | ||
import passport from '../config/passport.js'; | ||
import jwt from 'jsonwebtoken'; | ||
import { | ||
signUpWithEmail, | ||
signInWithEmailOrUsername, | ||
|
@@ -13,10 +15,38 @@ const router = Router(); | |
router.post('/email-password/signup', signUpWithEmail); | ||
router.post('/email-password/signin', signInWithEmailOrUsername); | ||
|
||
// Google-login | ||
router.get('/google', passport.authenticate('google', { scope: ['profile', 'email'] })); | ||
|
||
router.get( | ||
'/google/callback', | ||
passport.authenticate('google', { failureRedirect: '/' }), | ||
(req, res) => { | ||
const token = jwt.sign({ id: req.user._id }, process.env.JWT_SECRET, { expiresIn: '1h' }); | ||
res.cookie('access_token', token, { | ||
httpOnly: true, | ||
secure: process.env.NODE_ENV === 'production', | ||
sameSite: 'lax', | ||
}); | ||
res.redirect('http://localhost:5173/signup?google-callback=true'); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, kindly use frontend url env variable |
||
); | ||
|
||
router.get('/check', authMiddleware, (req, res) => { | ||
const token = req.cookies.access_token; | ||
res.json({ | ||
token, | ||
user: { | ||
_id: req.user._id, | ||
role: req.user.role, | ||
}, | ||
}); | ||
}); | ||
|
||
//SIGN OUT | ||
router.post('/signout', authMiddleware, signOutUser); | ||
|
||
//CHECK USER STATUS | ||
router.get('/check/:_id', isLoggedIn); | ||
router.get('/check/:_id', authMiddleware, isLoggedIn); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we are deploying in vercel, we can't hardcode this to 8080, kindly move this to evn variable or in this case i guess you need to use the backend_url env variable