Typing Role

I am using an enum from Prisma but I cant pass that enum as a type for additional fields
user: {
additionalFields: {
role: {
type: "string",
required: false,
input: false,
},
},
},
user: {
additionalFields: {
role: {
type: "string",
required: false,
input: false,
},
},
},
(see image) is there a way to have it typed as the values of my enum instead of as 'string' ?
No description
9 Replies
rhitune
rhitune2w ago
probably no
KHRM
KHRMOP2w ago
sometimes I forget simple solutions
import "server-only";

import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { Session, User } from "better-auth";
import { UserRole } from "@prisma/client";

type UserWithRole = Omit<User, "role"> & { role: UserRole };

export async function getAuthSession(): Promise<{
session: Session;
user: UserWithRole;
} | null> {
const session = await auth.api.getSession({
headers: await headers(),
});
return session as { session: Session; user: UserWithRole } | null;
}
import "server-only";

import { auth } from "@/lib/auth";
import { headers } from "next/headers";
import { Session, User } from "better-auth";
import { UserRole } from "@prisma/client";

type UserWithRole = Omit<User, "role"> & { role: UserRole };

export async function getAuthSession(): Promise<{
session: Session;
user: UserWithRole;
} | null> {
const session = await auth.api.getSession({
headers: await headers(),
});
return session as { session: Session; user: UserWithRole } | null;
}
rhitune
rhitune2w ago
oh you mean this.. role: { type: roleEnum("admin"), required: false, input: false, }, i thought u want something like this
KHRM
KHRMOP2w ago
i did but its not valid better auth, so this wrapper is relatively safe since all types are directly from better auth except role
rhitune
rhitune2w ago
yeah it must work good also make a function for client side
KHRM
KHRMOP2w ago
oh good point!
Ping
Ping2w ago
@KHRM @rhitune auth client's useSession can be typed with the additionalFields plugin
KHRM
KHRMOP2w ago
yes I have this setup, was trying to get to be more verbose on what literal strings a role can be i.e. 'user' , 'mod', 'admin'

Did you find this page helpful?