Authentication & Authorization
Portal auth is server/proxy-driven — there is no client-side OAuth SDK. The frontend
never talks to Google/Facebook (or the Repliers MLS API) directly: it asks the backend proxy
for a provider URL, redirects the browser to it, and receives an exchange code back on a
callback route. The proxy performs the real OAuth handshake and returns a JWT. The token
lives in a cookie and is attached as a Bearer header on every API call.
Key files
| File | Responsibility |
|---|---|
src/providers/UserProvider.tsx | Auth state, roles map, JWT decode, login methods, refresh, removeTokenFromUrl |
src/services/API/APIAuth.ts | Auth endpoints — auth (get url), authCallback, refresh, logout, signup, login, confirmOtp, tokenLogin |
src/services/API/types/auth.ts | AuthProvider union, AuthResponse, AuthCallbackResponse, ApiUserProfile |
src/utils/tokens.ts | Cookie token get/set/clear, expired, refreshNeeded |
src/services/API/APIBase.ts | getHeaders attaches Bearer token; fetchRaw clears token on 401 |
src/app/auth/[provider]/processing/page.tsx | OAuth callback page — exchanges ?code= for a token |
src/components/templates/components/TokenLoginHandler.tsx | Silent email-link ?token= login |
Providers & methods
AuthProvider = 'google' | 'facebook' | 'otp' (types/auth.ts). There is no Apple and
no email/password login.
- Google / Facebook — OAuth redirect flow (
APIAuth.auth→ provider URL →authCallback). - OTP — passwordless email one-time passcode.
requestOtpLogincallsAPIAuth.login; a404means the user does not exist yet, so it falls through toAPIAuth.signupand returns the signup result without a second login request.loginWithOtpconfirms the code viaAPIAuth.confirmOtp(/auth/otp).
Flow
- Start —
APIAuth.auth(provider)hits/auth/{provider}/urlwith aredirect_uriof/auth/{provider}/processingand returnsAuthResponse { url }; the app redirects there. - Callback — the provider returns to
/auth/[provider]/processingwith?code=. That page callslogin(provider, code)→APIAuth.authCallback→/auth/{provider}/cb. - Token —
parseAuthResponsereadsAuthCallbackResponse { profile, token },jwtDecodes the JWT forsub(clientId) androle, thenprocessAuthResponsepersists the profile (localStorage,profileKey) and the token viasetToken(cookie,tokenKey, 30-day expiry). A hardwindow.location.replacereturns the user to the saved callback URL. - Bearer — every request routes through
APIBase.getHeaders, which appendsAuthorization: Bearer <token>when the token exists and is notexpired. - Refresh — on mount,
UserProviderchecks the token:expired→logout;refreshNeeded(past 50% of lifespan) →refresh, which callsAPIAuth.refresh(/auth/refresh) and re-stores the new token. A failed refresh logs the user out.
Roles
UserProvider maps the JWT role index to a name (default index 2 when absent):
export const roles = { 1: 'root', 2: 'user', 3: 'admin', 4: 'agent' } as constThree boolean flags derived from profile.role gate the UI (used ~50 times app-wide):
userRole—role === 'user'agentRole—role === 'agent' || role === 'admin'adminRole—role === 'admin'
agentId is set to the sub claim only for the agent role. Consume all of this through
useUser() (see State Management).
Gotchas & hints
- Tokens can arrive in the URL, then get stripped. OAuth uses
?code=on the processing page; email links use?token=handled byTokenLoginHandler/ClientSideGuard(loginWithToken→/auth/repliers-token). Both immediately callremoveTokenFromUrl(history.replaceState) so the secret never lingers in the address bar or history. 401clears the token.APIBase.fetchRawcallsclearToken()on any401response.- Token lives in a cookie, profile in localStorage.
getTokenSyncreads thejs-cookievalue; clientloggedis reactive provider state initialized after mount. OAuth callbacks also use hard navigation to the saved callback URL. - OTP as a second factor. A
412from a?token=login means OTP is required — the handler opens theotp-authdialog. - Proxy-driven, not Repliers-direct. The frontend never performs OAuth itself and never calls the MLS API directly — the backend proxy owns the handshake and issues the JWT.
See also User Features.
Lifecycle and verification
Source review: e30dd9b47, 2026-11-02. The current UI logout clears local
profile/token state and reloads; it does not invoke APIAuth.logout. Clearing
a token after a 401 is not the same as resetting every mounted provider.
F05
traces the transitions and Data Ownership
describes persistence. No OAuth, OTP or server revocation flow was executed for
this documentation edition.