mirror of
https://github.com/vernu/textbee.git
synced 2026-07-30 17:07:46 -04:00
My earlier estimate of 34 errors was wrong, and the reason is worth recording: tsconfig carried "strictNullChecks": false as a duplicate key AFTER the strict flag, and an explicit option beats the strict umbrella even when --strict is passed on the CLI. So the measurement that produced 34 had silently excluded every null-safety error. The real number was 63. Both duplicate keys are gone; strict: true now stands alone. Most were implicit-any, but strict caught several genuine type lies: - SendSmsPayload.deviceId and WebhookData._id were optional while both are interpolated into request paths, so an absent value would have hit /gateway/devices/undefined/send-sms or PATCH /webhooks/undefined. - webhook-table's deviceName was typed string while buildDeviceLabel returns string | string[] and the Device cell already renders the array case. The type never described what the code produced. - webhooks-section read `webhooks?.data?.length > 0`, comparing undefined against 0 while the query was still in flight. - app-header declared a non-null Session while its own body guarded with session?.user throughout. Making the type honest surfaced four genuinely unguarded accesses. - api-keys kept a local ApiKeyRow duplicating the shared ApiKey type, so the list callback annotated rows as one type while the hook returned the other. ApiKeyRow is now an alias and the two extra fields moved onto ApiKey. - The notifications envelope typed its rows as unknown[], so the deliveries table's row type went entirely unchecked. Now a real WebhookNotification type. The react-hook-form cluster (20 of the 63) was one root cause: zod's .default() makes the input and output types differ, so z.infer (the output) is not what the resolver takes. Fixed by typing the forms with z.input and z.output separately, which changes nothing at runtime. Also bumped target es5 to ES2017, which fixes the Set-iteration error that made tsc --noEmit fail before any of this. Next compiles browser output via SWC and its own browserslist, so bundle targeting is unaffected. Added @types/papaparse and @types/react-syntax-highlighter, and a typecheck script, since next build does not check test files. No @ts-expect-error and no new any were used. Verified: typecheck clean, build clean, 0 lint errors (21 warnings, unchanged), 155 unit tests, 78 e2e. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
186 lines
4.9 KiB
TypeScript
186 lines
4.9 KiB
TypeScript
import CredentialsProvider from 'next-auth/providers/credentials'
|
|
import { httpServerClient } from './httpServerClient'
|
|
import type { DefaultSession, NextAuthOptions } from 'next-auth'
|
|
import { ApiEndpoints } from '@/config/api'
|
|
import { Routes } from '@/config/routes'
|
|
|
|
// add custom fields to the session and user interfaces
|
|
declare module 'next-auth' {
|
|
interface Session {
|
|
user: {
|
|
id?: string
|
|
role?: string
|
|
phone?: string
|
|
avatar?: string
|
|
accessToken?: string
|
|
} & DefaultSession['user']
|
|
}
|
|
|
|
interface User {
|
|
// The backend returns Mongo documents, so the id arrives as _id and is
|
|
// copied onto the token below.
|
|
_id?: string
|
|
role?: string
|
|
phone?: string
|
|
avatar?: string
|
|
accessToken?: string
|
|
}
|
|
}
|
|
|
|
// The jwt/session callbacks read and write these, so the token has to declare
|
|
// them or every callback parameter falls back to an implicit any.
|
|
declare module 'next-auth/jwt' {
|
|
interface JWT {
|
|
id?: string
|
|
role?: string
|
|
phone?: string
|
|
avatar?: string
|
|
accessToken?: string
|
|
}
|
|
}
|
|
|
|
export const authOptions: NextAuthOptions = {
|
|
providers: [
|
|
CredentialsProvider({
|
|
id: 'email-password-login',
|
|
name: 'email-password-login',
|
|
credentials: {
|
|
email: { label: 'email', type: 'text' },
|
|
password: { label: 'Password', type: 'password' },
|
|
turnstileToken: { label: 'Turnstile Token', type: 'text' },
|
|
},
|
|
async authorize(credentials) {
|
|
// NextAuth can invoke authorize with no credentials at all. There is
|
|
// nothing to authenticate in that case, so refuse rather than posting
|
|
// undefined fields to the login endpoint.
|
|
if (!credentials) return null
|
|
const { email, password, turnstileToken } = credentials
|
|
try {
|
|
const res = await httpServerClient.post(ApiEndpoints.auth.login(), {
|
|
email,
|
|
password,
|
|
turnstileToken,
|
|
})
|
|
|
|
const user = res.data.data.user
|
|
const accessToken = res.data.data.accessToken
|
|
|
|
return {
|
|
...user,
|
|
accessToken,
|
|
}
|
|
} catch (e) {
|
|
console.log(e)
|
|
|
|
return null
|
|
}
|
|
},
|
|
}),
|
|
CredentialsProvider({
|
|
id: 'email-password-register',
|
|
name: 'email-password-register',
|
|
credentials: {
|
|
email: { label: 'email', type: 'text' },
|
|
password: { label: 'Password', type: 'password' },
|
|
name: { label: 'Name', type: 'text' },
|
|
phone: { label: 'Phone', type: 'text' },
|
|
turnstileToken: { label: 'Turnstile Token', type: 'text' },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials) return null
|
|
const { email, password, name, phone, turnstileToken } = credentials
|
|
try {
|
|
const res = await httpServerClient.post(
|
|
ApiEndpoints.auth.register(),
|
|
{
|
|
email,
|
|
password,
|
|
name,
|
|
phone,
|
|
turnstileToken,
|
|
}
|
|
)
|
|
|
|
const user = res.data.data.user
|
|
const accessToken = res.data.data.accessToken
|
|
|
|
return {
|
|
...user,
|
|
accessToken,
|
|
}
|
|
} catch (e) {
|
|
console.log(e)
|
|
return null
|
|
}
|
|
},
|
|
}),
|
|
CredentialsProvider({
|
|
id: 'google-id-token-login',
|
|
name: 'google-id-token-login',
|
|
credentials: {
|
|
idToken: { label: 'idToken', type: 'text' },
|
|
},
|
|
async authorize(credentials) {
|
|
if (!credentials) return null
|
|
const { idToken } = credentials
|
|
try {
|
|
const res = await httpServerClient.post(
|
|
ApiEndpoints.auth.signInWithGoogle(),
|
|
{
|
|
idToken,
|
|
}
|
|
)
|
|
|
|
const user = res.data.data.user
|
|
const accessToken = res.data.data.accessToken
|
|
|
|
return {
|
|
...user,
|
|
accessToken,
|
|
}
|
|
} catch (e) {
|
|
console.log(e)
|
|
|
|
return null
|
|
}
|
|
},
|
|
}),
|
|
],
|
|
pages: {
|
|
signIn: Routes.login,
|
|
},
|
|
session: {
|
|
strategy: 'jwt',
|
|
},
|
|
callbacks: {
|
|
async jwt({ token, user, trigger, session }) {
|
|
if (trigger === 'update') {
|
|
if (session.name !== token.name) {
|
|
token.name = session.name
|
|
}
|
|
if (session.phone !== token.phone) {
|
|
token.phone = session.phone
|
|
}
|
|
return token
|
|
}
|
|
|
|
if (user) {
|
|
token.id = user._id
|
|
token.role = user.role
|
|
token.accessToken = user.accessToken
|
|
token.avatar = user.avatar
|
|
token.phone = user.phone
|
|
}
|
|
return token
|
|
},
|
|
async session({ session, token }): Promise<any> {
|
|
session.user.id = token.id
|
|
session.user.role = token.role
|
|
session.user.accessToken = token.accessToken
|
|
session.user.avatar = token.avatar
|
|
session.user.phone = token.phone
|
|
return session
|
|
},
|
|
},
|
|
}
|