-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from samar12-rad/Google/oAuth
Feat: Google/o auth using passport js #148
- Loading branch information
Showing
14 changed files
with
198 additions
and
64 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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
MONGO_URI=enter_your_mongo_uri | ||
EMAIL_USER=your_gmail | ||
EMAIL_PASS=your_16_digit_pass | ||
JWT_SECRET=secret | ||
JWT_SECRET=secret | ||
GOOGLE_CLIENT_ID=your_google_client_id | ||
GOOGLE_CLIENT_SECRET=your_google_client_secret | ||
FRONTEND_URL=your_frontend_url |
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,39 @@ | ||
const GoogleStrategy = require("passport-google-oauth20").Strategy; | ||
const passport = require("passport"); | ||
const Customer = require("../models/customer.model"); // Adjust the path as needed | ||
const config = require("./secret"); // Import your secrets (client ID, client secret) | ||
|
||
passport.use( | ||
new GoogleStrategy( | ||
{ | ||
clientID: process.env.GOOGLE_CLIENT_ID || config.GOOGLE_CLIENT_ID, | ||
clientSecret: | ||
process.env.GOOGLE_CLIENT_SECRET || config.GOOGLE_CLIENT_SECRET, | ||
callbackURL: "/auth/google/callback", | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
try { | ||
// Extract the email from Google profile | ||
const email = profile.emails[0].value; | ||
|
||
// Search for the user in the database by email | ||
let user = await Customer.findOne({ email }); | ||
if (!user) { | ||
// If user doesn't exist, create a new user | ||
user = new Customer({ | ||
name: profile.displayName || "Unamed User", | ||
email: email, // Email from Google profile | ||
}); | ||
await user.save(); | ||
} | ||
// Return the user if exists or after creation | ||
return done(null, user); | ||
} catch (error) { | ||
console.error("Error during Google authentication:", error); | ||
return done(null, false, { message: "Authentication failed" }); | ||
} | ||
} | ||
) | ||
); | ||
|
||
module.exports = 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
const secret = require("../config/secret"); | ||
const jwt = require("jsonwebtoken"); | ||
const jwtSecret = secret.JWT_SECRET; | ||
|
||
const handleGoogleOAuth = async (req, res) => { | ||
const token = jwt.sign( | ||
{ | ||
sub: req.user._id, | ||
email: req.user.email, | ||
iat: Math.floor(Date.now() / 1000), | ||
aud: "play-cafe", | ||
}, | ||
jwtSecret, | ||
{ | ||
expiresIn: "1d", | ||
algorithm: "HS256", | ||
} | ||
); | ||
|
||
res.cookie("authToken", token, { | ||
maxAge: 3600000, | ||
secure: true, | ||
}); | ||
res.redirect(process.env.FRONTEND_URL || "http://localhost:3000"); | ||
}; | ||
|
||
module.exports = { | ||
handleGoogleOAuth, | ||
}; |
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.