All files / web/src auth.ts

0% Statements 0/208
0% Branches 0/1
0% Functions 0/1
0% Lines 0/208

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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209                                                                                                                                                                                                                                                                                                                                                                                                                                 
import { and, eq, isNull } from 'drizzle-orm'
import NextAuth from 'next-auth'
import Google from 'next-auth/providers/google'
import Nodemailer from 'next-auth/providers/nodemailer'
import { db, schema } from '@/db'
import { GUEST_COOKIE_NAME, verifyGuestToken } from '@/lib/guest-token'
import { abacisAdapter } from '@/lib/auth/adapter'
import { isAdminEmail } from '@/lib/auth/admin-emails'
import { mergeGuestIntoUser } from '@/lib/auth/mergeGuestIntoUser'
import { upgradeGuestToUser } from '@/lib/auth/upgradeGuestToUser'

/**
 * NextAuth v5 configuration with Google OAuth + email magic links
 *
 * Uses JWT strategy (stateless) with HttpOnly cookies.
 * Supports guest users (via custom cookie) and authenticated users.
 * Handles seamless guest-to-account upgrade on sign-in.
 */

export type Role = 'guest' | 'user' | 'admin'

// Extend NextAuth types to include our custom fields
declare module 'next-auth' {
  interface Session {
    user: {
      id: string
      name?: string | null
      email?: string | null
      image?: string | null
    }
    isGuest?: boolean
    guestId?: string | null
  }
}

declare module '@auth/core/jwt' {
  interface JWT {
    role?: Role
    guestId?: string | null
  }
}

export const { handlers, auth, signIn, signOut } = NextAuth((req) => ({
  adapter: abacisAdapter,

  // JWT strategy for stateless sessions
  session: {
    strategy: 'jwt',
    maxAge: 60 * 60 * 24 * 30, // 30 days
  },

  providers: [
    Google,
    Nodemailer({
      server: process.env.EMAIL_SERVER,
      from: process.env.EMAIL_FROM || 'Abaci One <hallock@gmail.com>',
    }),
  ],

  pages: {
    signIn: '/auth/signin',
    verifyRequest: '/auth/verify-request',
    error: '/auth/error',
  },

  callbacks: {
    /**
     * JWT callback - shapes the token stored in the cookie
     *
     * Handles:
     * - New sign-in with existing guest session → upgrade guest to full account
     * - Returning user sign-in on new device → merge guest data into existing account
     * - Brand new user sign-in (no guest session) → adapter creates user
     */
    async jwt({ token, user, account, trigger }) {
      if (trigger === 'signIn' && account && user) {
        // Read the guest cookie from the current request
        const guestCookie = req?.cookies.get(GUEST_COOKIE_NAME)?.value
        let guestId: string | null = null

        if (guestCookie) {
          try {
            const verified = await verifyGuestToken(guestCookie)
            guestId = verified.sid
          } catch {
            // Invalid guest token, ignore
          }
        }

        // Check if this provider+account already has a linked user
        const existingAccount = await db.query.authAccounts.findFirst({
          where: and(
            eq(schema.authAccounts.provider, account.provider),
            eq(schema.authAccounts.providerAccountId, account.providerAccountId ?? '')
          ),
        })

        if (existingAccount) {
          // Returning user (already has an account linked)
          if (guestId) {
            // They had a guest session on this device — merge guest data
            // Non-fatal: if merge fails, sign-in still succeeds (guest data is lost)
            try {
              const guestUser = await db.query.users.findFirst({
                where: eq(schema.users.guestId, guestId),
              })
              if (guestUser && guestUser.id !== existingAccount.userId) {
                await mergeGuestIntoUser(guestUser.id, existingAccount.userId)
              }
            } catch (err) {
              console.error('[auth] guest merge failed (non-fatal, sign-in continues):', err)
            }
          }
          token.sub = existingAccount.userId
          token.role = 'user'

          // Heal: ensure upgraded_at is set for returning OAuth users
          db.update(schema.users)
            .set({ upgradedAt: new Date() })
            .where(
              and(eq(schema.users.id, existingAccount.userId), isNull(schema.users.upgradedAt))
            )
            .catch((err: unknown) => console.error('[auth] Failed to heal upgraded_at:', err))
        } else if (guestId) {
          // New sign-in with existing guest session → upgrade the guest
          // Non-fatal: if upgrade fails, adapter-created user is used instead
          try {
            const upgradedUserId = await upgradeGuestToUser({
              guestId,
              email: user.email ?? '',
              name: user.name,
              image: user.image,
              provider: account.provider,
              providerAccountId: account.providerAccountId ?? user.email ?? '',
              providerType: account.type,
            })

            if (upgradedUserId) {
              token.sub = upgradedUserId
            } else {
              token.sub = user.id
            }
          } catch (err) {
            console.error('[auth] guest upgrade failed (non-fatal, sign-in continues):', err)
            token.sub = user.id
          }
          token.role = 'user'
        } else {
          // Brand new user (no guest session) — adapter already created user
          token.sub = user.id
          token.role = 'user'
        }

        token.guestId = guestId

        // Auto-promote admins based on ADMIN_EMAILS env var
        const email = user.email ?? token.email
        if (isAdminEmail(email)) {
          token.role = 'admin'
          // Sync admin role to DB (non-fatal)
          if (token.sub) {
            db.update(schema.users)
              .set({ role: 'admin' })
              .where(eq(schema.users.id, token.sub))
              .catch((err: unknown) =>
                console.error('[auth] Failed to sync admin role to DB:', err)
              )
          }
        }
      }

      return token
    },

    /**
     * Session callback - shapes what the client sees
     */
    async session({ session, token }) {
      if (session.user && token.sub) {
        session.user.id = token.sub
      }

      session.isGuest = token.role === 'guest' || !token.role

      // Expose the stable guest ID from the cookie
      const guestCookie = req?.cookies.get(GUEST_COOKIE_NAME)?.value
      session.guestId = null
      if (guestCookie) {
        try {
          const { sid } = await verifyGuestToken(guestCookie)
          session.guestId = sid
        } catch {
          // Invalid guest token, ignore
        }
      }

      return session
    },

    /**
     * Authorized callback - used in middleware for route protection
     */
    authorized({ auth }) {
      // Allow all visitors (guests + authenticated)
      return true
    },
  },
}))