mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 12:54:20 +01:00
Add single-user authentication system with Better Auth integration (#76)
* Add Better Auth integration with session management - Implement user authentication using Better Auth library - Add login, register, and sign out functionality - Support passkey/WebAuthn authentication - Create database schema for users, sessions, accounts, passkeys - Add AuthProvider context and ProtectedRoute components - Add account settings page in settings dialog - Protect all API routes with authentication middleware - Update environment configuration for auth providers * Add single-user authentication system with admin setup - Add first-run Setup page for initial admin account creation - Add environment-based admin configuration (ADMIN_EMAIL, ADMIN_PASSWORD) - Add password reset capability via ADMIN_PASSWORD_RESET environment flag - Migrate to data routers with auth loaders, remove ProtectedRoute - Add change password functionality to account settings - Enforce AUTH_SECRET requirement in production - Auto-disable registration when admin is pre-configured via environment * Track busy/idle session status across hooks * Bump version to 0.7.5 * Update pnpm to 10.28.1
This commit is contained in:
1 parent
af292758c3
commit
81a66d2ddb
29 files changed
+2324
-149
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import { createMiddleware } from 'hono/factory'
|
||||
import type { AuthInstance, Session } from './index'
|
||||
|
||||
export function createAuthMiddleware(auth: AuthInstance) {
|
||||
return createMiddleware<{
|
||||
Variables: {
|
||||
session: Session['session']
|
||||
user: Session['user']
|
||||
}
|
||||
}>(async (c, next) => {
|
||||
const session = await auth.api.getSession({
|
||||
headers: c.req.raw.headers,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
return c.json({ error: 'Unauthorized' }, 401)
|
||||
}
|
||||
|
||||
c.set('session', session.session as Session['session'])
|
||||
c.set('user', session.user as Session['user'])
|
||||
await next()
|
||||
})
|
||||
}
|
||||
|
||||
export function createOptionalAuthMiddleware(auth: AuthInstance) {
|
||||
return createMiddleware<{
|
||||
Variables: {
|
||||
session: Session['session'] | null
|
||||
user: Session['user'] | null
|
||||
}
|
||||
}>(async (c, next) => {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: c.req.raw.headers,
|
||||
})
|
||||
|
||||
if (session) {
|
||||
c.set('session', session.session as Session['session'])
|
||||
c.set('user', session.user as Session['user'])
|
||||
} else {
|
||||
c.set('session', null)
|
||||
c.set('user', null)
|
||||
}
|
||||
} catch {
|
||||
c.set('session', null)
|
||||
c.set('user', null)
|
||||
}
|
||||
|
||||
await next()
|
||||
})
|
||||
}
|
||||
|
||||
export function createAdminMiddleware(auth: AuthInstance) {
|
||||
return createMiddleware<{
|
||||
Variables: {
|
||||
session: Session['session']
|
||||
user: Session['user']
|
||||
}
|
||||
}>(async (c, next) => {
|
||||
const session = await auth.api.getSession({
|
||||
headers: c.req.raw.headers,
|
||||
})
|
||||
|
||||
if (!session) {
|
||||
return c.json({ error: 'Unauthorized' }, 401)
|
||||
}
|
||||
|
||||
const user = session.user as Session['user']
|
||||
if (user.role !== 'admin') {
|
||||
return c.json({ error: 'Forbidden: Admin access required' }, 403)
|
||||
}
|
||||
|
||||
c.set('session', session.session as Session['session'])
|
||||
c.set('user', user)
|
||||
await next()
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user