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