-
Notifications
You must be signed in to change notification settings - Fork 0
add multi auth guard #41
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
Changes from 1 commit
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,13 @@ | ||
| import { check, user } from '@holo-js/auth' | ||
| import auth, { check, provider, user } from '@holo-js/auth' | ||
|
|
||
| export async function GET(request: Request) { | ||
| const guard = new URL(request.url).searchParams.get('guard') ?? undefined | ||
| const guardAuth = guard ? auth.guard(guard) : undefined | ||
|
|
||
| export async function GET() { | ||
| return Response.json({ | ||
| authenticated: await check(), | ||
| guard: 'web', | ||
| user: await user(), | ||
| authenticated: guardAuth ? await guardAuth.check() : await check(), | ||
| guard: guard ?? 'web', | ||
| provider: guardAuth ? await guardAuth.provider() : await provider(), | ||
| user: guardAuth ? await guardAuth.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
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 auth from '@holo-js/auth' | ||
| import { 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 auth.guard('admin').login(submission.data) | ||
| if (error) { | ||
| const failure = submission.fail({ | ||
| status: error.status, | ||
| errors: error.fields, | ||
| }) | ||
|
|
||
| return Response.json(failure, { status: failure.status }) | ||
| } | ||
|
|
||
| return Response.json(submission.success({ | ||
| message: 'Signed in as super admin.', | ||
| redirectTo: '/super-admin', | ||
| user: session.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,13 @@ | ||
| import auth from '@holo-js/auth' | ||
|
|
||
| export async function POST() { | ||
| const admin = auth.guard('admin') | ||
| await admin.logout() | ||
|
|
||
| return Response.json({ | ||
| ok: true, | ||
| authenticated: false, | ||
| message: 'Signed out of super admin.', | ||
| user: await admin.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
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,93 @@ | ||
| 'use client' | ||
|
|
||
| import { useRouter } from 'next/navigation' | ||
| import { useAuth } from '@holo-js/auth/next/client' | ||
| import { useForm } from '@holo-js/adapter-next/client' | ||
| import { loginForm } 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 SuperAdminLoginPage() { | ||
| const router = useRouter() | ||
| const auth = useAuth({ guard: 'admin' }) | ||
| const form = useForm(loginForm, { | ||
| validateOn: 'blur', | ||
| initialValues: { email: '', password: '', remember: false }, | ||
| async submitter({ formData }) { | ||
| const response = await fetch('/api/super-admin/login', { method: 'POST', body: formData }) | ||
| const submission = await response.json() | ||
| if (submission?.ok === true && typeof submission.data?.redirectTo === 'string') { | ||
| try { | ||
| await auth.refreshUser() | ||
| } catch (error) { | ||
| console.warn('Admin auth refresh failed after login.', error) | ||
| } | ||
| router.replace(submission.data.redirectTo) | ||
| } | ||
| return submission | ||
| }, | ||
| }) | ||
|
|
||
| return ( | ||
| <section style={panelStyle}> | ||
| <div> | ||
| <h1 style={{ margin: '0 0 0.5rem 0' }}>Super Admin Sign In</h1> | ||
| <p style={{ margin: 0, color: '#94a3b8' }}>Use an admin account to access the super admin area.</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.values.email} | ||
| 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> | ||
|
|
||
| <label style={{ display: 'grid', gap: '0.35rem' }}> | ||
| <span>Password</span> | ||
| <input | ||
| name="password" | ||
| type="password" | ||
| value={form.values.password} | ||
| onInput={(event) => form.fields.password.onInput(event.currentTarget.value)} | ||
| onBlur={() => form.fields.password.onBlur()} | ||
| /> | ||
| {form.errors.has('password') ? <span style={{ color: '#fca5a5' }}>{form.errors.first('password')}</span> : null} | ||
| </label> | ||
|
|
||
| <label style={{ display: 'flex', gap: '0.5rem', alignItems: 'center' }}> | ||
| <input | ||
| name="remember" | ||
| type="checkbox" | ||
| checked={form.values.remember} | ||
| onChange={(event) => form.fields.remember.onInput(event.currentTarget.checked)} | ||
| /> | ||
| Remember me | ||
| </label> | ||
|
|
||
| <button type="submit" disabled={form.submitting}> | ||
| {form.submitting ? 'Signing in...' : 'Sign in as super admin'} | ||
| </button> | ||
| </form> | ||
|
|
||
| {form.lastSubmission?.ok === true ? ( | ||
| <div style={{ color: '#86efac' }}> | ||
| <p style={{ margin: 0 }}>Signed in as super admin.</p> | ||
| </div> | ||
| ) : null} | ||
| </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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| 'use client' | ||
|
|
||
| import { useState } from 'react' | ||
| import { useRouter } from 'next/navigation' | ||
| import { useAuth } from '@holo-js/auth/next/client' | ||
|
|
||
| export function SuperAdminLogoutButton() { | ||
| const router = useRouter() | ||
| const auth = useAuth({ guard: 'admin' }) | ||
| const [isLoggingOut, setIsLoggingOut] = useState(false) | ||
|
|
||
| async function logout() { | ||
| if (isLoggingOut) { | ||
| return | ||
| } | ||
|
|
||
| setIsLoggingOut(true) | ||
| try { | ||
| const response = await fetch('/api/super-admin/logout', { method: 'POST' }) | ||
| if (!response.ok) { | ||
| console.warn('Super admin logout failed.', { status: response.status }) | ||
| return | ||
| } | ||
|
|
||
| await auth.refreshUser() | ||
| router.replace('/super-admin/login') | ||
| } catch (error) { | ||
| console.warn('Super admin logout failed.', error) | ||
| } finally { | ||
| setIsLoggingOut(false) | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <button type="button" onClick={logout} disabled={isLoggingOut}> | ||
| {isLoggingOut ? 'Signing out...' : 'Sign out of super admin'} | ||
| </button> | ||
| ) | ||
| } |
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,24 @@ | ||
| import { redirect } from 'next/navigation' | ||
| import { auth } from '@holo-js/auth/next/server' | ||
| import { SuperAdminLogoutButton } from './logout-button' | ||
|
|
||
| export default async function SuperAdminPage() { | ||
| const currentAuth = await auth({ guard: 'admin' }) | ||
|
|
||
| if (!currentAuth.authenticated) { | ||
| redirect('/super-admin/login') | ||
| } | ||
|
|
||
| const displayName = currentAuth.user?.name ?? currentAuth.user?.email ?? 'Super Admin' | ||
|
|
||
| return ( | ||
| <section style={{ display: 'grid', gap: '0.75rem', maxWidth: '42rem' }}> | ||
| <p style={{ margin: 0, color: '#7dd3fc', fontSize: '0.875rem', textTransform: 'uppercase' }}>Admin guard</p> | ||
| <h1 style={{ margin: 0 }}>Super Admin</h1> | ||
| <p style={{ margin: 0, color: '#cbd5e1' }}>Signed in as {displayName} through the admin guard.</p> | ||
| <div> | ||
| <SuperAdminLogoutButton /> | ||
| </div> | ||
| </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
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
18 changes: 18 additions & 0 deletions
18
apps/blog-next/server/db/migrations/2026_04_25_044031_create_admins.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,18 @@ | ||
| import { defineMigration, type MigrationContext } from '@holo-js/db' | ||
|
|
||
| export default defineMigration({ | ||
| async up({ schema }: MigrationContext) { | ||
| await schema.createTable('admins', (table) => { | ||
| table.id() | ||
| table.string('name') | ||
| table.string('email').unique() | ||
| table.string('password').nullable() | ||
| table.string('avatar').nullable() | ||
| table.timestamp('email_verified_at').nullable() | ||
| table.timestamps() | ||
| }) | ||
| }, | ||
| async down({ schema }: MigrationContext) { | ||
| await schema.dropTable('admins') | ||
| }, | ||
| }) |
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.
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.
Avoid hardcoded seeded passwords for privileged accounts.
Using fixed credentials (
'secret'/'admin-secret') creates a predictable login path if this seed data reaches non-local environments. Please source these from environment variables (or fail when missing outside local/dev).Suggested hardening
📝 Committable suggestion
🤖 Prompt for AI Agents