-
Notifications
You must be signed in to change notification settings - Fork 15
Implement flow for reauthenticating the user to request additional scopes #266
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ddc0763
Initial reauthenticate flow
tom-sherman f1fc640
Remove unused import
tom-sherman d42c5d0
Add avatar
tom-sherman 4091d98
Move auth scope check to layout
tom-sherman 34e8eff
Move default auth scopes to application code
tom-sherman 5465f67
Add default
tom-sherman 659251d
Better migration
tom-sherman 5ec1593
Use const as default scope
tom-sherman 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
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,9 @@ | ||
import { Card } from "@/lib/components/ui/card"; | ||
|
||
export default function Layout({ children }: { children: React.ReactNode }) { | ||
return ( | ||
<div className="flex items-center justify-center min-h-screen px-4 sm:px-6 lg:px-8"> | ||
<Card className="w-full max-w-md space-y-6 p-6">{children}</Card> | ||
</div> | ||
); | ||
} |
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
21 changes: 21 additions & 0 deletions
21
packages/frontpage/app/(auth)/reauthenticate/_lib/reauthenticate-action.ts
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,21 @@ | ||
"use server"; | ||
|
||
import { getSession } from "@/lib/auth"; | ||
import { signIn } from "@/lib/auth-sign-in"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export async function reauthenticateAction() { | ||
const session = await getSession(); | ||
if (!session) { | ||
redirect("/login?error=You've been logged out. Please log in again."); | ||
} | ||
const result = await signIn({ | ||
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. If this is successful we should probably delete the old session here. Could just leave it orphaned and let it expire naturally tho. |
||
identifier: session.user.did, | ||
}); | ||
|
||
if (result && "error" in result) { | ||
return { | ||
error: `An error occurred while re-authenticating (${result.error}), please try again.`, | ||
}; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/frontpage/app/(auth)/reauthenticate/_lib/reauthenticate-form.tsx
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,37 @@ | ||
"use client"; | ||
|
||
import { type ReactNode, useActionState } from "react"; | ||
import { reauthenticateAction } from "./reauthenticate-action"; | ||
import { Button } from "@/lib/components/ui/button"; | ||
import { Alert, AlertDescription, AlertTitle } from "@/lib/components/ui/alert"; | ||
import { CrossCircledIcon } from "@radix-ui/react-icons"; | ||
|
||
export function ReauthenticateForm({ | ||
// TODO: Use this prop to redirect after re-authentication, requires changes in signIn method | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
redirectPath, | ||
avatar, | ||
}: { | ||
redirectPath?: string; | ||
avatar: ReactNode; | ||
}) { | ||
const [state, action, isPending] = useActionState(reauthenticateAction, null); | ||
|
||
return ( | ||
<div className="space-y-3"> | ||
<form action={action} className="flex gap-2 items-center"> | ||
<div>{avatar}</div> | ||
<Button type="submit" disabled={isPending} size="lg" className="w-full"> | ||
Re-authenticate now | ||
</Button> | ||
</form> | ||
{state?.error ? ( | ||
<Alert variant="destructive"> | ||
<CrossCircledIcon className="h-4 w-4" /> | ||
<AlertTitle>Error</AlertTitle> | ||
<AlertDescription>{state?.error}</AlertDescription> | ||
</Alert> | ||
) : null} | ||
</div> | ||
); | ||
} |
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,60 @@ | ||
import { getSession, signOut } from "@/lib/auth"; | ||
import { AUTH_SCOPES } from "@repo/frontpage-oauth"; | ||
import { redirect } from "next/navigation"; | ||
import { ReauthenticateForm } from "./_lib/reauthenticate-form"; | ||
import { Button } from "@/lib/components/ui/button"; | ||
import { revalidatePath } from "next/cache"; | ||
import { UserAvatar } from "@/lib/components/user-avatar"; | ||
|
||
export default async function LoginPage({ | ||
searchParams, | ||
}: { | ||
searchParams: Promise<{ redirect?: string }>; | ||
}) { | ||
const session = await getSession(); | ||
if (!session) { | ||
redirect("/login?error=You've been logged out. Please log in again."); | ||
} | ||
|
||
const redirectParam = (await searchParams).redirect; | ||
|
||
// TODO: Test this doesnt allow you to redirect to an external URL | ||
const redirectPath = redirectParam?.startsWith("/") ? redirectParam : "/"; | ||
|
||
if (session.user.scope === AUTH_SCOPES) { | ||
console.warn( | ||
"User has AUTH_SCOPES, redirecting to the specified path or defaulting to /", | ||
); | ||
redirect(redirectPath); | ||
} | ||
|
||
return ( | ||
<> | ||
<div className="text-center"> | ||
<h2 className="text-3xl font-bold tracking-tight text-gray-900 dark:text-gray-100"> | ||
Re-authenticate to Frontpage | ||
</h2> | ||
<p className="mt-2 text-sm text-gray-600 dark:text-gray-400"> | ||
You need to re-authenticate to continue using Frontpage so that we | ||
have the latest permissions to access your data. | ||
</p> | ||
</div> | ||
<div> | ||
<ReauthenticateForm | ||
avatar={<UserAvatar did={session.user.did} size="smedium" />} | ||
/> | ||
<form | ||
action={async () => { | ||
"use server"; | ||
await signOut(); | ||
revalidatePath("/", "layout"); | ||
}} | ||
> | ||
<Button size="lg" variant="secondary" className="w-full mt-4"> | ||
Logout | ||
</Button> | ||
</form> | ||
</div> | ||
</> | ||
); | ||
} |
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 @@ | ||
ALTER TABLE `oauth_sessions` ADD `scope` text DEFAULT 'atproto transition:generic' NOT NULL; |
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.
If everything works correctly in this PR it should just be a matter of deploying the app with new scopes here. This can be done as a followup PR when implementing user email storage.