MastraAuthNeon and MastraRBACNeon
MastraAuthNeon connects Mastra authentication to Neon Auth. It authenticates JWT bearer tokens with Neon Auth's JWKS endpoint and falls back to validating Neon Auth session cookies through its session API. The provider also supports email-and-password sign-in, sign-up, and session management.
MastraRBACNeon maps Neon Auth organization roles to Mastra permissions. Register it separately when your application uses Neon Auth organization memberships for role-based access control.
UsageDirect link to Usage
import { Mastra } from '@mastra/core/mastra'
import { MastraAuthNeon, MastraRBACNeon } from '@mastra/auth-neon'
const auth = new MastraAuthNeon()
const rbac = new MastraRBACNeon({
roleMapping: {
admin: ['*'],
member: ['agents:read', 'workflows:read'],
_default: [],
},
})
export const mastra = new Mastra({
server: {
auth,
rbac,
},
})
Set NEON_AUTH_BASE_URL to your Neon Auth service URL. You can also pass baseUrl directly to both constructors.
MastraAuthNeonDirect link to mastraauthneon
Constructor parametersDirect link to Constructor parameters
The MastraAuthNeon constructor accepts an optional MastraAuthNeonOptions object.
baseUrl?:
jwksUrl?:
sessionCookieName?:
signUpEnabled?:
name?:
mapUserToResourceId?:
protected?:
public?:
Environment variablesDirect link to Environment variables
NEON_AUTH_BASE_URL:
NEON_AUTH_JWKS_URL:
Authentication methodsDirect link to Authentication methods
authenticateToken()Direct link to authenticatetoken
await auth.authenticateToken(token, request)
Verifies the token as a JWT with the configured JWKS endpoint. If JWT verification fails, the provider validates it as a Neon Auth session token through the session API. Returns the authenticated NeonAuthUser, or null when neither method succeeds.
authorizeUser()Direct link to authorizeuser
await auth.authorizeUser(user, request)
Runs the configured custom authorization function when one is provided. Otherwise, it requires a Neon Auth user ID and rejects expired JWT payloads.
Credentials methodsDirect link to Credentials methods
signIn()Direct link to signin
const result = await auth.signIn(email, password, request)
Authenticates email-and-password credentials through Neon Auth. Returns the authenticated user, optional token, and response cookies.
signUp()Direct link to signup
const result = await auth.signUp(email, password, name, request)
Creates a Neon Auth account using email-and-password credentials. name is optional; when omitted, the provider derives a display name from the email address. Returns the authenticated user, optional token, and response cookies.
isSignUpEnabled()Direct link to issignupenabled
const enabled = auth.isSignUpEnabled()
Returns the configured signUpEnabled value.
Session methodsDirect link to Session methods
createSession()Direct link to createsession
const session = await auth.createSession(userId, metadata)
Creates a normalized Mastra session with a generated ID and a seven-day expiration without creating a remote Neon Auth session.
validateSession()Direct link to validatesession
const session = await auth.validateSession(sessionId)
Validates a Neon Auth session token and returns a normalized Mastra session, or null when the session is invalid.
refreshSession()Direct link to refreshsession
const session = await auth.refreshSession(sessionId)
Validates the session through Neon Auth. Neon Auth refreshes sessions automatically when its configured update interval is reached.
destroySession()Direct link to destroysession
await auth.destroySession(sessionId)
Completes without a remote request. Neon Auth handles session destruction through its sign-out endpoint, while getClearSessionHeaders() returns the headers used to clear local session cookies.
MastraRBACNeonDirect link to mastrarbacneon
Constructor parametersDirect link to Constructor parameters
The MastraRBACNeon constructor accepts a MastraRBACNeonOptions object.
roleMapping:
baseUrl?:
organizationId?:
getUserRoles?:
cache?:
MethodsDirect link to Methods
getRoles()Direct link to getroles
const roles = await rbac.getRoles(user)
Returns roles from the configured getUserRoles function, the user's JWT role claim, or Neon Auth organization memberships. When organizationId is set, only memberships for that organization are considered.
hasRole()Direct link to hasrole
const allowed = await rbac.hasRole(user, 'admin')
Checks whether the user has the requested role.
getPermissions()Direct link to getpermissions
const permissions = await rbac.getPermissions(user)
Resolves the user's roles through roleMapping and returns Mastra permission patterns.
hasPermission()Direct link to haspermission
const allowed = await rbac.hasPermission(user, 'agents:read')
Checks whether the resolved permissions allow the requested permission.
hasAllPermissions() and hasAnyPermission()Direct link to hasallpermissions-and-hasanypermission
const canReadAndRun = await rbac.hasAllPermissions(user, ['agents:read', 'agents:execute'])
const canReadAnything = await rbac.hasAnyPermission(user, ['agents:read', 'workflows:read'])
Checks whether the user has all or at least one of the requested permissions.
getAvailableRoles()Direct link to getavailableroles
const roles = await rbac.getAvailableRoles()
Returns the configured role names except _default.
getRolePermissions()Direct link to getrolepermissions
const permissions = await rbac.getRolePermissions('member')
Resolves the permission patterns configured for a role.