Skip to content
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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from

Conversation

rachaelch3n
Copy link
Contributor

What's new in this PR 🧑‍🌾

Description

  • Implemented AuthContext to manage user authentication state
  • Added AuthProvider to wrap components and provide sign-in, sign-up, and sign-out functionality
  • Signing in reroutes to dashboard that confirms user is signed in, and users now have the option to sign out

Screenshots

Screen Shot 2024-10-22 at 6 57 56 PM Screen Shot 2024-10-22 at 6 57 32 PM

How to review

  • Review the AuthProvider implementation in utils/AuthProvider.tsx
  • Test the login flow in LoginPage.tsx and ensure signUp and signIn work as expected
  • Test the Dashboard component in DashboardPage.tsx to verify it correctly displays user data (e.g., email) and handles the sign-out process

Next steps

  • Styling

Relevant links

Online sources

Related PRs

CC: @ccatherinetan

@@ -14,9 +14,10 @@
"pre-commit": "(pnpm run tsc || true) && (pnpm run lint || true) && pnpm run prettier"

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)?

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?

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

Copy link
Collaborator

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

Copy link

@ronniebeggs ronniebeggs left a 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!

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.


// Define the shape of the context
interface AuthContextType {
authUser: any;

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.

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.

Comment on lines 95 to 96
setAuthUser(null);
setIsLoggedIn(false);

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"

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?

Comment on lines +6 to +12
export default function LoginLayout() {
return (
<AuthProvider>
<Login />
</AuthProvider>
);
}

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.

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

Comment on lines 27 to 30
<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 */}

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.

Copy link

@pragyakallanagoudar pragyakallanagoudar left a 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

Comment on lines +6 to +12
export default function LoginLayout() {
return (
<AuthProvider>
<Login />
</AuthProvider>
);
}

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

@@ -0,0 +1,5 @@
import { AuthProvider } from '../../utils/AuthProvider';

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.


// Define the shape of the context
interface AuthContextType {
authUser: any;

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

@ccatherinetan ccatherinetan linked an issue Oct 26, 2024 that may be closed by this pull request
@ccatherinetan ccatherinetan force-pushed the rachaelchen/tg-8-create-auth-context branch from 1e13530 to 1beb0f8 Compare November 8, 2024 21:54
Copy link
Collaborator

@ccatherinetan ccatherinetan left a 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!

Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's delete this!

Copy link
Collaborator

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

Comment on lines 22 to 24
interface AuthUser {
id: string;
email: string;
Copy link
Collaborator

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)


// AuthProvider component that wraps around your app or specific pages
export function AuthProvider({ children }: { children: ReactNode }) {
const [authUser, setAuthUser] = useState<AuthUser | null>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return (
<AuthContext.Provider value={value}>
{!loading ? children : <div>Loading...</div>}
Copy link
Collaborator

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>


setAuthUser(user);
setIsLoggedIn(true);
push('/dashboard'); // Redirect to dashboard after sign-in
Copy link
Collaborator

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

if (error) throw new Error(error.message);
setAuthUser(null);
setIsLoggedIn(false);
push('/login'); // Redirect to login after sign-out
Copy link
Collaborator

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?


setAuthUser(user);
setIsLoggedIn(true);
push('/dashboard'); // Redirect to dashboard after sign-up
Copy link
Collaborator

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create Auth Context
4 participants