fix qr auth
This commit is contained in:
@@ -33,7 +33,7 @@ import { PhoneInput, phoneCountries, toE164 } from '@/components/ui/phone-input'
|
||||
import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs';
|
||||
|
||||
import type { AuthTokens, LoginMethod, OtpChannel, OtpSendResponse } from '@/lib/api';
|
||||
import { AUTH_TOKEN_KEY, claimQrLoginSession, createQrLoginSession, fetchCurrentUser, fetchFedcmSessionStatus, pollQrLoginSession } from '@/lib/api';
|
||||
import { AUTH_TOKEN_KEY, claimQrLoginSession, createQrLoginSession, fetchFedcmSessionStatus, pollQrLoginSession } from '@/lib/api';
|
||||
|
||||
import { isFedcmLoginPopup, finalizeFedcmLogin } from '@/lib/fedcm-login-bridge';
|
||||
|
||||
@@ -224,8 +224,7 @@ function LoginPageContent() {
|
||||
function finishQrAuth(auth: Omit<AuthTokens, 'user'> & { user?: AuthTokens['user'] }) {
|
||||
void (async () => {
|
||||
try {
|
||||
const user = auth.user ?? await fetchCurrentUser(auth.accessToken);
|
||||
const resolved = await applyLoginAuth({ ...auth, user });
|
||||
const resolved = await applyLoginAuth(auth);
|
||||
finishAuth(resolved.pinVerified, resolved.sessionId);
|
||||
} catch (error) {
|
||||
qrAuthStartedRef.current = false;
|
||||
@@ -410,22 +409,25 @@ function LoginPageContent() {
|
||||
|
||||
}
|
||||
|
||||
if (session.status === 'APPROVED' && session.auth && !qrAuthStartedRef.current) {
|
||||
const qrAuth = session.auth;
|
||||
const accessToken = session.accessToken ?? qrAuth?.accessToken;
|
||||
|
||||
if (session.status === 'APPROVED' && accessToken && !qrAuthStartedRef.current) {
|
||||
qrAuthStartedRef.current = true;
|
||||
|
||||
finishQrAuth({
|
||||
|
||||
accessToken: session.auth.accessToken,
|
||||
accessToken,
|
||||
|
||||
refreshToken: session.auth.refreshToken,
|
||||
refreshToken: session.refreshToken ?? qrAuth?.refreshToken ?? '',
|
||||
|
||||
sessionId: session.auth.sessionId,
|
||||
sessionId: session.authSessionId ?? qrAuth?.sessionId ?? session.sessionId,
|
||||
|
||||
pinVerified: session.auth.pinVerified,
|
||||
pinVerified: session.pinVerified ?? qrAuth?.pinVerified ?? false,
|
||||
|
||||
expiresAt: session.auth.expiresAt,
|
||||
expiresAt: qrAuth?.expiresAt ?? session.expiresAt,
|
||||
|
||||
user: session.auth.user
|
||||
user: qrAuth?.user
|
||||
|
||||
});
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ interface AuthContextValue {
|
||||
beginTotpLogin: (recipient: string) => Promise<string>;
|
||||
verifyTotpLogin: (totpChallengeToken: string, code: string) => Promise<AuthTokens>;
|
||||
completePin: (sessionId: string, pin: string) => Promise<void>;
|
||||
applyLoginAuth: (auth: AuthTokens) => Promise<AuthTokens>;
|
||||
applyLoginAuth: (auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) => Promise<Omit<AuthTokens, 'user'> & { user?: PublicUser }>;
|
||||
applyUserPatch: (patch: Partial<PublicUser>) => void;
|
||||
register: (data: { displayName: string; login: string; password: string }) => Promise<void>;
|
||||
refreshProfile: () => Promise<void>;
|
||||
@@ -88,10 +88,12 @@ function readCachedUserProfile(): PublicUser | null {
|
||||
}
|
||||
}
|
||||
|
||||
function persistPartialAuth(auth: AuthTokens) {
|
||||
function persistPartialAuth(auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) {
|
||||
window.localStorage.setItem(AUTH_REFRESH_KEY, auth.refreshToken);
|
||||
window.localStorage.setItem(AUTH_SESSION_KEY, auth.sessionId);
|
||||
cacheUserProfile(auth.user);
|
||||
if (auth.user) {
|
||||
cacheUserProfile(auth.user);
|
||||
}
|
||||
}
|
||||
|
||||
function applySessionState(
|
||||
@@ -625,13 +627,20 @@ export function AuthProvider({ children }: { children: React.ReactNode }) {
|
||||
);
|
||||
|
||||
const applyLoginAuth = React.useCallback(
|
||||
async (auth: AuthTokens) => {
|
||||
async (auth: Omit<AuthTokens, 'user'> & { user?: PublicUser }) => {
|
||||
if (auth.pinVerified) {
|
||||
await establishSession(auth);
|
||||
if (!auth.user) {
|
||||
const user = await fetchAuthSession(auth.accessToken).then((session) => session.user);
|
||||
await establishSession({ ...auth, user });
|
||||
} else {
|
||||
await establishSession(auth);
|
||||
}
|
||||
} else {
|
||||
persistPartialAuth(auth);
|
||||
if (auth.user) {
|
||||
setUser(auth.user);
|
||||
}
|
||||
setHasStoredSession(true);
|
||||
activatePinLock(auth.sessionId);
|
||||
}
|
||||
return auth;
|
||||
},
|
||||
|
||||
@@ -238,6 +238,10 @@ export interface QrLoginSession {
|
||||
qrPayload?: string;
|
||||
claimed?: boolean;
|
||||
claimedDeviceName?: string;
|
||||
accessToken?: string;
|
||||
refreshToken?: string;
|
||||
authSessionId?: string;
|
||||
pinVerified?: boolean;
|
||||
auth?: Omit<AuthTokens, 'user'> & { user?: PublicUser };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user