All backend auth now goes through our own token service — Firebase Auth
dependency is fully removed from auth paths. FCM (firebase-admin messaging)
is still used for push.
Schema:
- auth_sessions (multi-device refresh tokens, bcrypt-hashed)
- otp_requests (Fazpass reference + rate-limit history)
- customers.email + google_sub + apple_sub (social identity)
- control_center_users.password_hash + failed_login_count + lockout_until
- firebase_uid columns made nullable (drop in later cleanup migration)
- 6 new app_config keys for OTP + CC lockout tuning
Services:
- password.service.js — bcrypt cost 12 + complexity (min 8, digit + upper +
lower)
- token.service.js — JWT HS256 access (1h) + opaque refresh (30d, bcrypt-
hashed, rotated on use); session_id claim pre-wires future Valkey-based
instant revocation; revokeSession + revokeAllSessionsForUser helpers
- social-identity.service.js — Google via google-auth-library, Apple via
jwks-rsa + jsonwebtoken
- otp.service.js — Fazpass stub (generates locally, logs the code) clearly
marked for replacement once real API docs arrive; rate-limit + resend
cooldown + verify-attempts all configurable via app_config
- auth.service.js — orchestrator: signInAnonymous, completeCustomer/Mitra-
PhoneSignIn, signInWithGoogle, signInWithApple, signInCcUser, refresh,
logout; reject-on-existing for identity conflicts
- cc-user.service.js — email+password helpers + lockout counters
Routes & middleware:
- authenticate middleware now verifies our JWT and attaches
request.auth = { userType, userId, sessionId }
- WebSocket handshake verifies our JWT (no more Firebase lookup)
- All existing routes updated to use request.auth.userId instead of
request.firebaseUser.uid
- New public routes:
/api/shared/auth/anonymous /refresh /logout
/api/client/auth/otp/request /otp/verify /google /apple /me /profile
/api/mitra/auth/otp/request /otp/verify /me
- New internal routes:
/internal/auth/login /refresh /logout /me (httpOnly cookie refresh)
/internal/control-center-users (accepts plain password, bcrypt-hashed)
/internal/control-center-users/me/password (self-service change)
/internal/control-center-users/:id/password (admin forced reset)
- Deleted legacy customer.routes.js (anonymous + link handled by auth now)
- app.internal.js: @fastify/cookie + CORS credentials for CC httpOnly cookie
Config:
- AUTH_JWT_SECRET + ACCESS_TOKEN_TTL_SECONDS + REFRESH_TOKEN_TTL_DAYS env
- FAZPASS_* env vars (TBD until real API docs)
- GOOGLE_OAUTH_CLIENT_IDS, APPLE_SERVICES_ID/TEAM_ID/KEY_ID/PRIVATE_KEY
- ADMIN_EMAIL + ADMIN_PASSWORD for seed
- CC_ORIGIN for internal-app CORS origin allowlist
Dependencies:
- Added: bcrypt, jsonwebtoken, jwks-rsa, google-auth-library, @fastify/cookie
- Kept: firebase-admin (messaging only)
Still outstanding: Fazpass API integration (stub in place), Apple Developer
prereqs for end-to-end iOS testing, client_app/mitra_app/control_center auth
flow rewrites.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
118 lines
3.5 KiB
JavaScript
118 lines
3.5 KiB
JavaScript
import { getDb } from '../db/client.js'
|
|
|
|
const sql = getDb()
|
|
|
|
|
|
export const getCcUserById = async (id) => {
|
|
const [user] = await sql`
|
|
SELECT
|
|
u.id, u.email, u.display_name, u.created_at,
|
|
u.password_hash, u.failed_login_count, u.lockout_until,
|
|
r.id as role_id, r.name as role_name, r.permissions
|
|
FROM control_center_users u
|
|
JOIN roles r ON r.id = u.role_id
|
|
WHERE u.id = ${id}
|
|
`
|
|
if (!user) return null
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
display_name: user.display_name,
|
|
created_at: user.created_at,
|
|
password_hash: user.password_hash,
|
|
failed_login_count: user.failed_login_count,
|
|
lockout_until: user.lockout_until,
|
|
role: { id: user.role_id, name: user.role_name, permissions: user.permissions },
|
|
}
|
|
}
|
|
|
|
export const getCcUserByEmail = async (email) => {
|
|
const [user] = await sql`
|
|
SELECT
|
|
u.id, u.email, u.display_name, u.created_at,
|
|
u.password_hash, u.failed_login_count, u.lockout_until,
|
|
r.id as role_id, r.name as role_name, r.permissions
|
|
FROM control_center_users u
|
|
JOIN roles r ON r.id = u.role_id
|
|
WHERE u.email = ${email}
|
|
`
|
|
if (!user) return null
|
|
return {
|
|
id: user.id,
|
|
email: user.email,
|
|
display_name: user.display_name,
|
|
created_at: user.created_at,
|
|
password_hash: user.password_hash,
|
|
failed_login_count: user.failed_login_count,
|
|
lockout_until: user.lockout_until,
|
|
role: { id: user.role_id, name: user.role_name, permissions: user.permissions },
|
|
}
|
|
}
|
|
|
|
export const createCcUserWithPassword = async ({ email, display_name, role_id, password_hash }) => {
|
|
const [user] = await sql`
|
|
INSERT INTO control_center_users (email, display_name, role_id, password_hash)
|
|
VALUES (${email}, ${display_name}, ${role_id}, ${password_hash})
|
|
RETURNING id, email, display_name, role_id, created_at
|
|
`
|
|
const [role] = await sql`SELECT id, name FROM roles WHERE id = ${role_id}`
|
|
return { ...user, role }
|
|
}
|
|
|
|
export const updateCcUserPasswordHash = async (id, password_hash) => {
|
|
await sql`
|
|
UPDATE control_center_users SET password_hash = ${password_hash}
|
|
WHERE id = ${id}
|
|
`
|
|
}
|
|
|
|
export const incrementCcUserFailedLogin = async (id, lockoutMinutes, maxAttempts) => {
|
|
// Atomic: increment counter; if reaches maxAttempts set lockout_until
|
|
const [row] = await sql`
|
|
UPDATE control_center_users
|
|
SET failed_login_count = failed_login_count + 1,
|
|
lockout_until = CASE
|
|
WHEN failed_login_count + 1 >= ${maxAttempts}
|
|
THEN NOW() + (${lockoutMinutes} || ' minutes')::interval
|
|
ELSE lockout_until
|
|
END
|
|
WHERE id = ${id}
|
|
RETURNING failed_login_count, lockout_until
|
|
`
|
|
return row
|
|
}
|
|
|
|
export const resetCcUserFailedLogin = async (id) => {
|
|
await sql`
|
|
UPDATE control_center_users
|
|
SET failed_login_count = 0, lockout_until = NULL
|
|
WHERE id = ${id}
|
|
`
|
|
}
|
|
|
|
export const listCcUsers = async ({ page = 1, limit = 20 }) => {
|
|
const offset = (page - 1) * limit
|
|
const items = await sql`
|
|
SELECT
|
|
u.id, u.email, u.display_name, u.created_at,
|
|
r.id as role_id, r.name as role_name
|
|
FROM control_center_users u
|
|
JOIN roles r ON r.id = u.role_id
|
|
ORDER BY u.created_at DESC
|
|
LIMIT ${limit} OFFSET ${offset}
|
|
`
|
|
const [{ count }] = await sql`SELECT COUNT(*) FROM control_center_users`
|
|
return {
|
|
items: items.map((u) => ({
|
|
id: u.id,
|
|
email: u.email,
|
|
display_name: u.display_name,
|
|
created_at: u.created_at,
|
|
role: { id: u.role_id, name: u.role_name },
|
|
})),
|
|
total: Number(count),
|
|
page,
|
|
limit,
|
|
}
|
|
}
|