-
Notifications
You must be signed in to change notification settings - Fork 7
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
Google auth added #93
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
602a6f9
google auth added
Naresh-chandanbatve a5d2eb6
Merge branch 'main' of https://github.com/UBA-GCOEN/ARrow into google…
Naresh-chandanbatve f893d0e
google auth added
Naresh-chandanbatve a89e293
removed redundant user admin model
Naresh-chandanbatve 261612e
removed useradmin controller and useradmin routes
Naresh-chandanbatve 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import '../middlewares/passportConfig.js' | ||
import passport from 'passport' | ||
import generateToken from "../middlewares/generateToken.js"; | ||
|
||
|
||
|
||
/** | ||
* Route: /auth/google | ||
* Desc: Open google consent screen | ||
*/ | ||
export const authGoogle = passport.authenticate('google', { scope: [ 'email', 'profile' ]}) | ||
|
||
|
||
|
||
/** | ||
* Route: /auth/google/callback | ||
* Desc: handle callback from google | ||
*/ | ||
export const callbackGoogle = passport.authenticate('google', { successRedirect: '/auth/protected', failureRedirect: '/auth/failed' }) | ||
|
||
|
||
|
||
/** | ||
* Route /protected | ||
* desc: reditrection after successfull | ||
* google auth with userdata in req | ||
*/ | ||
export const authenticated = (req, res)=>{ | ||
// let name = req.user.displayName | ||
|
||
const SECRET = process.env.USER_SECRET | ||
const token = generateToken(req.user, SECRET); | ||
|
||
req.session.user = { | ||
token: token, | ||
user: req.user | ||
} | ||
|
||
res.status(200).json({ | ||
success: true, | ||
user: req.user, | ||
token: token | ||
}) | ||
} | ||
|
||
|
||
/** | ||
* Route: /failed | ||
* Desc: Redirection if google authentication failed | ||
*/ | ||
export const failed = (req, res)=>{ | ||
res.status(401).send("google authentication failed") | ||
} |
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,73 @@ | ||
import passport from 'passport' | ||
import { Strategy as GoogleStrategy } from 'passport-google-oauth20' | ||
import * as dotenv from "dotenv"; | ||
dotenv.config(); | ||
import userModel from '../models/userModel.js'; | ||
import session from 'express-session'; | ||
|
||
|
||
const initializePassport = (app) => { | ||
app.use( | ||
session({ | ||
secret: process.env.SESSION_SECRET, | ||
resave: false, | ||
saveUninitialized: true | ||
}) | ||
Comment on lines
+11
to
+15
Check warning Code scanning / CodeQL Clear text transmission of sensitive cookie Medium
Sensitive cookie sent without enforcing SSL encryption.
|
||
); | ||
|
||
app.use(passport.initialize()); | ||
app.use(passport.session()); | ||
|
||
|
||
|
||
passport.use(new GoogleStrategy({ | ||
clientID: process.env.CLIENT_ID, | ||
clientSecret: process.env.CLIENT_SECRET, | ||
callbackURL: process.env.BASE_URL+'/auth/google/callback' | ||
}, | ||
async (accessToken, refreshToken, profile, done) => { | ||
// Handle user data and pass it to the 'done' callback | ||
// Typically, you would save user data to your database here | ||
try { | ||
|
||
let User = await userModel.findOne({ googleId: profile.id }); | ||
|
||
// If user already exists, return the user | ||
if (User) { | ||
return done(null, User); | ||
} | ||
|
||
// If user does not exist, create a new user record | ||
const result = await userModel.create({ | ||
googleId: profile.id, | ||
name: profile.displayName, | ||
email: profile.emails[0].value, | ||
}); | ||
if(result){ | ||
console.log("user created"+ result) | ||
} | ||
|
||
let newUser = await userModel.findOne({ googleId: profile.id }) | ||
|
||
return done(null, newUser); | ||
} | ||
catch(err){ | ||
return done(err); | ||
} | ||
} | ||
)); | ||
|
||
passport.serializeUser((user, done )=>{ | ||
done(null, user) | ||
}) | ||
|
||
passport.deserializeUser((user, done) => { | ||
done(null, user) | ||
}) | ||
|
||
|
||
} | ||
|
||
|
||
|
||
export default initializePassport; |
This file was deleted.
Oops, something went wrong.
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.
Check failure
Code scanning / CodeQL
Missing CSRF middleware High