fix: track user on page load
This commit is contained in:
21
auth.ts
21
auth.ts
@@ -2,9 +2,23 @@ import NextAuth from "next-auth"
|
||||
|
||||
import { env } from "@/env/server"
|
||||
|
||||
import { LoginTypeEnum } from "./types/components/tracking"
|
||||
|
||||
import type { NextAuthConfig, User } from "next-auth"
|
||||
import type { OIDCConfig } from "next-auth/providers"
|
||||
|
||||
function getLoginType(user: User) {
|
||||
if (user?.nonce) {
|
||||
return LoginTypeEnum.MagicLink
|
||||
}
|
||||
|
||||
if (user?.login_with.includes("@")) {
|
||||
return LoginTypeEnum.Email
|
||||
} else {
|
||||
return LoginTypeEnum.MembershipNumber
|
||||
}
|
||||
}
|
||||
|
||||
const customProvider = {
|
||||
clientId: env.CURITY_CLIENT_ID_USER,
|
||||
clientSecret: env.CURITY_CLIENT_SECRET_USER,
|
||||
@@ -39,6 +53,8 @@ const customProvider = {
|
||||
id: profile.id,
|
||||
sub: profile.sub,
|
||||
given_name: profile.given_name,
|
||||
login_with: profile.login_with,
|
||||
nonce: profile.nonce,
|
||||
}
|
||||
},
|
||||
} satisfies OIDCConfig<User>
|
||||
@@ -96,7 +112,8 @@ export const config = {
|
||||
async authorized({ auth, request }) {
|
||||
return true
|
||||
},
|
||||
async jwt({ account, session, token, trigger }) {
|
||||
async jwt({ account, session, token, trigger, user }) {
|
||||
const loginType = getLoginType(user)
|
||||
if (account) {
|
||||
return {
|
||||
access_token: account.access_token,
|
||||
@@ -104,6 +121,7 @@ export const config = {
|
||||
? account.expires_at * 1000
|
||||
: undefined,
|
||||
refresh_token: account.refresh_token,
|
||||
loginType,
|
||||
}
|
||||
} else if (Date.now() < token.expires_at) {
|
||||
return token
|
||||
@@ -158,6 +176,7 @@ export const config = {
|
||||
access_token: new_tokens.access_token,
|
||||
expires_at: new_tokens.expires_at,
|
||||
refresh_token: new_tokens.refresh_token ?? token.refresh_token,
|
||||
loginType,
|
||||
}
|
||||
} catch (error) {
|
||||
console.log("token-debug Error thrown when trying to refresh", {
|
||||
|
||||
Reference in New Issue
Block a user