-
Notifications
You must be signed in to change notification settings - Fork 455
Upgrade react router and add auth middleware #1040
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
Open
kentcdodds
wants to merge
6
commits into
main
Choose a base branch
from
cursor/upgrade-react-router-and-add-auth-middleware-3245
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 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f9766a
Refactor: Use react-router middleware for auth checks
cursoragent 05e00cf
Checkpoint before follow-up message
cursoragent 1e86901
Refactor: Apply requireAnonymousMiddleware to auth routes
cursoragent d0db7c6
Refactor: Use `middleware` instead of `unstable_middleware`
cursoragent ff7fc72
Refactor: Improve middleware and context usage
cursoragent 426b315
Extract router context to separate file
cursoragent 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 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,5 @@ | ||
import { createContext } from 'react-router' | ||
|
||
// Holds the authenticated user's ID for routes protected by middleware | ||
export const userIdContext = createContext<string | null>(null) | ||
|
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,35 @@ | ||
import { redirect, type MiddlewareFunction } from 'react-router' | ||
import { prisma } from '#app/utils/db.server.ts' | ||
import { authSessionStorage } from '#app/utils/session.server.ts' | ||
import { userIdContext } from '#app/context.ts' | ||
|
||
export const requireUserMiddleware: MiddlewareFunction = async ({ | ||
request, | ||
context, | ||
}) => { | ||
const cookie = request.headers.get('cookie') | ||
const session = await authSessionStorage.getSession(cookie) | ||
const sessionId = session.get('sessionId') as string | undefined | ||
if (!sessionId) throw redirect(`/login?redirectTo=${encodeURIComponent(new URL(request.url).pathname)}`) | ||
|
||
const sessionRecord = await prisma.session.findUnique({ | ||
select: { userId: true, expirationDate: true }, | ||
where: { id: sessionId }, | ||
}) | ||
|
||
if (!sessionRecord || sessionRecord.expirationDate < new Date()) { | ||
throw redirect(`/login?redirectTo=${encodeURIComponent(new URL(request.url).pathname)}`) | ||
} | ||
|
||
context.set(userIdContext, sessionRecord.userId) | ||
} | ||
|
||
export const requireAnonymousMiddleware: MiddlewareFunction = async ({ | ||
request, | ||
}) => { | ||
const cookie = request.headers.get('cookie') | ||
const session = await authSessionStorage.getSession(cookie) | ||
const sessionId = session.get('sessionId') as string | undefined | ||
if (sessionId) throw redirect('/') | ||
} | ||
|
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
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
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 |
---|---|---|
@@ -1,19 +1,3 @@ | ||
import { invariant } from '@epic-web/invariant' | ||
import { redirect } from 'react-router' | ||
import { verifySessionStorage } from '#app/utils/verification.server.ts' | ||
import { onboardingEmailSessionKey } from './onboarding.tsx' | ||
import { type VerifyFunctionArgs } from './verify.server.ts' | ||
import { requireAnonymousMiddleware } from '#app/middleware.server.ts' | ||
|
||
export async function handleVerification({ submission }: VerifyFunctionArgs) { | ||
invariant( | ||
submission.status === 'success', | ||
'Submission should be successful by now', | ||
) | ||
const verifySession = await verifySessionStorage.getSession() | ||
verifySession.set(onboardingEmailSessionKey, submission.value.target) | ||
return redirect('/onboarding', { | ||
headers: { | ||
'set-cookie': await verifySessionStorage.commitSession(verifySession), | ||
}, | ||
}) | ||
} | ||
export const middleware = [requireAnonymousMiddleware] |
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
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,3 @@ | ||
import { requireAnonymousMiddleware } from '#app/middleware.server.ts' | ||
|
||
export const middleware = [requireAnonymousMiddleware] |
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
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,3 @@ | ||
import { requireUserMiddleware } from '#app/middleware.server.ts' | ||
|
||
export const middleware = [requireUserMiddleware] |
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
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.
Could use this modified version https://gist.github.com/hilja/5e6f6ec6a86a3e06113501f8d60e20a4