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:
@@ -2,7 +2,15 @@
|
||||
|
||||
import React, { createContext, useCallback, useContext, useEffect, useMemo, useState } from "react";
|
||||
import { apiFetch, ApiError } from "@/lib/api";
|
||||
import { AuthResponse, AuthUser, TwoFactorChallenge, clearToken, getDeviceToken, getToken, isTwoFactorChallenge, saveDeviceToken, saveToken } from "@/lib/auth";
|
||||
import { AuthResponse, AuthUser, TwoFactorChallenge, clearToken, getDeviceToken, getToken, saveDeviceToken, saveToken } from "@/lib/auth";
|
||||
import { apiV2, type V2Result } from "@/lib/apiV2";
|
||||
|
||||
// A v2 "signed_in" answer as the session the app keeps (the user's fields plus the token).
|
||||
type V2Session = Extract<V2Result<"/auth/login", "post">["data"], { status: "signed_in" }>;
|
||||
const toAuthResponse = (s: V2Session): AuthResponse => ({
|
||||
...s.user, token: s.token, newsPromptPending: s.newsPromptPending, noticePromptPending: s.noticePromptPending,
|
||||
...(s.deviceToken ? { deviceToken: s.deviceToken } : {}),
|
||||
});
|
||||
|
||||
export type AuthContextValue = {
|
||||
user: AuthUser | null;
|
||||
@@ -91,22 +99,21 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}, []);
|
||||
|
||||
const login = useCallback(async (email: string, password: string) => {
|
||||
const res = await apiFetch<AuthResponse | TwoFactorChallenge>("/api/users/login", {
|
||||
method: "POST",
|
||||
body: { email, password, deviceToken: getDeviceToken() || undefined },
|
||||
const { data } = await apiV2("post", "/auth/login", {
|
||||
body: { identifier: email, password, deviceToken: getDeviceToken() || undefined },
|
||||
});
|
||||
// 2FA accounts get a challenge back instead of a token — nothing is saved
|
||||
// until verifyTwoFactor exchanges the emailed/WhatsApp code for one.
|
||||
if (isTwoFactorChallenge(res)) return res;
|
||||
return startSession(res as AuthResponse);
|
||||
if (data.status === "code_required") {
|
||||
const challenge: TwoFactorChallenge = { twoFactorRequired: true, challengeId: data.challengeId, method: data.method, destination: data.destination || "", ...(data.enrolling ? { enrolling: true } : {}) };
|
||||
return challenge;
|
||||
}
|
||||
return startSession(toAuthResponse(data));
|
||||
}, [startSession]);
|
||||
|
||||
const verifyTwoFactor = useCallback(async (challengeId: string, code: string, rememberDevice: boolean) => {
|
||||
const res = await apiFetch<AuthResponse>("/api/users/login/2fa", {
|
||||
method: "POST",
|
||||
body: { challengeId, code, rememberDevice },
|
||||
});
|
||||
return startSession(res);
|
||||
const { data } = await apiV2("post", "/auth/login/code", { body: { challengeId, code, rememberDevice } });
|
||||
return startSession(toAuthResponse(data));
|
||||
}, [startSession]);
|
||||
|
||||
const resendTwoFactor = useCallback((challengeId: string) => {
|
||||
@@ -117,11 +124,16 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
}, []);
|
||||
|
||||
const register = useCallback(async (data: { name: string; email: string; password: string; phoneNumber?: string; notificationPreference?: "email" | "whatsapp" | "both"; newsOptIn?: boolean }) => {
|
||||
const res = await apiFetch<AuthResponse>("/api/users", {
|
||||
method: "POST",
|
||||
body: data,
|
||||
// The sign-up form only submits once "I agree to the Terms of Use and Privacy Policy" is ticked.
|
||||
const { data: made } = await apiV2("post", "/auth/signup", {
|
||||
body: {
|
||||
name: data.name, email: data.email, password: data.password, acceptTerms: true,
|
||||
...(data.phoneNumber ? { phone: data.phoneNumber } : {}),
|
||||
...(data.notificationPreference ? { notificationPreference: data.notificationPreference } : {}),
|
||||
...(data.newsOptIn !== undefined ? { newsOptIn: data.newsOptIn } : {}),
|
||||
},
|
||||
});
|
||||
const { token, ...registeredUser } = res;
|
||||
const { token, ...registeredUser } = toAuthResponse(made);
|
||||
saveToken(token);
|
||||
setToken(token);
|
||||
setUser(registeredUser);
|
||||
|
||||
Reference in New Issue
Block a user