-
Notifications
You must be signed in to change notification settings - Fork 0
Supabase Integration #5
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
390b063
installed dependencies / initiated supabase
orimcoding 3a92832
supabase middleware setup
orimcoding 0e1a15d
login/signup mock routes (for testing)
orimcoding 0f35e94
temp useAuth
orimcoding ed4107a
supabase-client interaction handling
orimcoding 51fd0e1
Update src/app/api/auth/login/route.ts
orimcoding 0b6d13b
proper input validation (CodeRabbit suggestion)
orimcoding b93d06f
client instance stabilization
orimcoding c6c0f25
Update src/middleware.ts
orimcoding f5a2b77
removed potential ReDoS regex cause
orimcoding 85d38bf
lint fixes
orimcoding 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,47 @@ | ||
| import { createServerSupabaseClient } from "@/lib/supabase/server"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| let body: unknown; | ||
| try { | ||
| body = await request.json(); | ||
| } catch { | ||
| return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); | ||
| } | ||
|
|
||
| const { email, password } = body as { | ||
| email?: unknown; | ||
| password?: unknown; | ||
| }; | ||
| if ( | ||
| typeof email !== "string" || | ||
| typeof password !== "string" || | ||
| !email || | ||
| !password | ||
| ) { | ||
| return NextResponse.json( | ||
| { error: "Email and password are required" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const supabase = await createServerSupabaseClient(); | ||
|
|
||
| const { data, error } = await supabase.auth.signInWithPassword({ | ||
| email, | ||
| password, | ||
| }); | ||
|
|
||
| if (error) { | ||
| return NextResponse.json({ error: error.message }, { status: 400 }); | ||
| } | ||
|
|
||
| return NextResponse.json(data); | ||
| } catch { | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import { createServerSupabaseClient } from "@/lib/supabase/server"; | ||
| import { NextRequest, NextResponse } from "next/server"; | ||
|
|
||
| export async function POST(request: NextRequest) { | ||
| try { | ||
| // Parse request body | ||
| let body: unknown; | ||
| try { | ||
| body = await request.json(); | ||
| } catch { | ||
| return NextResponse.json( | ||
| { error: "Invalid JSON request" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Validate body is an object | ||
| if (!body || typeof body !== "object") { | ||
| return NextResponse.json( | ||
| { error: "Invalid request body" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const { email, password } = body as Record<string, unknown>; | ||
|
|
||
| // Validate email and password exist and are strings | ||
| if (typeof email !== "string" || email.trim().length === 0) { | ||
| return NextResponse.json( | ||
| { error: "Email is required and must be a string" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| if (typeof password !== "string" || password.length === 0) { | ||
| return NextResponse.json( | ||
| { error: "Password is required and must be a string" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Validate email format - simple check, Supabase will do deeper validation | ||
| const trimmedEmail = email.trim(); | ||
| if (!trimmedEmail.includes("@") || !trimmedEmail.includes(".")) { | ||
| return NextResponse.json( | ||
| { error: "Invalid email format" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| const [localPart, domain] = trimmedEmail.split("@"); | ||
| if (!localPart || !domain || localPart.length === 0 || domain.length < 3) { | ||
| return NextResponse.json( | ||
| { error: "Invalid email format" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Validate password length | ||
| if (password.length < 8) { | ||
| return NextResponse.json( | ||
| { error: "Password must be at least 8 characters" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| const supabase = await createServerSupabaseClient(); | ||
|
|
||
| const { data, error } = await supabase.auth.signUp({ | ||
| email: email.trim(), | ||
| password, | ||
| }); | ||
|
|
||
| if (error) { | ||
| return NextResponse.json({ error: error.message }, { status: 400 }); | ||
| } | ||
|
|
||
| return NextResponse.json(data); | ||
| } catch { | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| "use client"; | ||
|
|
||
| import { createClient } from "@/lib/supabase/client"; | ||
| import { useEffect, useMemo, useState } from "react"; | ||
| import type { Session } from "@supabase/supabase-js"; | ||
|
|
||
| export function useAuth() { | ||
| const [session, setSession] = useState<Session | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
| const supabase = useMemo(() => createClient(), []); | ||
|
|
||
| useEffect(() => { | ||
| // Check active sessions and sets the user | ||
| supabase.auth.getSession().then(({ data: { session } }) => setSession(session)).finally(() => setLoading(false)); | ||
|
|
||
| // Listen for changes on auth state (logged in, signed out, etc.) | ||
| const { | ||
| data: { subscription }, | ||
| } = supabase.auth.onAuthStateChange((_event, session) => { | ||
| setSession(session); | ||
| }); | ||
|
|
||
| return () => subscription?.unsubscribe(); | ||
| }, [supabase]); | ||
|
|
||
| return { session, loading }; | ||
| } |
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,8 @@ | ||
| import { createBrowserClient } from "@supabase/ssr"; | ||
|
|
||
| export function createClient() { | ||
| return createBrowserClient( | ||
| process.env.NEXT_PUBLIC_SUPABASE_URL!, | ||
| process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY! | ||
| ); | ||
| } |
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.
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: orimcoding/swale
Length of output: 162
🏁 Script executed:
cat -n src/app/api/auth/login/route.ts | head -50Repository: orimcoding/swale
Length of output: 1519
Guard
bodyshape before destructuring to avoid 500 onnull.At Line 13, destructuring
bodyoccurs before confirming it is a non-null object. When a request body is JSONnull, it passes the JSON parsing check (Line 8) but causes a destructuring error on Line 13, which bubbles up to the outer catch block (Line 41) and returns 500 instead of 400.Add a guard to validate
bodyis a non-null object before destructuring:Proposed fix
let body: unknown; try { body = await request.json(); } catch { return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); } + if (typeof body !== "object" || body === null) { + return NextResponse.json({ error: "Invalid request body" }, { status: 400 }); + } + const { email, password } = body as { email?: unknown; password?: unknown; };📝 Committable suggestion
🤖 Prompt for AI Agents