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
rhitune3w ago
probably no
KHRM
KHRMOP3w 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
rhitune3w ago
oh you mean this.. role: { type: roleEnum("admin"), required: false, input: false, }, i thought u want something like this
KHRM
KHRMOP3w 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
rhitune3w ago
yeah it must work good also make a function for client side
KHRM
KHRMOP3w ago
oh good point!
Ping
Ping3w ago
@KHRM @rhitune auth client's useSession can be typed with the additionalFields plugin
KHRM
KHRMOP3w 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?