API v2 accounts and check-in (1.51.2)
- v2: auth/signup, auth/login (+ code step, rate limited), PATCH /me; check-in look-up and scan returning an outcome for every normal result. Campus rule as v1. - Sign up/login/2FA/profile moved to src/services/auth.ts; scan preview and scan to src/services/checkin.ts. v1 answers unchanged (ServiceError v1Status). - Login, 2FA step, sign up and profile/password pages use v2. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ls1vMdg2GYqbnKZmPaX1ot
This commit is contained in:
@@ -5,6 +5,7 @@ import { pageQuery, paged, single, skipTake, pageOf } from '../paging';
|
||||
import { id, iso, isoDate } from '../schemas';
|
||||
import { prisma } from '../legacy';
|
||||
import { registrationSummary, SUMMARY_INCLUDE, toSummary } from './registrations';
|
||||
import { updateProfile } from '../../services/auth';
|
||||
|
||||
const me = z.object({
|
||||
id,
|
||||
@@ -13,9 +14,17 @@ const me = z.object({
|
||||
phone: z.string().nullable(),
|
||||
role: z.enum(['admin', 'supervisor', 'staff', 'user']),
|
||||
twoFactorEnabled: z.boolean(),
|
||||
notificationPreference: z.enum(['email', 'whatsapp', 'both']),
|
||||
homeCampusId: id.nullable(),
|
||||
createdAt: isoDate,
|
||||
});
|
||||
|
||||
type MeRow = { id: string; name: string; email: string; phoneNumber: string | null; role: 'admin' | 'supervisor' | 'staff' | 'user'; twoFactorEnabled: boolean | null; notificationPreference: 'email' | 'whatsapp' | 'both'; homeCampusId: string | null; createdAt: Date };
|
||||
const toMe = (u: MeRow): z.input<typeof me> => ({
|
||||
id: u.id, name: u.name, email: u.email, phone: u.phoneNumber ?? null, role: u.role, twoFactorEnabled: !!u.twoFactorEnabled,
|
||||
notificationPreference: u.notificationPreference, homeCampusId: u.homeCampusId ?? null, createdAt: iso(u.createdAt),
|
||||
});
|
||||
|
||||
export const getMe = defineRoute({
|
||||
method: 'get',
|
||||
path: '/me',
|
||||
@@ -26,10 +35,34 @@ export const getMe = defineRoute({
|
||||
async handler({ user }) {
|
||||
const u = await prisma.user.findUnique({
|
||||
where: { id: user!.id },
|
||||
select: { id: true, name: true, email: true, phoneNumber: true, role: true, twoFactorEnabled: true, createdAt: true },
|
||||
select: { id: true, name: true, email: true, phoneNumber: true, role: true, twoFactorEnabled: true, notificationPreference: true, homeCampusId: true, createdAt: true },
|
||||
});
|
||||
if (!u) throw new ApiError('UNAUTHENTICATED', 'User not found');
|
||||
return { data: { id: u.id, name: u.name, email: u.email, phone: u.phoneNumber ?? null, role: u.role, twoFactorEnabled: !!u.twoFactorEnabled, createdAt: iso(u.createdAt) } };
|
||||
return { data: toMe(u) };
|
||||
},
|
||||
});
|
||||
|
||||
export const updateMe = defineRoute({
|
||||
method: 'patch',
|
||||
path: '/me',
|
||||
tag: 'Me',
|
||||
summary: 'Change my details',
|
||||
description: 'Only what is sent changes. A new `password` needs `currentPassword`; it sends a security notice and stops other devices skipping the 2FA code. The answer has a fresh `token` (the old one keeps working).',
|
||||
access: 'user',
|
||||
body: z.object({
|
||||
name: z.string().trim().min(1).max(200).optional(),
|
||||
email: z.email().max(200).optional(),
|
||||
phone: z.string().trim().max(40).nullable().optional().describe('null or empty removes it'),
|
||||
notificationPreference: z.enum(['email', 'whatsapp', 'both']).optional(),
|
||||
homeCampusId: id.nullable().optional().describe('While campuses are on'),
|
||||
password: z.string().min(8).max(200).optional(),
|
||||
currentPassword: z.string().max(200).optional(),
|
||||
}),
|
||||
response: single(me).extend({ token: z.string() }),
|
||||
async handler({ body, user, req }) {
|
||||
const { getClientIp } = require('../../utils/requestUtils') as { getClientIp(req: unknown): string };
|
||||
const { user: u, token } = await updateProfile(user!.id, { ...body, phoneNumber: body.phone }, { ip: getClientIp(req), userAgent: req.headers['user-agent'] });
|
||||
return { data: toMe(u), token };
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user