콘텐츠로 이동

@gj-kit/expo-auth 레시피

각 코드 블록은 패키지 README에서 이미 타입 검사하는 public-contract 예제를 재사용합니다.

storage와 refresh callback을 한 번 선택하면 패키지가 그 앱 소유 경계 주변의 호출자를 조정합니다.

완료 상태: 로그인 상태를 저장하고 동시 refresh를 조정하는 앱 소유 세션 하나를 만듭니다.

앱 소유 경계: storage adapter를 한 번 선택하고, 앱의 refresh endpoint 콜백만 연결합니다.

import { createAuthSession, type RefreshRequest } from '@gj-kit/expo-auth';
import { createTokenStorage, createWebLocksRefreshLock } from '@gj-kit/expo-auth/storage';
declare const refresh: RefreshRequest; // Your API client classifies rotated, invalid, or transient.
export const session = createAuthSession({
storage: createTokenStorage({ keyPrefix: 'myapp.auth' }),
lock: createWebLocksRefreshLock({ name: 'myapp.auth' }),
refresh,
});

2. 모든 refresh outcome을 명시적으로 처리하기

섹션 제목: “2. 모든 refresh outcome을 명시적으로 처리하기”

matcher를 사용해 transient 또는 rotated 분기를 빼먹으면 컴파일 오류가 나게 합니다.

refresh()의 다섯 결말을 키로 소비하므로, 네트워크 오류를 오탐 로그아웃으로 바꾸는 누락이 컴파일 단계에서 걸립니다. tokens는 refreshed·adopted로 좁힌 뒤에만 접근할 수 있고, refresh 콜백이 throw하면 던진 값이 cause에 담긴 채 transient로 도착합니다.

import { matchRefreshOutcome, type RefreshOutcome } from '@gj-kit/expo-auth';
declare const outcome: RefreshOutcome; // the app owns this — it is `await session.refresh()`
declare const goToSignIn: () => null; // the app owns navigation
declare const report: (cause: unknown) => void; // the app owns telemetry
export const accessToken = matchRefreshOutcome<string | null>(outcome, {
refreshed: ({ tokens }) => tokens.accessToken, // `tokens` exists here and on `adopted` only
adopted: ({ tokens }) => tokens.accessToken,
'signed-out': () => goToSignIn(),
invalid: () => goToSignIn(), // a definitive server rejection
transient: ({ cause }) => {
report(cause); // stored tokens are left untouched — a 5xx is not a sign-out
return null;
},
});
// Delete the `transient` line above and tsc refuses the call:
// error TS2345: ... Property 'transient' is missing in type ... but required in type ...

전체 API reference에서 모든 export와 import path를 확인하세요.