-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat-#429: Added Google sign-in button to login page
- Loading branch information
1 parent
db4b416
commit ba42d33
Showing
11 changed files
with
242 additions
and
41 deletions.
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
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.