-
Notifications
You must be signed in to change notification settings - Fork 1
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
[feat] fixed auth context + sessionDemo #23
Open
RohinJ444
wants to merge
9
commits into
main
Choose a base branch
from
rohin/user-auth-new
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.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f8359e5
[feat + style] user authentication + styling
rohin-444 f745304
eslint style fixes
rohin-444 a931e5a
typo in text for signup page
rohin-444 f6559d0
forgot to capitalize the 'in' in 'Sign In'
rohin-444 e82d270
updated imports + links + added exclamation points
rohin-444 b720d7d
updated imports + links + added exclamation points
rohin-444 e7237cb
resolve merge conflict in app/page.tsx and app/auth/auth-styles.ts
rohin-444 06f9cc6
[feat] auth context
rohin-444 3d6775c
[feat] auth context with all files staged for commit
rohin-444 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
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,23 @@ | ||
'use client'; | ||
|
||
// import { useRouter } from 'next/navigation'; | ||
import { useSession } from '@/utils/AuthProvider'; | ||
|
||
export default function SessionDemo() { | ||
const { session, signOut } = useSession(); | ||
|
||
if (session === undefined) return <p>Loading...</p>; | ||
|
||
return ( | ||
<div> | ||
{session ? ( | ||
<> | ||
<h1>Welcome, {session.user?.email}!</h1> | ||
<button onClick={() => signOut()}>Logout</button> | ||
</> | ||
) : ( | ||
<p>You are not logged in.</p> | ||
)} | ||
</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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
'use client'; | ||
|
||
import { useState } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import supabase from '@/api/supabase/createClient'; | ||
import { H5 } from '@/styles/text'; | ||
import { useSession } from '@/utils/AuthProvider'; | ||
import { | ||
Button, | ||
Card, | ||
|
@@ -20,6 +22,8 @@ import { | |
} from '../auth-styles'; | ||
|
||
export default function SignIn() { | ||
const router = useRouter(); | ||
const sessionHandler = useSession(); | ||
const [email, setEmail] = useState(''); | ||
const [password, setPassword] = useState(''); | ||
const [message, setMessage] = useState(''); | ||
|
@@ -29,18 +33,35 @@ export default function SignIn() { | |
const handleSignIn = async () => { | ||
setMessage(''); | ||
setIsError(false); | ||
|
||
const { error } = await supabase.auth.signInWithPassword({ | ||
email, | ||
password, | ||
}); | ||
|
||
// Ik this wasn't part of the sprint but I added so I could verify that supabase functionality is working | ||
if (error) { | ||
setMessage(`Login failed: ${error.message}`); | ||
setIsError(true); | ||
} else { | ||
setMessage('Login successful!'); | ||
setIsError(false); | ||
try { | ||
const { error } = await sessionHandler.signInWithEmail(email, password); | ||
if (error) { | ||
setMessage(`Login failed: ${error.message}`); | ||
setIsError(true); | ||
} else { | ||
setMessage('Login successful!'); | ||
setIsError(false); | ||
setTimeout(() => { | ||
router.push('/sessionDemo'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you push |
||
}, 1000); | ||
} | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setMessage(`Login failed: ${error.message}`); | ||
} else { | ||
setMessage('Login failed: An unknown error occurred'); | ||
} | ||
setIsError(true); | ||
} | ||
} | ||
}; | ||
|
||
|
@@ -77,7 +98,7 @@ export default function SignIn() { | |
<span>or</span> | ||
</Separator> | ||
<GoogleButton>Continue with Google</GoogleButton> | ||
{message && <LoginMessage isError={isError}>{message}</LoginMessage>} | ||
{message && <LoginMessage $isError={isError}>{message}</LoginMessage>} | ||
</Form> | ||
</Card> | ||
<SmallBuffer /> | ||
|
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,92 @@ | ||
'use client'; | ||
|
||
import React, { | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { AuthResponse, Session } from '@supabase/supabase-js'; | ||
import supabase from '@/api/supabase/createClient'; | ||
|
||
export interface AuthState { | ||
session: Session | null; | ||
signIn: (newSession: Session | null) => void; | ||
signUp: (email: string, password: string) => Promise<AuthResponse>; | ||
signInWithEmail: (email: string, password: string) => Promise<AuthResponse>; | ||
signOut: () => void; | ||
} | ||
|
||
const AuthContext = createContext({} as AuthState); | ||
|
||
export function useSession() { | ||
const value = useContext(AuthContext); | ||
return value; | ||
} | ||
|
||
export function AuthContextProvider({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
const [session, setSession] = useState<Session | null>(null); | ||
|
||
/* useEffect(() => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you delete the comments? |
||
supabase.auth.getSession().then(({ data: { session: newSession } }) => { | ||
setSession(newSession); | ||
}); | ||
|
||
supabase.auth.onAuthStateChange((_event, newSession) => { | ||
setSession(newSession); | ||
}); | ||
}, []); */ | ||
|
||
useEffect(() => { | ||
supabase.auth.getSession().then(({ data: { session: newSession } }) => { | ||
console.log('Initial session:', newSession); | ||
setSession(newSession); | ||
}); | ||
|
||
supabase.auth.onAuthStateChange((_event, newSession) => { | ||
console.log('Session change event:', newSession); | ||
setSession(newSession); | ||
}); | ||
}, []); | ||
|
||
const signIn = (newSession: Session | null) => { | ||
setSession(newSession); | ||
}; | ||
|
||
const signInWithEmail = async (email: string, password: string) => | ||
supabase.auth.signInWithPassword({ | ||
email, | ||
password, | ||
}); // will trigger the use effect to update the session | ||
const signUp = async (email: string, password: string) => | ||
supabase.auth.signUp({ | ||
email, | ||
password, | ||
}); // will trigger the use effect to update the session | ||
const signOut = () => { | ||
supabase.auth.signOut(); | ||
setSession(null); | ||
}; | ||
|
||
const authContextValue = useMemo( | ||
() => ({ | ||
session, | ||
signIn, | ||
signUp, | ||
signInWithEmail, | ||
signOut, | ||
}), | ||
[session], | ||
); | ||
|
||
return ( | ||
<AuthContext.Provider value={authContextValue}> | ||
{children} | ||
</AuthContext.Provider> | ||
); | ||
} |
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.
thanks for the including this file for testing! can you unadd it so that i can merge it to main?
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.
oop yeah