All files / app-dashboard/src/accounts handler.ts

82.22% Statements 37/45
73.68% Branches 42/57
100% Functions 4/4
82.5% Lines 33/40

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                              2x         81x 81x 81x 52x 52x 52x       3x 49x   49x 37x 37x             37x   12x   12x 12x   12x 12x 12x 8x 8x 8x 8x       8x 8x   8x 1x 4x 10x         12x 12x                                               12x               12x                              
import { oauthProviderAuthServerMetadata, oauthProviderOpenIdConfigMetadata } from '@better-auth/oauth-provider';
import { eq } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/d1';
import { createAuth, provisionClients, type Env } from './auth.ts';
import { isLocale, localeFromCookie, localeSetCookie, resolveLocale } from '@be-music/service-web-theme';
import { accountNotice, renderAccountPage, renderWelcomePage } from './page.ts';
import { CLIENT_JS } from './client.generated.ts';
import { avatarURL } from './avatars.ts';
import { profileHTTP } from './profile-http.ts';
import { createProfile, needsOnboarding, onAccountKept, welcomeNextPath } from './profiles.ts';
import { openDatabase } from './db/client.ts';
import { account, profile, user } from './schema.ts';
 
/** A redirect that is never cached. */
function redirect(location: string): Response {
  return new Response(null, { status: 302, headers: { location, 'cache-control': 'no-store' } });
}
 
/** Accounts routes on the Dashboard host: Better Auth, OIDC metadata, profiles, avatars and the account pages. */
export async function handleAccounts(request: Request, env: Env): Promise<Response> {
  const url = new URL(request.url);
  const profileResponse = await profileHTTP(request, env);
  if (profileResponse) return profileResponse;
  await provisionClients(env);
  const auth = createAuth(env);
  if (
    url.pathname === '/.well-known/openid-configuration' ||
    url.pathname === '/api/auth/.well-known/openid-configuration'
  )
    return oauthProviderOpenIdConfigMetadata(auth)(request);
  Iif (url.pathname === '/.well-known/oauth-authorization-server/api/auth')
    return oauthProviderAuthServerMetadata(auth)(request);
  if (url.pathname.startsWith('/api/auth/')) {
    const response = await auth.handler(request);
    Iif (url.pathname === '/api/auth/passkey/verify-registration' && response.ok) {
      const session = await auth.api.getSession({ headers: request.headers });
      if (session) {
        await drizzle(env.DB).update(user).set({ isAnonymous: false }).where(eq(user.id, session.user.id));
        if (session.user.isAnonymous === true) await onAccountKept(openDatabase(env.DB), session.user.id);
      }
    }
    return response;
  }
  Iif (url.pathname === '/assets/client.js' && request.method === 'GET')
    return new Response(CLIENT_JS, { headers: { 'content-type': 'text/javascript', 'cache-control': 'no-cache' } });
  Eif (['/login', '/account', '/welcome'].includes(url.pathname) && request.method === 'GET') {
    const session = await auth.api.getSession({ headers: request.headers });
    let current;
    let linkedDiscord = false;
    let linkedGoogle = false;
    if (session) {
      const db = openDatabase(env.DB);
      await createProfile(db, session.user.id, session.user.isAnonymous === true);
      current = await db.query.profile.findFirst({ where: eq(profile.userId, session.user.id) });
      const providers = await db
        .select({ providerId: account.providerId })
        .from(account)
        .where(eq(account.userId, session.user.id));
      linkedDiscord = providers.some((row) => row.providerId === 'discord');
      linkedGoogle = providers.some((row) => row.providerId === 'google');
      // A new account is welcomed before its account page (docs/services.md, "Welcome after sign-up").
      if (url.pathname === '/account' && (await needsOnboarding(db, session.user.id)))
        return redirect(`/welcome?next=${encodeURIComponent('/account')}`);
    } else if (url.pathname === '/welcome') return redirect('/login');
    const locale = resolveLocale({
      query: url.searchParams.get('lang') ?? undefined,
      cookie: localeFromCookie(request.headers.get('cookie')),
      acceptLanguage: request.headers.get('accept-language') ?? undefined,
    });
    const chosen = url.searchParams.get('lang');
    const common = {
      buildInfo: env.BUILD_TIMESTAMP
        ? { builtAt: env.BUILD_TIMESTAMP, revision: env.BUILD_REVISION ?? 'unknown' }
        : undefined,
      showDonateLink: env.SHOW_DONATE_LINK === 'true',
      anonymous: session?.user.isAnonymous === true,
      name: current?.displayName ?? session?.user.name ?? '',
      profile: current
        ? {
            handle: current.handle,
            displayName: current.displayName,
            avatarURL: avatarURL(current.handle, current.avatarKey),
          }
        : undefined,
      locale,
      requestURL: request.url,
      discord: !!env.DISCORD_CLIENT_ID && !!env.DISCORD_CLIENT_SECRET,
      google: !!env.GOOGLE_CLIENT_ID && !!env.GOOGLE_CLIENT_SECRET,
      linkedDiscord,
      linkedGoogle,
      notice: accountNotice(url.searchParams.get('error'), locale),
      apps: { dashboard: '/', ...(JSON.parse(env.OIDC_CLIENTS) as Record<string, string>) },
    };
    const body =
      url.pathname === '/welcome'
        ? renderWelcomePage({
            ...common,
            next: welcomeNextPath(url.searchParams.get('next')),
            continuing: url.searchParams.has('oauth_query'),
            step: url.searchParams.get('step') === 'finish' ? 'finish' : 'profile',
          })
        : renderAccountPage({ ...common, signedIn: !!session, login: url.pathname === '/login' });
    return new Response(body, {
      headers: {
        'content-type': 'text/html; charset=utf-8',
        'cache-control': 'private, no-store',
        vary: 'cookie, accept-language',
        ...(isLocale(chosen) ? { 'set-cookie': localeSetCookie(chosen, url.protocol === 'https:') } : {}),
        'content-security-policy':
          "default-src 'none'; script-src 'self'; style-src 'unsafe-inline'; connect-src 'self'; img-src 'self' blob:; frame-ancestors 'none'; base-uri 'none'; form-action 'self'",
        'referrer-policy': 'no-referrer',
        'x-content-type-options': 'nosniff',
      },
    });
  }
  return new Response('Not Found', { status: 404 });
}