This library is in early development. Expect breaking changes.
Integrations

i18n

Translate authentication errors with @nuxtjs/i18n.

This integration enables translated authentication error messages using your existing @nuxtjs/i18n setup.

Setup

Install @nuxtjs/i18n

pnpm add @nuxtjs/i18n

Configure nuxt.config.ts

nuxt.config.ts
export default defineNuxtConfig({
  modules: ['@nuxtjs/i18n', '@onmax/nuxt-better-auth'],
  i18n: {
    locales: ['en', 'es'],
    defaultLocale: 'en'
  }
})

Translation Files

Add error translations to your locale files using the auth.errors.* prefix:

auth:
  errors:
    USER_NOT_FOUND: "User not found"
    INVALID_PASSWORD: "Invalid password"
    INVALID_EMAIL_OR_PASSWORD: "Invalid email or password"
    EMAIL_NOT_VERIFIED: "Please verify your email first"
    SESSION_EXPIRED: "Session expired, please sign in again"

Client-Side Error Handling

Better Auth errors include a code property that you can translate using useI18n():

pages/login.vue
<script setup lang="ts">
const {
  execute: signInWithEmail,
  error,
} = useSignIn('email')
const { t, te } = useI18n()

async function handleLogin(email: string, password: string) {
  await signInWithEmail(
    { email, password },
    {
      onSuccess: () => navigateTo('/app'),
    }
  )
}

const localizedErrorMessage = computed(() => {
  const code = error.value?.code
  if (!code)
    return error.value?.message
  const key = `auth.errors.${code}`
  return te(key) ? t(key) : error.value?.message
})
</script>

<template>
  <div v-if="localizedErrorMessage" class="error">{{ localizedErrorMessage }}</div>
</template>

This pattern uses typed error.code for translation and falls back to error.message when no localized key exists.

Server-Side Locale Detection

For auth config callbacks like sendResetPassword, use @intlify/utils/h3 to detect the user's locale:

@intlify/utils is installed as a dependency of @nuxtjs/i18n, but you need to import it explicitly in server code.
server/auth.config.ts
import { tryCookieLocale, tryHeaderLocale } from '@intlify/utils/h3'
import { defineServerAuth } from '@onmax/nuxt-better-auth/config'

export default defineServerAuth(() => ({
  emailAndPassword: {
    enabled: true,
    sendResetPassword: async ({ user, url }, request) => {
      const locale = tryCookieLocale(request)?.toString()
        ?? tryHeaderLocale(request)?.toString()
        ?? 'en'

      // Use locale for email content...
    }
  }
}))

Error Codes Reference

CodeDefault Message
USER_NOT_FOUNDUser not found
INVALID_PASSWORDInvalid password
INVALID_EMAIL_OR_PASSWORDInvalid email or password
EMAIL_NOT_VERIFIEDEmail not verified
SESSION_EXPIREDSession expired
INVALID_TOKENInvalid token
TOKEN_EXPIREDToken expired
USER_ALREADY_EXISTSUser already exists
INVALID_EMAILInvalid email
PASSWORD_TOO_SHORTPassword too short
See Better Auth Error Codes for the complete list of error codes.
For comprehensive translations, see better-auth-localization plugin with 30+ languages.