-
Notifications
You must be signed in to change notification settings - Fork 8
Fixed Issue #6 #44
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
Closed
Closed
Fixed Issue #6 #44
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
ca0dc14
Added Mongodb.ts for auth setup
2980851
feat: setup Auth.js with MongoDB
bea1245
feat: Implemented email link authentication with domain restriction t…
290cdc6
feat: add Google OAuth provider with domain restriction as well
86b2db1
feat: added a rate limit feature
66b188d
Feat: added logging got auth failures
9ac7f80
Added set password for Google Sign in Users
5e90675
Fixed password authorization and credential login, along with hashing…
e0cf018
Fixed various previous issues with implementation of more clear syste…
d44bc68
fixing stagingUser and createuser adapter override function
b4e4cfc
Fix: rate limit issue
9237b16
Fixing logging issue
36d00bf
added trim().toLowerCase, along with adding domain check to login and…
a4ecc38
Callback url redirection.
7be69d4
Finished setting up a proper access denied page for emails not follow…
04da815
feat: Setup forgot password through link through mail.
b6acefb
Merge branch 'main' into feat/auth-setup
Freny07 560d57f
Fixed dependency repetetion issue.
5ee3d2e
Fixed dependency repetetion issue.
85ab125
Finished setting up a proper access denied page for emails not follow…
e12db39
Fixedissues persisting in pnpm run lint
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
Some comments aren't visible on the classic Files Changed page.
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 |
|---|---|---|
|
|
@@ -32,6 +32,7 @@ yarn-error.log* | |
|
|
||
| # env files (can opt-in for committing if needed) | ||
| .env* | ||
| .env.local | ||
|
|
||
| # vercel | ||
| .vercel | ||
|
|
||
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,30 @@ | ||
| "use client" | ||
|
|
||
| import { useSession } from "next-auth/react" | ||
| import { useRouter } from "next/navigation" | ||
| import { useEffect } from "react" | ||
|
|
||
| export default function HomeWrapper({ children }: { children: React.ReactNode }) { | ||
| const { data: session, status } = useSession() | ||
| const router = useRouter() | ||
|
|
||
| useEffect(() => { | ||
| if (status === "loading") return | ||
|
|
||
| if (session?.user?.needsPassword) { | ||
| router.replace("/set-password?email=" + session.user.email) | ||
| } | ||
| }, [session, status, router]) | ||
|
|
||
| if (status === "loading") { | ||
| return ( | ||
| <div className="flex min-h-screen items-center justify-center"> | ||
| <p className="text-sm text-muted">Loading...</p> | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| if (session?.user?.needsPassword) return null | ||
|
|
||
| return <>{children}</> | ||
| } | ||
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,3 @@ | ||
|
|
||
|
|
||
| export { GET, POST } from "@/lib/auth" |
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 @@ | ||
| import { NextResponse } from "next/server" | ||
| import { checkRateLimit } from "@/lib/rateLimit" | ||
|
|
||
| export async function POST(req: Request) { | ||
| const { email } = await req.json() | ||
|
|
||
| const result = checkRateLimit(email) | ||
|
|
||
| return NextResponse.json(result) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } | ||
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,38 @@ | ||
| import { NextResponse } from "next/server" | ||
| import clientPromise from "@/lib/mongodb" | ||
| import bcrypt from "bcryptjs" | ||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
| const { email, password } = await req.json() | ||
|
|
||
| if (!email || !password) { | ||
| return NextResponse.json( | ||
| { error: "Missing email or password" }, | ||
| { status: 400 } | ||
| ) | ||
| } | ||
|
|
||
| const client = await clientPromise | ||
| const db = client.db() | ||
|
|
||
| const hashedPassword = await bcrypt.hash(password, 10) | ||
|
|
||
| await db.collection("users").updateOne( | ||
| { email }, | ||
| { | ||
| $set: { | ||
| password: hashedPassword, | ||
| }, | ||
| } | ||
| ) | ||
|
|
||
| return NextResponse.json({ success: true }) | ||
|
coderabbitai[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| } catch (err) { | ||
| console.error("SET PASSWORD ERROR:", err) | ||
| return NextResponse.json( | ||
| { error: "Internal server error" }, | ||
| { status: 500 } | ||
| ) | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| "use client" | ||
|
|
||
| import Link from "next/link" | ||
| import { Section } from "@/components/Section" | ||
| import { signIn } from "next-auth/react" | ||
| import { useState } from "react" | ||
|
|
||
| export default function LoginClient() { | ||
| const [email, setEmail] = useState("") | ||
| const [error, setError] = useState("") | ||
|
|
||
| const handleLogin = async (e: React.FormEvent) => { | ||
| e.preventDefault() | ||
|
|
||
| setError("") | ||
|
|
||
| try { | ||
| // rate limit check BEFORE signIn | ||
| const res = await fetch("/api/rate-limit", { | ||
| method: "POST", | ||
| headers: { | ||
| "Content-Type": "application/json", | ||
| }, | ||
| body: JSON.stringify({ email }), | ||
| }) | ||
|
|
||
| const data = await res.json() | ||
|
|
||
| if (!data.allowed) { | ||
| setError("Too many requests. Please try again later.") | ||
| return | ||
| } | ||
|
|
||
| // proceed normally | ||
| await signIn("email", { | ||
| email, | ||
| callbackUrl: "/", | ||
| }) | ||
| } catch (err) { | ||
| setError("Something went wrong. Please try again.") | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <Section className="py-20"> | ||
| <div className="mx-auto max-w-md rounded-xl border border-border bg-background p-8"> | ||
| <p className="mt-2 text-sm text-muted"> | ||
| Sign in to access the alumni directory, events, and the job board. | ||
| </p> | ||
|
|
||
| <form className="mt-6 space-y-4" onSubmit={handleLogin}> | ||
| <div> | ||
| <label className="text-sm font-medium">Email</label> | ||
| <input | ||
| type="email" | ||
| value={email} | ||
| onChange={(e) => setEmail(e.target.value)} | ||
| className="mt-1 h-10 w-full rounded-md border border-border bg-background px-3 text-sm" | ||
| /> | ||
| </div> | ||
|
|
||
| <div> | ||
| <label className="text-sm font-medium">Password</label> | ||
| <input | ||
| type="password" | ||
| className="mt-1 h-10 w-full rounded-md border border-border bg-background px-3 text-sm" | ||
| /> | ||
| </div> | ||
|
|
||
| <button | ||
| type="submit" | ||
| className="inline-flex h-11 w-full items-center justify-center rounded-md bg-brand text-sm font-semibold text-white hover:bg-brand-700" | ||
| > | ||
| Sign in | ||
| </button> | ||
| </form> | ||
|
|
||
| {/* ERROR MESSAGE (no UI change, just added) */} | ||
| {error && ( | ||
| <p className="mt-2 text-sm text-red-500"> | ||
| {error} | ||
| </p> | ||
| )} | ||
|
|
||
| <button | ||
| type="button" | ||
| onClick={() => signIn("google", { callbackUrl: "/" })} | ||
| className="mt-4 inline-flex h-11 w-full items-center justify-center rounded-md border border-border text-sm font-semibold" | ||
| > | ||
| Sign in with Google | ||
| </button> | ||
|
|
||
| <p className="mt-4 text-center text-sm text-muted"> | ||
| New to IIITL Alumni?{" "} | ||
| <Link href="/register" className="font-medium text-brand"> | ||
| Create an account | ||
| </Link> | ||
| </p> | ||
| </div> | ||
| </Section> | ||
| ) | ||
| } |
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,7 @@ | ||
| import Link from "next/link"; | ||
| import { Section } from "@/components/Section"; | ||
| import LoginClient from "./LoginClient" | ||
|
|
||
| export const metadata = { title: "Sign in" }; | ||
| export const metadata = { title: "Sign in" } | ||
|
|
||
| export default function LoginPage() { | ||
| return ( | ||
| <Section className="py-20"> | ||
| <div className="mx-auto max-w-md rounded-xl border border-border bg-background p-8"> | ||
| <h1 className="font-serif text-3xl font-semibold">Welcome back</h1> | ||
| <p className="mt-2 text-sm text-muted"> | ||
| Sign in to access the alumni directory, events, and the job board. | ||
| </p> | ||
| <form className="mt-6 space-y-4"> | ||
| <div> | ||
| <label className="text-sm font-medium">Email</label> | ||
| <input | ||
| type="email" | ||
| className="mt-1 h-10 w-full rounded-md border border-border bg-background px-3 text-sm" | ||
| /> | ||
| </div> | ||
| <div> | ||
| <label className="text-sm font-medium">Password</label> | ||
| <input | ||
| type="password" | ||
| className="mt-1 h-10 w-full rounded-md border border-border bg-background px-3 text-sm" | ||
| /> | ||
| </div> | ||
| <button | ||
| type="button" | ||
| className="inline-flex h-11 w-full items-center justify-center rounded-md bg-brand text-sm font-semibold text-white hover:bg-brand-700" | ||
| > | ||
| Sign in | ||
| </button> | ||
| </form> | ||
| <p className="mt-4 text-center text-sm text-muted"> | ||
| New to IIITL Alumni?{" "} | ||
| <Link href="/register" className="font-medium text-brand"> | ||
| Create an account | ||
| </Link> | ||
| </p> | ||
| </div> | ||
| </Section> | ||
| ); | ||
| } | ||
| export default function Page() { | ||
| return <LoginClient /> | ||
| } |
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.
Do not put user email in the redirect URL.
On Line 15, appending
session.user.emailto the query string exposes PII in logs/history/referrers. Redirect to/set-passwordwithout email and resolve identity from the authenticated session on that page/API.Proposed patch
📝 Committable suggestion
🤖 Prompt for AI Agents
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.
@Freny07 note