All files / src/features/authentication functions.ts

0% Statements 0/32
0% Branches 0/10
0% Functions 0/9
0% Lines 0/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                                                                                                                                                                                 
// Server functions for signing in, called by the sign-in page and the
// signed-in layout. The logic is in sign-in.server.ts; these validate the
// input with the shared schemas and wire in the request context.
import { createServerFn } from '@tanstack/react-start'
import { getRequest } from '@tanstack/react-start/server'
import { getLocale } from '#/paraglide/runtime.js'
import { getRequestContext } from '#/server/platform/context.server'
import { consumeLimit } from '#/server/platform/limits.server'
import { setDisplayName as setName } from './display-name.server'
import { setLocaleCookie } from './locale-cookie.server'
import { displayNameInput, requestCodeInput, verifyCodeInput } from './schemas'
import { requestCode, verifyCode, type SignInDeps } from './sign-in.server'
import { userRepository } from './user.repository.server'
 
function signInDeps(): SignInDeps {
  const context = getRequestContext()
  return {
    auth: context.auth,
    sendEmail: context.sendEmail,
    log: context.log,
    consumeLimit: (key, rule) => consumeLimit(context.db, key, rule),
    clientIp: context.clientIp(),
    requestLocale: getLocale(),
    appUrl: new URL(getRequest().url).origin,
    users: userRepository(context.db),
    now: () => new Date(),
  }
}
 
export const requestSignInCode = createServerFn({ method: 'POST' })
  .validator(requestCodeInput)
  .handler(({ data }) => requestCode(signInDeps(), data.email, data.locale))
 
export const verifySignInCode = createServerFn({ method: 'POST' })
  .validator(verifyCodeInput)
  .handler(async ({ data }) => {
    const result = await verifyCode(
      signInDeps(),
      data.email,
      data.code,
      data.locale,
    )
    if (result.ok) {
      setLocaleCookie(result.locale)
    }
    return result
  })
 
/**
 * The signed-in user, or null. Also puts the language cookie back in step
 * with the account's language if it was changed or removed; `localeReset`
 * then tells the layout to reload, because this request already resolved
 * its language from the old cookie.
 */
export const getSignedInUser = createServerFn({ method: 'GET' }).handler(
  async () => {
    const session = await getRequestContext().auth.getSession()
    if (!session) {
      return null
    }
    const { email, name, locale } = session.user
    const localeReset = getLocale() !== locale
    if (localeReset) {
      setLocaleCookie(locale)
    }
    return { email, name, locale, localeReset }
  },
)
 
export const setDisplayName = createServerFn({ method: 'POST' })
  .validator(displayNameInput)
  .handler(async ({ data }) => {
    const context = getRequestContext()
    const session = await context.auth.getSession()
    if (!session) {
      throw new Error('Not signed in')
    }
    return setName(
      {
        userId: session.user.id,
        users: userRepository(context.db),
        log: context.log,
        now: () => new Date(),
      },
      data.name,
    )
  })
 
export const signOut = createServerFn({ method: 'POST' }).handler(async () => {
  const { auth, log } = getRequestContext()
  // Reading the session first puts the user id on the line.
  if (await auth.getSession()) {
    await auth.signOut()
    log.info('authentication.signed_out')
  }
})