nuxt-users
Version:
A comprehensive user management module for Nuxt 3 and Nuxt 4 applications with authentication, authorization, database support, and CLI tools
273 lines (271 loc) • 7.31 kB
text/typescript
type DatabaseType = 'sqlite' | 'mysql' | 'postgresql';
type HttpMethod = 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
type Permission = string | {
path: string;
methods: HttpMethod[];
};
type DatabaseConfig = {
path?: string;
host?: string;
port?: number;
user?: string;
password?: string;
database?: string;
};
interface GoogleOAuthOptions {
/**
* Google OAuth client ID from Google Cloud Console
*/
clientId: string;
/**
* Google OAuth client secret from Google Cloud Console
*/
clientSecret: string;
/**
* Callback URL for Google OAuth (must match what's configured in Google Cloud Console)
* @default '/api/nuxt-users/auth/google/callback'
*/
callbackUrl?: string;
/**
* Redirect URL after successful authentication
* @default '/'
*/
successRedirect?: string;
/**
* Redirect URL after failed authentication
* @default '/login?error=oauth_failed'
*/
errorRedirect?: string;
/**
* Google OAuth scopes to request
* @default ['openid', 'profile', 'email']
*/
scopes?: string[];
/**
* Allow automatic user registration when logging in with Google for the first time
* If false, only existing users with matching email can log in with Google
* @default false
*/
allowAutoRegistration?: boolean;
}
interface RuntimeModuleOptions {
connector?: {
name: DatabaseType;
options: DatabaseConfig;
};
/**
* Base path for all module API endpoints
* @default '/api/nuxt-users'
*/
apiBasePath?: string;
tables?: {
migrations?: string;
users?: string;
personalAccessTokens?: string;
passwordResetTokens?: string;
};
/**
* Mailer configuration options for sending emails (e.g., password resets)
* Uses nodemailer
*/
mailer?: MailerOptions;
/**
* URL path for password reset page
* @default '/reset-password'
*/
passwordResetUrl?: string;
/**
* URL to redirect to after email confirmation (success or failure)
* Query parameters will be added: ?status=success|error&message=...
* @default '/login'
*/
emailConfirmationUrl?: string;
/**
* Skip database checks during module setup to prevent hanging
* @default false
*/
auth?: {
/**
* Whitelisted routes that do not require authentication
* @default ['/login']
* @example ['/register']
*/
whitelist?: string[];
/**
* Token expiration time in minutes
* @default 1440
*/
tokenExpiration?: number;
/**
* Remember me token expiration time in days
* @default 30
*/
rememberMeExpiration?: number;
/**
* Role-based permissions configuration
* @default {}
* @example {
* admin: ['*'], // admin can access everything
* user: ['/profile', '/api/nuxt-users/me'],
* moderator: ['/admin/*', '/api/admin/*']
* }
*/
permissions?: Record<string, (string | Permission)[]>;
/**
* Google OAuth configuration
* Enable Google OAuth login/registration
*/
google?: GoogleOAuthOptions;
};
/**
* Password validation configuration
*/
passwordValidation?: {
/**
* Minimum password length
* @default 8
*/
minLength?: number;
/**
* Require uppercase letters
* @default true
*/
requireUppercase?: boolean;
/**
* Require lowercase letters
* @default true
*/
requireLowercase?: boolean;
/**
* Require numbers
* @default true
*/
requireNumbers?: boolean;
/**
* Require special characters
* @default true
*/
requireSpecialChars?: boolean;
/**
* Prevent common passwords
* @default true
*/
preventCommonPasswords?: boolean;
};
/**
* Enable hard delete for user deletion
* When false (default), users are soft deleted (active set to false)
* When true, users are permanently deleted from database
* @default false
*/
hardDelete?: boolean;
}
interface ModuleOptions {
connector: {
name: DatabaseType;
options: DatabaseConfig;
};
apiBasePath: string;
tables: {
migrations: string;
users: string;
personalAccessTokens: string;
passwordResetTokens: string;
};
mailer?: MailerOptions;
passwordResetUrl: string;
emailConfirmationUrl: string;
auth: {
whitelist: string[];
tokenExpiration: number;
rememberMeExpiration: number;
permissions: Record<string, (string | Permission)[]>;
google?: GoogleOAuthOptions;
};
passwordValidation: {
minLength: number;
requireUppercase: boolean;
requireLowercase: boolean;
requireNumbers: boolean;
requireSpecialChars: boolean;
preventCommonPasswords: boolean;
};
hardDelete: boolean;
}
interface MailerOptions {
host: string;
port: number;
secure?: boolean;
auth: {
user: string;
pass: string;
};
defaults?: {
from: string;
};
}
interface User {
id: number;
email: string;
name: string;
password: string;
role: string;
active: boolean;
google_id?: string;
profile_picture?: string;
created_at: string;
updated_at: string;
last_login_at?: string;
}
type UserWithoutPassword = Omit<User, 'password'>;
interface PersonalAccessToken {
id: number;
tokenable_type: string;
tokenable_id: number;
name: string;
token: string;
abilities?: string;
last_used_at?: string;
expires_at?: string;
created_at: string;
updated_at: string;
}
interface PasswordResetToken {
id: number;
email: string;
token: string;
created_at: string;
}
interface LoginFormData {
email: string;
password: string;
rememberMe?: boolean;
}
interface LoginFormProps {
apiEndpoint?: string;
redirectTo?: string;
forgotPasswordEndpoint?: string;
}
interface ResetPasswordFormProps {
apiEndpoint?: string;
updatePasswordEndpoint?: string;
resetPasswordEndpoint?: string;
redirectTo?: string;
}
interface DisplayFieldsProps {
displayFields?: string[];
fieldLabels?: Record<string, string>;
}
declare const defaultDisplayFields: string[];
declare const defaultFieldLabels: {
id: string;
name: string;
email: string;
role: string;
active: string;
created_at: string;
updated_at: string;
last_login_at: string;
};
export { defaultDisplayFields as h, defaultFieldLabels as i };
export type { DisplayFieldsProps as D, GoogleOAuthOptions as G, LoginFormData as L, ModuleOptions as M, PersonalAccessToken as P, RuntimeModuleOptions as R, User as U, UserWithoutPassword as a, PasswordResetToken as b, LoginFormProps as c, ResetPasswordFormProps as d, Permission as e, DatabaseType as f, DatabaseConfig as g };