-
Notifications
You must be signed in to change notification settings - Fork 0
update auth api #19
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
Merged
Merged
update auth api #19
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 { check, user } from '@holo-js/auth' | ||
|
|
||
| export async function GET() { | ||
| return Response.json({ | ||
| authenticated: await check(), | ||
| guard: 'web', | ||
| user: await user(), | ||
| }) | ||
| } |
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,31 @@ | ||
| import { requestPasswordReset } from '@holo-js/auth' | ||
| import { sanitizeFlashedInput, validate } from '@holo-js/forms' | ||
|
|
||
| import { forgotPasswordForm } from '@/lib/schemas/auth' | ||
|
|
||
| export async function POST(request: Request) { | ||
| const submission = await validate(request, forgotPasswordForm) | ||
|
|
||
| if (!submission.valid) { | ||
| return Response.json(submission.fail(), { | ||
| status: submission.fail().status, | ||
| }) | ||
| } | ||
|
|
||
| const { error } = await requestPasswordReset(submission.data) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| valid: false as const, | ||
| values: sanitizeFlashedInput(submission.values), | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: 'If an account exists for that email, a reset link has been sent.', | ||
| })) | ||
| } |
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,46 @@ | ||
| import { login } from '@holo-js/auth' | ||
| import { sanitizeFlashedInput, validate } from '@holo-js/forms' | ||
|
|
||
| import { loginForm } from '@/lib/schemas/auth' | ||
|
|
||
| export async function POST(request: Request) { | ||
| const submission = await validate(request, loginForm, { | ||
| throttle: 'login', | ||
| }) | ||
|
|
||
| if (!submission.valid) { | ||
| return Response.json(submission.fail(), { | ||
| status: submission.fail().status, | ||
| }) | ||
| } | ||
|
|
||
| const { data: session, error } = await login(submission.data) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| valid: false as const, | ||
| values: sanitizeFlashedInput(submission.values), | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| const headers = new Headers() | ||
| for (const cookie of session.cookies) { | ||
| headers.append('set-cookie', cookie) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: session.emailVerificationRequired | ||
| ? 'Signed in. Verify your email address to continue.' | ||
| : 'Signed in successfully.', | ||
| redirectTo: session.emailVerificationRequired | ||
| ? session.emailVerificationRoute ?? '/verify-email' | ||
| : '/admin', | ||
| user: session.user, | ||
| }), { | ||
| headers, | ||
| }) | ||
| } |
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,18 @@ | ||
| import { logout, user } from '@holo-js/auth' | ||
|
|
||
| export async function POST() { | ||
| const signedOut = await logout() | ||
| const headers = new Headers() | ||
| for (const cookie of signedOut.cookies) { | ||
| headers.append('set-cookie', cookie) | ||
| } | ||
|
|
||
| return Response.json({ | ||
| ok: true, | ||
| authenticated: false, | ||
| message: 'Signed out successfully.', | ||
| user: await user(), | ||
| }, { | ||
| headers, | ||
| }) | ||
| } |
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,48 @@ | ||
| import { loginUsing, register } from '@holo-js/auth' | ||
| import { sanitizeFlashedInput, validate } from '@holo-js/forms' | ||
|
|
||
| import { registerForm } from '@/lib/schemas/auth' | ||
|
|
||
| export async function POST(request: Request) { | ||
| const submission = await validate(request, registerForm, { | ||
| throttle: 'register', | ||
| }) | ||
|
|
||
| if (!submission.valid) { | ||
| return Response.json(submission.fail(), { | ||
| status: submission.fail().status, | ||
| }) | ||
| } | ||
|
|
||
| const { data: created, error } = await register(submission.data) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| valid: false as const, | ||
| values: sanitizeFlashedInput(submission.values), | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| const session = await loginUsing(created) | ||
| const headers = new Headers() | ||
| for (const cookie of session.cookies) { | ||
| headers.append('set-cookie', cookie) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: session.emailVerificationRequired | ||
| ? 'Account created. Check your inbox to verify your email address.' | ||
| : 'Account created and signed in successfully.', | ||
| redirectTo: session.emailVerificationRequired | ||
| ? session.emailVerificationRoute ?? '/verify-email' | ||
| : '/admin', | ||
| user: session.user, | ||
| }, 201), { | ||
| status: 201, | ||
| headers, | ||
| }) | ||
| } | ||
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,32 @@ | ||
| import { resetPassword } from '@holo-js/auth' | ||
| import { sanitizeFlashedInput, validate } from '@holo-js/forms' | ||
|
|
||
| import { resetPasswordForm } from '@/lib/schemas/auth' | ||
|
|
||
| export async function POST(request: Request) { | ||
| const submission = await validate(request, resetPasswordForm) | ||
|
|
||
| if (!submission.valid) { | ||
| return Response.json(submission.fail(), { | ||
| status: submission.fail().status, | ||
| }) | ||
| } | ||
|
|
||
| const { error } = await resetPassword(submission.data) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| valid: false as const, | ||
| values: sanitizeFlashedInput(submission.values), | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: 'Password reset successfully. You can sign in with your new password.', | ||
| redirectTo: '/login', | ||
| })) | ||
| } |
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,42 @@ | ||
| import { verification } from '@holo-js/auth' | ||
|
|
||
| interface ResendVerificationRequestBody { | ||
| readonly email?: string | ||
| } | ||
|
|
||
| async function readRequestBody(request: Request): Promise<ResendVerificationRequestBody> { | ||
| const contentType = request.headers.get('content-type') ?? '' | ||
| if (!contentType.includes('application/json')) { | ||
| return {} | ||
| } | ||
|
|
||
| const payload = await request.json().catch(() => null) | ||
| if (!payload || typeof payload !== 'object') { | ||
| return {} | ||
| } | ||
|
|
||
| const email = typeof payload.email === 'string' ? payload.email.trim() : undefined | ||
| return email ? { email } : {} | ||
| } | ||
|
|
||
| export async function POST(request: Request) { | ||
| const input = await readRequestBody(request) | ||
| const { error } = await verification.resend(input) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| return Response.json({ | ||
| ok: true as const, | ||
| status: 200, | ||
| data: { | ||
| message: 'A fresh verification email has been sent.', | ||
| }, | ||
| }) | ||
| } |
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,32 @@ | ||
| import { verification } from '@holo-js/auth' | ||
| import { sanitizeFlashedInput, validate } from '@holo-js/forms' | ||
|
|
||
| import { verifyEmailForm } from '@/lib/schemas/auth' | ||
|
|
||
| export async function POST(request: Request) { | ||
| const submission = await validate(request, verifyEmailForm) | ||
|
|
||
| if (!submission.valid) { | ||
| return Response.json(submission.fail(), { | ||
| status: submission.fail().status, | ||
| }) | ||
| } | ||
|
|
||
| const { error } = await verification.consume(submission.data.token) | ||
| if (error) { | ||
| return Response.json({ | ||
| ok: false as const, | ||
| status: error.status, | ||
| valid: false as const, | ||
| values: sanitizeFlashedInput(submission.values), | ||
| errors: error.fields, | ||
| }, { | ||
| status: error.status, | ||
| }) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: 'Email address verified. You can sign in now.', | ||
| redirectTo: '/login', | ||
| })) | ||
| } |
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,59 @@ | ||
| 'use client' | ||
|
|
||
| import Link from 'next/link' | ||
|
|
||
| import { useForm } from '@holo-js/adapter-next/client' | ||
|
|
||
| import { forgotPasswordForm } from '@/lib/schemas/auth' | ||
|
|
||
| const panelStyle = { | ||
| display: 'grid', | ||
| gap: '1rem', | ||
| maxWidth: '32rem', | ||
| padding: '1.5rem', | ||
| borderRadius: '1rem', | ||
| background: '#111827', | ||
| border: '1px solid rgba(148, 163, 184, 0.16)', | ||
| } satisfies React.CSSProperties | ||
|
|
||
| export default function ForgotPasswordPage() { | ||
| const form = useForm(forgotPasswordForm, { | ||
| validateOn: 'blur', | ||
| initialValues: { email: '' }, | ||
| async submitter({ formData }) { | ||
| const response = await fetch('/api/forgot-password', { method: 'POST', body: formData }) | ||
| return await response.json() | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <section style={panelStyle}> | ||
| <div> | ||
| <h1 style={{ margin: '0 0 0.5rem 0' }}>Forgot password</h1> | ||
| <p style={{ margin: 0, color: '#94a3b8' }}>Request a password reset link for your local account.</p> | ||
| </div> | ||
|
|
||
| <form onSubmit={(event) => { event.preventDefault(); form.submit() }} style={{ display: 'grid', gap: '0.9rem' }}> | ||
| <label style={{ display: 'grid', gap: '0.35rem' }}> | ||
| <span>Email</span> | ||
| <input | ||
| name="email" | ||
| type="email" | ||
| value={form.fields.email.value} | ||
| onInput={(event) => form.fields.email.onInput(event.currentTarget.value)} | ||
| onBlur={() => form.fields.email.onBlur()} | ||
| /> | ||
| {form.errors.has('email') ? <span style={{ color: '#fca5a5' }}>{form.errors.first('email')}</span> : null} | ||
| </label> | ||
|
|
||
| <button type="submit" disabled={form.submitting}> | ||
| {form.submitting ? 'Sending link...' : 'Send reset link'} | ||
| </button> | ||
| </form> | ||
|
|
||
| {form.lastSubmission?.ok === true ? <p style={{ margin: 0, color: '#86efac' }}>A password reset link has been sent if the account exists.</p> : null} | ||
|
|
||
| <Link href="/login" style={{ color: '#7dd3fc' }}>Back to sign in</Link> | ||
| </section> | ||
| ) | ||
| } |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.