Auth resource mode
Resource mode is for an app that has its own server, database and frigear server modules, but lets an
issuer handle sign-in. People sign in at the issuer in the browser, exactly as in
client mode: PKCE, the Capacitor flow and the Tauri system browser with a deep
link. The app's server then accepts the issuer's access tokens as bearer tokens, through
OAuth Resource. Routes use authorize(event, permission) as in server mode, users
are created locally the first time they call, and server modules that need Auth and Users work.
Use it for a desktop app that bundles its own server (Desktop local mode), or for any single-page app (SPA) whose API is its own server. Keep server mode when the app is where people sign in. Use client mode when the app has no server at all.
@frigear-nu/module-kit and @frigear-nu/notify. Not
nuxt-auth-utils.You add:@nuxthub/core and @frigear-nu/oauth-resource, which installs
Users.Optional:@nuxt/ui (the sign-in and callback pages), Authorization (roles and
page guards) and the Capacitor and Tauri plugins of client mode.Quick setup
Install the modules
pnpm add @frigear-nu/auth @frigear-nu/oauth-resource @frigear-nu/authorization @nuxthub/core
Configure resource mode
Set the mode, the issuer and your app's client id under authentication, as in client mode. Point OAuth Resource at
the same issuer, with the audience the issuer puts in the tokens it issues to your app.
export default defineNuxtConfig({
modules: ['@nuxt/ui', '@nuxthub/core', '@frigear-nu/oauth-resource', '@frigear-nu/authorization'],
authentication: {
mode: 'resource',
client: {
issuer: 'https://frigear.nu',
clientId: 'my-desk',
scopes: ['openid', 'profile', 'email', 'offline_access'], // must contain 'openid' and 'email'
native: { scheme: 'nu.frigear.desk' }, // Tauri or Capacitor: com.example.app:/auth/callback
},
},
oauthResource: {
issuer: 'https://frigear.nu',
audience: 'frigear-services',
},
routeRules: {
'/**': { authenticated: true },
},
})
Register the app at the issuer
Register a public application with the redirect URIs of client mode.
Its access tokens must be at+jwt with your oauthResource.audience in aud. At
OAuth Server that is accessTokenAudience.
Generate the migration
Users and Auth's columns on users are in your schema. Generate and commit the migration:
pnpm db:generate
/login. After you sign in at the issuer you come back, and
useFetch('/api/...') reaches your server signed in. The first request creates your user in the local users table.What it includes
| Resource mode | |
|---|---|
| Sign-in | At the issuer, in the browser or the system browser: client mode's PKCE client, pages, useAuth(), useUserSession() and $api. |
| Server rendering | Off (ssr: false), as in client mode: the tokens live in the browser. |
| Credentials | Bearer access tokens only, through a resolver. No cookie sessions, no API tokens. |
| Access control | Server mode's: the registry, authorize / allows / requireAuth, roles, policies, grants and authorization:before. |
| Users | The user store of Users. Users are created from the issuer's profile on their first request. |
| Routes of Auth | GET /api/auth/me/access only. |
| Tables of Auth | None. The credential columns on users stay, so the users admin reads the same table in every mode. |
Not included: nuxt-auth-utils and its /api/_auth/session, the sign-in, sign-up, sign-out, password, emailed link,
passkey and OAuth provider routes, API tokens (/api/auth/me/tokens, /api/auth/scopes, the auth_user_tokens
table) and the account menu entries for them.
The mode
Resource mode checks the app when it builds:
| Check | What happens | Why |
|---|---|---|
| A resolver is registered | Without one the build fails. nuxi prepare only warns. | Nobody could ever be authenticated: every guarded route would answer 401. |
| No nuxt-auth-utils | Installing it fails the build, naming the module that installed it. | It would start cookie sessions next to the issuer's sign-in. |
| Sign-in options | apiTokens, tokenTtlDays, settings.webAuthn, magicLink, verifyEmail, oauth, session.maxAge and pages.register print a warning. | They do nothing here. |
| Client mode's checks | ssr: true and scopes without openid fail the build. | As in client mode. |
resolveAuthMode(nuxt) from @frigear-nu/auth/mode returns 'resource'. authModeHasServer(mode) is true for server
and resource mode, and authModeSignsInAtIssuer(mode) for client and resource mode, for modules that branch on the
mode.
Calling the server
The app's own origin is an API too. It is read in the browser (location.origin), so a port picked when the app starts
works, as in Desktop local mode.
- Every request to the app's own origin carries the access token:
$fetch,useFetchand the pages of other modules, as a cookie would. Requests to other origins, and requests that set their ownauthorizationheader, are left alone. $apianduseApidefault to the app's own origin.client.apiBasestill sets another base, andapiOriginsother origins. The app's own origin always gets the token.useAuthorization()readsaccessfrom your server'sGET /api/auth/me/access: your registry and your roles. It never falls back to the token'spermissionsclaim, which holds the issuer's permissions.
runtimeConfig.public.authentication.client.sameOrigin is true in this mode.
What a token may do
OAuth Resource decides it:
- Your app's own sign-in. A token whose
client_idisauthentication.client.clientIdacts with the user's roles in this app, like a session. Assign roles here with Authorization. - Any other client. A token issued to another application stays foreign: the issuer's
permissionsclaim, or its scopes against your permissions.
Session-only permissions are never granted. Every caller is a bearer token, so a sessionOnly permission answers
403 session_required, before roles, authorization:before and policies run. requireAuth(event, { sessionOnly: true })
answers the same. GET /api/auth/me/access never lists them. In development the build lists the session-only
permissions of your app.
That affects module routes too, which answer 403 session_required: Authorization's roles:assign (give roles with
authorization.roles, defaultRoles or the authorization:roles hook instead), Users' users:delete and
users:manage, and PATCH /api/preferences of Preferences.
Sessions in server code
getAuth(event) never reads a cookie session in this mode: without a bearer token the caller is anonymous. Server code
written for server mode still runs: nuxt-auth-utils' getUserSession answers an empty session, clearUserSession does
nothing and requireUserSession answers 401. setUserSession and replaceUserSession throw, with a message that
names this mode. Use getAuth and authorize instead.
Security
- The tokens live in the browser, as in client mode. Any cross-site scripting (XSS) can read them. A Tauri store is not encrypted.
- Requests to the app's own origin carry the token, so keep your API on routes you control. A token never goes to
another origin unless it is in
apiOrigins. - Without introspection a token stays valid until it expires. Keep the issuer's access token lifetime short.
Next steps
- OAuth Resource – how the token is checked and users are created.
- Client mode – the sign-in options, Tauri and Capacitor.
- Desktop local mode – a desktop app with its own server.