2026-05-22 15:53:48 +00:00
|
|
|
import { Elysia } from 'elysia';
|
|
|
|
|
import type { AuthRequest, RegisterRequest } from '@zeavis/shared';
|
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
|
import { createDbClient } from '../db/client';
|
|
|
|
|
import { users } from '../db/schema';
|
|
|
|
|
import { badRequest, serviceUnavailable } from '../lib/http-errors';
|
|
|
|
|
import {
|
|
|
|
|
clearSessionCookie,
|
|
|
|
|
createSession,
|
|
|
|
|
createSessionCookie,
|
|
|
|
|
deleteSession,
|
|
|
|
|
getAuthFeatures,
|
|
|
|
|
getCurrentUser,
|
|
|
|
|
hashPassword,
|
|
|
|
|
readSessionToken,
|
|
|
|
|
verifyPassword,
|
|
|
|
|
} from '../lib/auth';
|
|
|
|
|
import { env } from '../config/env';
|
2026-06-07 18:01:26 +07:00
|
|
|
import { authCounter } from '../lib/telemetry';
|
2026-05-22 15:53:48 +00:00
|
|
|
|
|
|
|
|
function normalizeEmail(email: unknown) {
|
|
|
|
|
return typeof email === 'string' ? email.trim().toLowerCase() : '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function normalizeName(name: unknown) {
|
|
|
|
|
return typeof name === 'string' ? name.trim() : '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validatePassword(password: unknown) {
|
|
|
|
|
return typeof password === 'string' && password.length >= 8;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const authRoutes = new Elysia({ prefix: '/api/v1/auth' })
|
|
|
|
|
.get('/me', async ({ request }) => {
|
|
|
|
|
const user = await getCurrentUser(request.headers.get('cookie'));
|
|
|
|
|
return {
|
|
|
|
|
user,
|
|
|
|
|
features: getAuthFeatures(),
|
|
|
|
|
};
|
|
|
|
|
})
|
2026-06-15 18:17:26 +07:00
|
|
|
.post('/register', async ({ body, set, request }) => {
|
2026-05-22 15:53:48 +00:00
|
|
|
const req = body as Partial<RegisterRequest> | undefined;
|
|
|
|
|
const email = normalizeEmail(req?.email);
|
|
|
|
|
const name = normalizeName(req?.name);
|
|
|
|
|
|
|
|
|
|
if (!email || !email.includes('@') || !name || !validatePassword(req?.password)) {
|
|
|
|
|
return badRequest('Name, valid email, and password with at least 8 characters are required');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const db = createDbClient();
|
|
|
|
|
const existing = await db.select().from(users).where(eq(users.email, email)).limit(1);
|
|
|
|
|
if (existing.length > 0) {
|
|
|
|
|
return badRequest('Email is already registered');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const passwordHash = await hashPassword(req!.password!);
|
|
|
|
|
const inserted = await db
|
|
|
|
|
.insert(users)
|
|
|
|
|
.values({ email, name, passwordHash, role: 'user' })
|
|
|
|
|
.returning();
|
|
|
|
|
|
|
|
|
|
const user = inserted[0];
|
|
|
|
|
const token = await createSession(user.id);
|
2026-06-15 18:17:26 +07:00
|
|
|
set.headers['Set-Cookie'] = createSessionCookie(token, request.headers);
|
2026-05-22 15:53:48 +00:00
|
|
|
|
2026-06-07 18:01:26 +07:00
|
|
|
authCounter.labels('register', 'true').inc();
|
|
|
|
|
|
2026-05-22 15:53:48 +00:00
|
|
|
return {
|
|
|
|
|
user: {
|
|
|
|
|
id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
name: user.name,
|
|
|
|
|
role: 'user' as const,
|
|
|
|
|
},
|
|
|
|
|
features: getAuthFeatures(),
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return serviceUnavailable('Database unavailable');
|
|
|
|
|
}
|
|
|
|
|
})
|
2026-06-15 18:17:26 +07:00
|
|
|
.post('/login', async ({ body, set, request }) => {
|
2026-05-22 15:53:48 +00:00
|
|
|
const req = body as Partial<AuthRequest> | undefined;
|
|
|
|
|
const email = normalizeEmail(req?.email);
|
|
|
|
|
|
|
|
|
|
if (!email || !validatePassword(req?.password)) {
|
|
|
|
|
return badRequest('Valid email and password are required');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const db = createDbClient();
|
|
|
|
|
const rows = await db.select().from(users).where(eq(users.email, email)).limit(1);
|
|
|
|
|
const user = rows[0];
|
|
|
|
|
|
|
|
|
|
if (!user?.passwordHash || !(await verifyPassword(req!.password!, user.passwordHash))) {
|
2026-06-07 18:01:26 +07:00
|
|
|
authCounter.labels('login', 'false').inc();
|
2026-05-22 15:53:48 +00:00
|
|
|
return badRequest('Invalid email or password');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const token = await createSession(user.id);
|
2026-06-15 18:17:26 +07:00
|
|
|
set.headers['Set-Cookie'] = createSessionCookie(token, request.headers);
|
2026-05-22 15:53:48 +00:00
|
|
|
|
2026-06-07 18:01:26 +07:00
|
|
|
authCounter.labels('login', 'true').inc();
|
|
|
|
|
|
2026-05-22 15:53:48 +00:00
|
|
|
return {
|
|
|
|
|
user: {
|
|
|
|
|
id: user.id,
|
|
|
|
|
email: user.email,
|
|
|
|
|
name: user.name,
|
|
|
|
|
role: user.role === 'expert' ? 'expert' as const : 'user' as const,
|
|
|
|
|
},
|
|
|
|
|
features: getAuthFeatures(),
|
|
|
|
|
};
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return serviceUnavailable('Database unavailable');
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.post('/logout', async ({ request, set }) => {
|
2026-06-15 18:17:26 +07:00
|
|
|
const cookieHeader = request.headers.get('cookie');
|
|
|
|
|
await deleteSession(readSessionToken(cookieHeader));
|
|
|
|
|
set.headers['Set-Cookie'] = clearSessionCookie(request.headers);
|
2026-05-22 15:53:48 +00:00
|
|
|
return { ok: true };
|
|
|
|
|
})
|
|
|
|
|
.get('/google', ({ set }) => {
|
|
|
|
|
if (!env.googleOAuthEnabled) {
|
|
|
|
|
set.status = 404;
|
|
|
|
|
return { error: 'Google OAuth is not configured' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const params = new URLSearchParams({
|
|
|
|
|
client_id: env.googleClientId!,
|
|
|
|
|
redirect_uri: env.googleRedirectUri!,
|
|
|
|
|
response_type: 'code',
|
|
|
|
|
scope: 'openid email profile',
|
|
|
|
|
access_type: 'offline',
|
|
|
|
|
prompt: 'select_account',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
set.redirect = `https://accounts.google.com/o/oauth2/v2/auth?${params.toString()}`;
|
|
|
|
|
})
|
|
|
|
|
.get('/google/callback', ({ set }) => {
|
|
|
|
|
if (!env.googleOAuthEnabled) {
|
|
|
|
|
set.status = 404;
|
|
|
|
|
return { error: 'Google OAuth is not configured' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set.redirect = `${env.webAppUrl}/login?oauth=not-implemented`;
|
|
|
|
|
});
|