-
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] created AuthContext #20
base: main
Are you sure you want to change the base?
Conversation
@@ -14,9 +14,10 @@ | |||
"pre-commit": "(pnpm run tsc || true) && (pnpm run lint || true) && pnpm run prettier" |
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.
cc @ccatherinetan, maybe we want to remove this change & also the package-lock.json
change? (do we want/need this change)?
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.
@pragyakallanagoudar what change are you referring to?
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.
this comment is fully on the wrong line lol but i'm referring to the one on line 17/20 of this file
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.
yes sg! we'll delete the @next/swc-darwin-arm64
line
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.
Looking good so far! I left a few comments and I imagine @ccatherinetan will also want to review soon as well. Good job!
package-lock.json
Outdated
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.
@ccatherinetan It seems that devs may've accidentally used npm
commands and now we have both npm
and pnpm
lock files. This may cause dependency issues in the future. I'd look into cleaning up the repository by deleting this package-lock.json
file.
app/utils/AuthProvider.tsx
Outdated
|
||
// Define the shape of the context | ||
interface AuthContextType { | ||
authUser: any; |
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.
Create an AuthUser
type/interface that contains user profile related attributes. This will get rid of the eslint error.
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.
The authUser
attribute would likely have an AuthUser | null
type.
app/utils/AuthProvider.tsx
Outdated
setAuthUser(null); | ||
setIsLoggedIn(false); |
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.
Consider when a logout event is sent from the server. This should redirect a user to the /login
screen.
@@ -14,9 +14,10 @@ | |||
"pre-commit": "(pnpm run tsc || true) && (pnpm run lint || true) && pnpm run prettier" |
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.
@pragyakallanagoudar what change are you referring to?
export default function LoginLayout() { | ||
return ( | ||
<AuthProvider> | ||
<Login /> | ||
</AuthProvider> | ||
); | ||
} |
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.
You only need to wrap the layout.tsx
file with the AuthProvider
to give its child screens access to context data. It seems that an Auth context would need to wrap the entire app, so look into wrapping the root layout.tsx
file with the auth provider.
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.
+1! See outermost layout.tsx
in IJP's codebase
app/dashboard/page.tsx
Outdated
<p>User is currently: {isLoggedIn ? 'Logged In' : 'Logged Out'}</p> | ||
{authUser && <p>User name: {authUser.email}</p>}{' '} | ||
{/* Display user's email */} | ||
<button onClick={signOut}>Log Out</button> {/* Logout button */} |
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.
Nitpick: it's better to have separate pages/flows for logged in/out users. However, this page looks like it'll be changed in the future, so keep this in mind when/if it does change.
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.
nice work @rachaelch3n ! have just left some comments / questions
export default function LoginLayout() { | ||
return ( | ||
<AuthProvider> | ||
<Login /> | ||
</AuthProvider> | ||
); | ||
} |
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.
+1! See outermost layout.tsx
in IJP's codebase
app/(auth)/login/layout.tsx
Outdated
@@ -0,0 +1,5 @@ | |||
import { AuthProvider } from '../../utils/AuthProvider'; |
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.
You can probably get rid of this whole file when you move the <AuthProvider>
wrap out to the outer layout.tsx
.
app/utils/AuthProvider.tsx
Outdated
|
||
// Define the shape of the context | ||
interface AuthContextType { | ||
authUser: any; |
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.
@ronniebeggs curious if you have thoughts on whether storing the AuthUser
object vs. the Session
in the AuthContext would be better? i did the latter in ijp but tbh i have no idea why
1e13530
to
1beb0f8
Compare
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.
looking great! i added some more comments, esp about commenting out code and adjusting the AuthProvider to include the auth session
as well as unpacking AuthUser
i'll try to take another look at it again soon!
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.
while this is a good test of using AuthProvider, we don't want a dashboard page yet, so let's delete this file.
You can leave the code in this file as a block comment in AuthProvider.tsx for example usage
app/utils.ts
Outdated
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.
let's delete this!
package-lock.json
Outdated
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.
this file should be deleted
app/utils/AuthProvider.tsx
Outdated
interface AuthUser { | ||
id: string; | ||
email: string; |
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.
we don't need to pack these together, we can directly have 2 states in AuthProvider (i.e. after line 40):
const [userId, setUserId] = useState<string | null>(null)
const [email, setEmail] = useState<string | null>(null)
app/utils/AuthProvider.tsx
Outdated
|
||
// AuthProvider component that wraps around your app or specific pages | ||
export function AuthProvider({ children }: { children: ReactNode }) { | ||
const [authUser, setAuthUser] = useState<AuthUser | null>(null); |
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.
we should be using tracking session as well, see https://github.com/calblueprint/immigration-justice-project/blob/main/src/utils/AuthProvider.tsx
app/utils/AuthProvider.tsx
Outdated
|
||
return ( | ||
<AuthContext.Provider value={value}> | ||
{!loading ? children : <div>Loading...</div>} |
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.
let's remove this conditional check. we can do this !loading
check on individual pages, but we don't want even the home page to display "Loading...". So these lines should look like
<AuthContext.Provider value={value}>
{children}
</AuthContext.Provider>
app/utils/AuthProvider.tsx
Outdated
|
||
setAuthUser(user); | ||
setIsLoggedIn(true); | ||
push('/dashboard'); // Redirect to dashboard after sign-in |
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.
the redirecting should be moved to /login
. The functions in AuthProvider should only handle the supabase calls and updating the AuthContext. All additional logic/processing should happen within the respective page
app/utils/AuthProvider.tsx
Outdated
if (error) throw new Error(error.message); | ||
setAuthUser(null); | ||
setIsLoggedIn(false); | ||
push('/login'); // Redirect to login after sign-out |
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.
flagging that this routing shouldn't be here
also to clarify, we're not using signOut
anywhere yet right?
app/utils/AuthProvider.tsx
Outdated
|
||
setAuthUser(user); | ||
setIsLoggedIn(true); | ||
push('/dashboard'); // Redirect to dashboard after sign-up |
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.
the redirecting should be moved to /signup. The functions in AuthProvider should only handle the supabase calls and updating the AuthContext. All additional logic/processing should happen within the respective page
also, since /dashboard is a test page that will be deleted, we should nav to '/' instead (i.e. the home page)
…d pushes to respective pages
What's new in this PR 🧑🌾
Description
Screenshots
How to review
Next steps
Relevant links
Online sources
Related PRs
CC: @ccatherinetan