-
Notifications
You must be signed in to change notification settings - Fork 154
Feat/GitHub signin oauth #336
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
Open
pratyushranjn
wants to merge
5
commits into
GitMetricsLab:main
Choose a base branch
from
pratyushranjn:feat/github-signin-oauth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+290
−68
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
255d658
feat: add GitHub OAuth sign-in support
pratyushranjn fc7af75
Merge remote-tracking branch 'upstream/main' into feat/github-signin-…
pratyushranjn 493a341
Merge remote-tracking branch 'upstream/main' into feat/github-signin-…
pratyushranjn 8a1d518
fix: address CodeRabbit review comments
pratyushranjn cb0157b
fix: address CodeRabbit review comments
pratyushranjn 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 hidden or 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,10 @@ | ||
| PORT=5000 | ||
| MONGO_URI=mongodb://localhost:27017/githubTracker | ||
| SESSION_SECRET=your-secret-key | ||
|
|
||
| # GitHub OAuth | ||
| GITHUB_CLIENT_ID=your_client_id | ||
| GITHUB_CLIENT_SECRET=your_secret | ||
| GITHUB_CALLBACK_URL=http://localhost:5000/api/auth/github/callback | ||
|
|
||
| FRONTEND_URL=http://localhost:5173 |
This file contains hidden or 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,45 +1,126 @@ | ||
| const passport = require("passport"); | ||
| const LocalStrategy = require('passport-local').Strategy; | ||
| const LocalStrategy = require("passport-local").Strategy; | ||
| const GitHubStrategy = require("passport-github2").Strategy; | ||
| const User = require("../models/User"); | ||
|
|
||
| passport.use( | ||
| new LocalStrategy( | ||
| { usernameField: "email" }, | ||
| async (email, password, done) => { | ||
| try { | ||
| const user = await User.findOne( {email} ); | ||
| if (!user) { | ||
| return done(null, false, { message: 'Email is invalid '}); | ||
| } | ||
|
|
||
| const isMatch = await user.comparePassword(password); | ||
| if (!isMatch) { | ||
| return done(null, false, { message: 'Invalid password' }); | ||
| } | ||
|
|
||
| return done(null, { | ||
| id : user._id.toString(), | ||
| username: user.username, | ||
| email: user.email | ||
| }); | ||
| } catch (err) { | ||
| return done(err); | ||
| new LocalStrategy( | ||
| { usernameField: "email" }, | ||
| async (email, password, done) => { | ||
| try { | ||
| const user = await User.findOne({ email }); | ||
|
|
||
| if (!user) { | ||
| return done(null, false, { | ||
| message: "Invalid email or password", | ||
| }); | ||
| } | ||
|
|
||
| if (!user.password) { | ||
| return done(null, false, { | ||
| message: "Use GitHub sign in for this account", | ||
| }); | ||
| } | ||
|
|
||
| const isMatch = await user.comparePassword(password); | ||
|
|
||
| if (!isMatch) { | ||
| return done(null, false, { | ||
| message: "Invalid email or password", | ||
| }); | ||
| } | ||
|
|
||
| return done(null, { | ||
| id: user._id.toString(), | ||
| username: user.username, | ||
| email: user.email, | ||
| }); | ||
| } catch (err) { | ||
| return done(err); | ||
| } | ||
| } | ||
| ) | ||
| ); | ||
|
|
||
| if (process.env.GITHUB_CLIENT_ID && process.env.GITHUB_CLIENT_SECRET) { | ||
| passport.use( | ||
| new GitHubStrategy( | ||
| { | ||
| clientID: process.env.GITHUB_CLIENT_ID, | ||
| clientSecret: process.env.GITHUB_CLIENT_SECRET, | ||
| callbackURL: process.env.GITHUB_CALLBACK_URL, | ||
| scope: ["user:email"], | ||
| state: true, | ||
| }, | ||
|
|
||
| async (accessToken, refreshToken, profile, done) => { | ||
| try { | ||
| const primaryEmail = profile.emails?.[0]?.value; | ||
| const avatar = profile.photos?.[0]?.value || ""; | ||
|
|
||
| let user = await User.findOne({ githubId: profile.id }); | ||
|
|
||
| if (!user && primaryEmail) { | ||
| user = await User.findOne({ email: primaryEmail }); | ||
| } | ||
|
|
||
| if (!user) { | ||
| const loginName = | ||
| profile.username || `github_${profile.id}`; | ||
|
|
||
| const uniqueSuffix = Math.random() | ||
| .toString(36) | ||
| .slice(2, 7); | ||
|
|
||
| const userData = { | ||
| githubId: profile.id, | ||
| username: `${loginName}_${uniqueSuffix}`, | ||
| avatar, | ||
| }; | ||
|
|
||
| if (primaryEmail) { | ||
| userData.email = primaryEmail; | ||
| } | ||
|
|
||
| user = new User(userData); | ||
|
|
||
| } else { | ||
| user.githubId = user.githubId || profile.id; | ||
|
|
||
| if (primaryEmail) { | ||
| user.email = user.email || primaryEmail; | ||
| } | ||
|
|
||
| user.avatar = user.avatar || avatar; | ||
| } | ||
|
|
||
| await user.save(); | ||
|
|
||
| return done(null, { | ||
| id: user._id.toString(), | ||
| username: user.username, | ||
| email: user.email, | ||
| }); | ||
|
|
||
| } catch (err) { | ||
| return done(err); | ||
| } | ||
| } | ||
| ) | ||
| ); | ||
| ); | ||
| } | ||
|
|
||
| // Serialize user (store user info in session) | ||
| // Serialize user | ||
| passport.serializeUser((user, done) => { | ||
| done(null, user.id); | ||
| done(null, user.id); | ||
| }); | ||
|
|
||
| // Deserialize user (retrieve user from session) | ||
| // Deserialize user | ||
| passport.deserializeUser(async (id, done) => { | ||
| try { | ||
| const user = await User.findById(id); | ||
| done(null, user); | ||
| } catch (err) { | ||
| done(err, null); | ||
| } | ||
| }); | ||
| try { | ||
| const user = await User.findById(id); | ||
| done(null, user); | ||
| } catch (err) { | ||
| done(err, null); | ||
| } | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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.
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.
🧩 Analysis chain
🏁 Script executed:
Repository: GitMetricsLab/github_tracker
Length of output: 3224
Fail fast if
SESSION_SECRETis missing in production (backend/server.js:26)express-sessioncurrently falls back to'dev-session-secret', making session cookie signing predictable when prod env config is incomplete. (Also, withsameSite: 'lax', cross-site credentialed requests may fail if the frontend is on a different site.)Suggested fix
🤖 Prompt for AI Agents