-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move from next-auth to supabase-auth
- Loading branch information
Daniel Farina
authored and
Daniel Farina
committed
Nov 22, 2023
1 parent
c0160c4
commit 8010ff4
Showing
19 changed files
with
379 additions
and
594 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
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,129 @@ | ||
'use client' | ||
import { useCallback, useEffect, useState } from 'react' | ||
import { Database } from '@/types/database' | ||
import { Session, createClientComponentClient } from '@supabase/auth-helpers-nextjs' | ||
|
||
export default function AccountForm({ session }: { session: Session | null }) { | ||
const supabase = createClientComponentClient<Database>() | ||
const [loading, setLoading] = useState(true) | ||
const [fullname, setFullname] = useState<string | null>(null) | ||
const [username, setUsername] = useState<string | null>(null) | ||
const [website, setWebsite] = useState<string | null>(null) | ||
const [avatar_url, setAvatarUrl] = useState<string | null>(null) | ||
const user = session?.user | ||
|
||
const getProfile = useCallback(async () => { | ||
try { | ||
setLoading(true) | ||
|
||
const { data, error, status } = await supabase | ||
.from('profiles') | ||
.select(`full_name, username, website, avatar_url`) | ||
.eq('id', user?.id) | ||
.single() | ||
|
||
if (error && status !== 406) { | ||
throw error | ||
} | ||
|
||
if (data) { | ||
setFullname(data.full_name) | ||
setUsername(data.username) | ||
setWebsite(data.website) | ||
setAvatarUrl(data.avatar_url) | ||
} | ||
} catch (error) { | ||
alert('Error loading user data!') | ||
} finally { | ||
setLoading(false) | ||
} | ||
}, [user, supabase]) | ||
|
||
useEffect(() => { | ||
getProfile() | ||
}, [user, getProfile]) | ||
|
||
async function updateProfile({ | ||
username, | ||
website, | ||
avatar_url, | ||
}: { | ||
username: string | null | ||
fullname: string | null | ||
website: string | null | ||
avatar_url: string | null | ||
}) { | ||
try { | ||
setLoading(true) | ||
|
||
const { error } = await supabase.from('profiles').upsert({ | ||
id: user?.id as string, | ||
full_name: fullname, | ||
username, | ||
website, | ||
avatar_url, | ||
updated_at: new Date().toISOString(), | ||
}) | ||
if (error) throw error | ||
alert('Profile updated!') | ||
} catch (error) { | ||
alert('Error updating the data!') | ||
} finally { | ||
setLoading(false) | ||
} | ||
} | ||
|
||
return ( | ||
<div className="form-widget"> | ||
<div> | ||
<label htmlFor="email">Email</label> | ||
<input id="email" type="text" value={session?.user.email} disabled /> | ||
</div> | ||
<div> | ||
<label htmlFor="fullName">Full Name</label> | ||
<input | ||
id="fullName" | ||
type="text" | ||
value={fullname || ''} | ||
onChange={(e) => setFullname(e.target.value)} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="username">Username</label> | ||
<input | ||
id="username" | ||
type="text" | ||
value={username || ''} | ||
onChange={(e) => setUsername(e.target.value)} | ||
/> | ||
</div> | ||
<div> | ||
<label htmlFor="website">Website</label> | ||
<input | ||
id="website" | ||
type="url" | ||
value={website || ''} | ||
onChange={(e) => setWebsite(e.target.value)} | ||
/> | ||
</div> | ||
|
||
<div> | ||
<button | ||
className="button primary block" | ||
onClick={() => updateProfile({ fullname, username, website, avatar_url })} | ||
disabled={loading} | ||
> | ||
{loading ? 'Loading ...' : 'Update'} | ||
</button> | ||
</div> | ||
|
||
<div> | ||
<form action="/auth/signout" method="post"> | ||
<button className="button block" type="submit"> | ||
Sign out | ||
</button> | ||
</form> | ||
</div> | ||
</div> | ||
) | ||
} |
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,14 @@ | ||
import { createServerComponentClient } from '@supabase/auth-helpers-nextjs' | ||
import { cookies } from 'next/headers' | ||
import { Database } from '@/types/database' | ||
import AccountForm from './account-form' | ||
|
||
export default async function Account() { | ||
const supabase = createServerComponentClient<Database>({ cookies }) | ||
|
||
const { | ||
data: { session }, | ||
} = await supabase.auth.getSession() | ||
|
||
return <AccountForm session={session} /> | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs' | ||
import { cookies } from 'next/headers' | ||
import { NextResponse } from 'next/server' | ||
|
||
import type { Database } from '@/types/database' | ||
|
||
export async function GET(request: Request) { | ||
const requestUrl = new URL(request.url) | ||
const code = requestUrl.searchParams.get('code') | ||
|
||
if (code) { | ||
const supabase = createRouteHandlerClient<Database>({ cookies }) | ||
await supabase.auth.exchangeCodeForSession(code) | ||
} | ||
|
||
// URL to redirect to after sign in process completes | ||
return NextResponse.redirect(requestUrl.origin) | ||
} |
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,16 @@ | ||
import { createRouteHandlerClient } from '@supabase/auth-helpers-nextjs' | ||
import { cookies } from 'next/headers' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET(req: NextRequest) { | ||
const cookieStore = cookies() | ||
const supabase = createRouteHandlerClient({ cookies: () => cookieStore }) | ||
const { searchParams } = new URL(req.url) | ||
const code = searchParams.get('code') | ||
|
||
if (code) { | ||
await supabase.auth.exchangeCodeForSession(code) | ||
} | ||
|
||
return NextResponse.redirect(new URL('/', req.url)) | ||
} |
Oops, something went wrong.