Clears the 124 CODE_SMELL findings from the SonarQube scan (server, client, and bot). All changes are behaviour-preserving refactors — no route, protocol, schema, or config changes — verified against the full server (381) and client (43) test suites plus a clean client build. By rule: - S3776 (20, cognitive complexity): extract helpers/handlers so each function drops under the threshold — shard model upsert builders, page/wiki update, block validation, notification stream mapping (dispatch table), SSO mobile login, shard ingest deps, uo-link socket backfill/connect, the bot slash- command dispatchers + discord manager, and the Shard/UserDetail/HeroEditor/ CharacterStats React components. - S4624 (34, nested template literals): pull inner templates into locals / a withQs() helper; rewrite shardEvents.describe() as a formatter table. - S3358 (35, nested ternaries): lift to if/else vars, lookup maps, small components, or guarded JSX expressions. - S6479 (12, array-index React keys): key by stable content instead of index (two in-editor lists left as-is; index matches their by-index edit model). - S6353 (6): [0-9]/[^0-9] -> \d/\D. S125 (5): reword state-shape comments that parsed as code. S3800/S3782 (botScore): JSDoc-type PATH_WEIGHTS tuples. - S6481 (2): memoize Auth/Site context values (and SiteContext brand). - S4144: dedupe HeroEditor upload handler into useImageUpload(). - S1126 (2), S6035, S5869 (redundant A-Z under /i), S5843 (town-name regex -> prefix list): assorted one-liners. Co-Authored-By: Claude <noreply@anthropic.com>
83 lines
2.5 KiB
JavaScript
83 lines
2.5 KiB
JavaScript
import { createContext, useContext, useEffect, useState, useCallback, useMemo } from 'react'
|
|
import { api } from '../api/client.js'
|
|
|
|
const AuthContext = createContext(null)
|
|
|
|
export function AuthProvider({ children }) {
|
|
const [user, setUser] = useState(null)
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
const refresh = useCallback(async () => {
|
|
try {
|
|
const data = await api.me()
|
|
setUser(data.user)
|
|
} catch {
|
|
setUser(null)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}, [])
|
|
|
|
useEffect(() => {
|
|
refresh()
|
|
}, [refresh])
|
|
|
|
// Step 1. Returns { user } on success, or { totpRequired, challenge } when the
|
|
// account has 2FA on (caller then calls loginTotp). `extra` carries honeypot.
|
|
const login = useCallback(async (username, password, extra) => {
|
|
const data = await api.login(username, password, extra)
|
|
if (data.user) setUser(data.user)
|
|
return data
|
|
}, [])
|
|
|
|
// Public self-registration (player). Creates the account, sets the session
|
|
// cookie, and returns { user }. `extra` carries the honeypot + optional email.
|
|
const register = useCallback(async (username, password, extra) => {
|
|
const data = await api.register(username, password, extra)
|
|
if (data.user) setUser(data.user)
|
|
return data
|
|
}, [])
|
|
|
|
// Step 2 for TOTP users: exchange the challenge + code for a real session.
|
|
const loginTotp = useCallback(async (challenge, code) => {
|
|
const data = await api.loginTotp(challenge, code)
|
|
setUser(data.user)
|
|
return data.user
|
|
}, [])
|
|
|
|
// Step 2 for SSO logins whose account has 2FA on. The pending challenge lives in
|
|
// an httpOnly cookie, so only the code is sent. Returns { user, returnTo }.
|
|
const ssoLoginTotp = useCallback(async (code) => {
|
|
const data = await api.ssoLoginTotp(code)
|
|
setUser(data.user)
|
|
return data
|
|
}, [])
|
|
|
|
const logout = useCallback(async () => {
|
|
try {
|
|
await api.logout()
|
|
} finally {
|
|
setUser(null)
|
|
}
|
|
}, [])
|
|
|
|
// Memoized so consumers don't re-render on every provider render (the callbacks
|
|
// are already stable via useCallback).
|
|
const value = useMemo(
|
|
() => ({ user, loading, login, register, loginTotp, ssoLoginTotp, logout, refresh }),
|
|
[user, loading, login, register, loginTotp, ssoLoginTotp, logout, refresh],
|
|
)
|
|
|
|
return (
|
|
<AuthContext.Provider value={value}>
|
|
{children}
|
|
</AuthContext.Provider>
|
|
)
|
|
}
|
|
|
|
export function useAuth() {
|
|
const ctx = useContext(AuthContext)
|
|
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
|
|
return ctx
|
|
}
|