Authentication
Lucent provides a complete authentication system with JWT support.
Setup
Enable authentication in your configuration:
typescript
// lucent.config.ts
export default defineConfig({
auth: {
jwtSecret: process.env.JWT_SECRET,
jwtExpiry: "7d",
},
collections: {
users: defineCollection({
/* ... */
}),
},
});User Collection
Your users collection must include specific fields:
typescript
export const users = defineCollection({
name: "users",
fields: {
email: { type: "email", required: true, unique: true },
password: { type: "string", required: true, hidden: true },
name: { type: "string" },
role: { type: "select", options: ["admin", "user"], default: "user" },
// Auth fields
verified: { type: "boolean", default: false },
verificationToken: { type: "string", hidden: true },
resetToken: { type: "string", hidden: true },
resetExpiry: { type: "datetime", hidden: true },
},
});Authentication Methods
Email/Password
typescript
// Register
const user = await lucent.auth.register({
email: "user@example.com",
password: "securepassword123",
name: "John Doe",
});
// Login
const { user, token } = await lucent.auth.login({
email: "user@example.com",
password: "securepassword123",
});
// Logout
await lucent.auth.logout(token);Using the Token
Include the token in API requests:
typescript
fetch("/api/posts", {
headers: {
Authorization: `Bearer ${token}`,
},
});Password Requirements
typescript
auth: {
password: {
minLength: 8,
requireUppercase: true,
requireNumbers: true,
requireSymbols: true
}
}Email Verification
typescript
// Send verification email
await lucent.auth.sendVerificationEmail(userId);
// Verify email
await lucent.auth.verifyEmail(token);Password Reset
typescript
// Request password reset
await lucent.auth.requestPasswordReset(email);
// Reset password
await lucent.auth.resetPassword(token, newPassword);