phoskee
phoskee
BABetter Auth
Created by phoskee on 3/7/2025 in #help
Additional fields
5 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
Found where I went wrong in caveats, thank you a lot!
11 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
I've already tried this, but I can give it another try Should I simply move the customSession to export const auth?
11 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
XHRGET
http://localhost:3000/api/auth/get-session
[HTTP/1.1 200 OK 96ms]


permissions [ {…} ]

user Object { id: "t0fwVlYcYF6D97fPbpOC8lDI1Cw5olXX", name: "qwe", email: "[email protected]", … }
id "t0fwVlYcYF6D97fPbpOC8lDI1Cw5olXX"
name "qwe"
emailVerified false
image ""
createdAt "2025-03-16T18:24:30.914Z"
updatedAt "2025-03-16T18:24:30.914Z"
twoFactorEnabled true
role "user"
banned null
banReason null
banExpires null
username "qweqwe"
displayUsername "qweqwe"
surname "qwe"
gender "F"
dateOfBirth "2025-03-04T00:00:00.000Z"
rank "qwe"
userPermissions [ {…} ]

session Object { id: "dCuqjiwaW0aw2Qhr6lLVNgVcw9Q3CAbx", expiresAt: "2025-03-24T21:19:14.590Z", token: "vVoMczrk5Hp6tJfSYLIdsJjKmOMK8yZt", … }
XHRGET
http://localhost:3000/api/auth/get-session
[HTTP/1.1 200 OK 96ms]


permissions [ {…} ]

user Object { id: "t0fwVlYcYF6D97fPbpOC8lDI1Cw5olXX", name: "qwe", email: "[email protected]", … }
id "t0fwVlYcYF6D97fPbpOC8lDI1Cw5olXX"
name "qwe"
emailVerified false
image ""
createdAt "2025-03-16T18:24:30.914Z"
updatedAt "2025-03-16T18:24:30.914Z"
twoFactorEnabled true
role "user"
banned null
banReason null
banExpires null
username "qweqwe"
displayUsername "qweqwe"
surname "qwe"
gender "F"
dateOfBirth "2025-03-04T00:00:00.000Z"
rank "qwe"
userPermissions [ {…} ]

session Object { id: "dCuqjiwaW0aw2Qhr6lLVNgVcw9Q3CAbx", expiresAt: "2025-03-24T21:19:14.590Z", token: "vVoMczrk5Hp6tJfSYLIdsJjKmOMK8yZt", … }
11 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
same for all additionalFields declared, btw
11 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
@bekacru no, adding the customSession actually adds the userPermission object to my response, but an error is reported in the code (example:
Property 'twoFactorEnabled' does not exist on type '{ userPermissions: { id: string; role: string; userId: string; port: string; }[]; id: string; name: string; email: string; emailVerified: boolean; createdAt: Date; updatedAt: Date; image?: string | ... 1 more ... | undefined; }'.
Property 'twoFactorEnabled' does not exist on type '{ userPermissions: { id: string; role: string; userId: string; port: string; }[]; id: string; name: string; email: string; emailVerified: boolean; createdAt: Date; updatedAt: Date; image?: string | ... 1 more ... | undefined; }'.
)
11 replies
BABetter Auth
Created by Manuel Coffin on 3/15/2025 in #help
What's the difference between additionalFields and customSession?
@bekacru by doing that, additionalFields are not typed, any suggestions?
import type { BetterAuthOptions } from "better-auth";
import { expo } from "@better-auth/expo";
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import {
admin,
customSession,
multiSession,
oAuthProxy,
twoFactor,
username,
} from "better-auth/plugins";
import { passkey } from "better-auth/plugins/passkey";

import { db } from "../../db/src/client";
import { env } from "../env";

export const config = {
database: prismaAdapter(db, {
provider: "sqlite",
}),
secret: env.AUTH_SECRET,
baseURL: "http://localhost:3000",
plugins: [
oAuthProxy(),
twoFactor(),
passkey(),
expo(),
multiSession(),
admin({
adminRoles: ["admin", "superadmin"],
}),
username(),
nextCookies(),
customSession(async ({ user, session }) => {
const permissions = await db.userPermissions.findMany({
where: {
userId: user.id,
},
});
return {
permissions,
user: {
...user,
userPermissions: permissions,
},
session,
};
}),
],
emailAndPassword: {
enabled: true,
autoSignIn: false,
},
trustedOrigins: ["exp://", "http://localhost:3000"],

user: {
additionalFields: {
surname: {
type: "string",
},
role: {
type: "string",
required: true,
},
gender: {
type: "string",
},
dateOfBirth: {
type: "date",
},
rank: {
type: "string",
},
},
},
} satisfies BetterAuthOptions;

export const auth = betterAuth(config);
export type Session = typeof auth.$Infer.Session;
import type { BetterAuthOptions } from "better-auth";
import { expo } from "@better-auth/expo";
import { betterAuth } from "better-auth";
import { prismaAdapter } from "better-auth/adapters/prisma";
import { nextCookies } from "better-auth/next-js";
import {
admin,
customSession,
multiSession,
oAuthProxy,
twoFactor,
username,
} from "better-auth/plugins";
import { passkey } from "better-auth/plugins/passkey";

import { db } from "../../db/src/client";
import { env } from "../env";

export const config = {
database: prismaAdapter(db, {
provider: "sqlite",
}),
secret: env.AUTH_SECRET,
baseURL: "http://localhost:3000",
plugins: [
oAuthProxy(),
twoFactor(),
passkey(),
expo(),
multiSession(),
admin({
adminRoles: ["admin", "superadmin"],
}),
username(),
nextCookies(),
customSession(async ({ user, session }) => {
const permissions = await db.userPermissions.findMany({
where: {
userId: user.id,
},
});
return {
permissions,
user: {
...user,
userPermissions: permissions,
},
session,
};
}),
],
emailAndPassword: {
enabled: true,
autoSignIn: false,
},
trustedOrigins: ["exp://", "http://localhost:3000"],

user: {
additionalFields: {
surname: {
type: "string",
},
role: {
type: "string",
required: true,
},
gender: {
type: "string",
},
dateOfBirth: {
type: "date",
},
rank: {
type: "string",
},
},
},
} satisfies BetterAuthOptions;

export const auth = betterAuth(config);
export type Session = typeof auth.$Infer.Session;
11 replies
BABetter Auth
Created by phoskee on 3/7/2025 in #help
Additional fields
@Ping The problem here remains the same as previously mentioned, and the solution you provided, particularly concerning obtaining fields from the session, is not functioning as expected..
5 replies