-
-
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 branch 'main' into bug-GifResponsive
- Loading branch information
Showing
18 changed files
with
411 additions
and
110 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,11 @@ | ||
MONGO_URI=enter_your_mongo_uri | ||
EMAIL_USER=your_gmail | ||
PORT=3000 | ||
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 | ||
CALLBACK_URL=http://localhost:3000/auth/google/callback | ||
PROD_CALLBACK_URL=https://play-cafe.vercel.app/auth/google/callback | ||
NODE_ENV=development |
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,43 @@ | ||
/* eslint-disable prettier/prettier */ | ||
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) | ||
console.log("config", config); | ||
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: | ||
process.env.NODE_ENV === "production" | ||
? process.env.PROD_CALLBACK_URL // Use production callback URL | ||
: process.env.CALLBACK_URL, // Use development (localhost) callback URL | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// passportConfig.js | ||
const { Strategy: JwtStrategy, ExtractJwt } = require("passport-jwt"); | ||
const passport = require("passport"); | ||
const config = require("./secret"); | ||
const Customer = require("../models/customer.model"); | ||
const Admin = require("../models/admin.model"); | ||
require("./oauth.config"); | ||
|
||
// Secret key to sign the JWT token | ||
const secret = config.JWT_SECRET; | ||
const opts = { | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.JWT_SECRET || secret, | ||
algorithms: ["HS256"], | ||
}; | ||
|
||
passport.use( | ||
new JwtStrategy(opts, (jwt_payload, done) => { | ||
// jwt_payload contains the decoded token | ||
// You can use the payload data (such as user id) to check if the user exists | ||
|
||
const userId = jwt_payload.sub; | ||
const role = jwt_payload.role; | ||
const roleModelMap = { | ||
customer: Customer, | ||
admin: Admin, | ||
}; | ||
const Model = roleModelMap[role]; | ||
if (Model) { | ||
Model.findById(userId) | ||
.then((user) => { | ||
if (user) { | ||
return done(null, user); | ||
} | ||
return done(null, false); | ||
}) | ||
.catch((error) => { | ||
return done(error, false); | ||
}); | ||
} else { | ||
// Handle unknown roles | ||
return done(null, false); | ||
} | ||
}) | ||
); | ||
|
||
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
Oops, something went wrong.