Skip to content

Auth

import { noAuth, apiKeyAuth } from "@swenyai/providers/auth";
interface AuthProvider {
readonly displayName: string;
readonly loginFields?: LoginField[];
authenticate(userId: string): Promise<UserIdentity | null>;
login?(userId: string, credentials: Record<string, string>): Promise<UserIdentity>;
hasValidSession(userId: string): boolean;
clearSession(userId: string): void;
}
interface UserIdentity {
userId: string;
displayName: string;
email?: string;
roles: string[];
metadata: Record<string, unknown>;
}

Allows all users without authentication. Every user gets the admin role:

const auth = noAuth();

Useful for local development and single-user deployments.

Validates users against a custom function:

const auth = apiKeyAuth({
validate: async (apiKey) => {
// Look up the key in your database
const user = await db.findByApiKey(apiKey);
if (!user) return null;
return {
userId: user.id,
displayName: user.name,
email: user.email,
roles: user.roles,
metadata: {},
};
},
});

Sessions are stored in memory and lost on restart.