GitHub 37
Skip to content
ViteHub is still experimental. Expect bugs and breaking changes.

Auth

Add sign-in and user sessions to your app with Better Auth, set up from one server file.

server/auth.ts
import { defineAuth } from 'vite-hub/auth'

// Sign-up, sign-in, and session routes
// appear under /api/auth.
export default defineAuth({
  appName: 'Acme',
  emailAndPassword: { enabled: true },
  access: {
    // Requests to these APIs need a session
    routes: ['/api/admin/**'],
  },
})
server/auth.ts

One file sets up sign-in

Add server/auth.ts to get sign-up, sign-in, and session routes under /api/auth.

Protect a route

Wrap a handler in withAuth() so only signed-in users can call it.

Protect a Nuxt page

Set definePageMeta({ auth: 'user' }) to send signed-out visitors to the login page.

Decide who gets in

Check roles in authorize and return false to reject with 403.

Session state in Vue

useUserSession() gives you the user, loggedIn, and sign-in and sign-out helpers.

Keep secrets out of your code

Read your auth secret and OAuth keys from server environment variables at request time.

Add a database for production

authDatabase() adds the Better Auth tables to your app Database.