Is it a possible to use enum as type for fields in additionalFields ?

I want to use the role field directly with my user session, I have a enum with "ADMIN" and "USER"
13 Replies
bekacru
bekacru3mo ago
hmm? does this mean you want to have a role field in your user table?
Mattèo
MattèoOP3mo ago
Yes exactly, i'm using Prisma as database adapter and I have a role: Role? field
bekacru
bekacru3mo ago
define addtional fields in your user config. If you need to define it as enum for type inference you can pass an array as a type.
const auth = betterAuth({
user: {
role: ["ADMIN", "USER"]
}
})
const auth = betterAuth({
user: {
role: ["ADMIN", "USER"]
}
})
Mattèo
MattèoOP3mo ago
None of these implementations works for me, I got TS errors (even this snippet ⬆️)
// Server Side
export const auth = betterAuth({
user: {
fields: {
role: ["ADMIN", "USER"],
// ^? error here
},
},
user: {
role: ["ADMIN", "USER"],
// ^? error here
},
user: {
additionalFields: {
role: ["ADMIN", "USER"],
// ^? error here
},
},
})
// Server Side
export const auth = betterAuth({
user: {
fields: {
role: ["ADMIN", "USER"],
// ^? error here
},
},
user: {
role: ["ADMIN", "USER"],
// ^? error here
},
user: {
additionalFields: {
role: ["ADMIN", "USER"],
// ^? error here
},
},
})
bekacru
bekacru3mo ago
oh wait sorry
const auth = betterAuth({
user: {
role: {
type: ["ADMIN", "USER"]
}
}
})
const auth = betterAuth({
user: {
role: {
type: ["ADMIN", "USER"]
}
}
})
Mattèo
MattèoOP3mo ago
No problem, but the TS error is still appearing
user: {
role: {
// ^? error here
type: ["ADMIN", "USER"]
}
}
user: {
role: {
// ^? error here
type: ["ADMIN", "USER"]
}
}
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
Mattèo
MattèoOP3mo ago
thanks, this implementation kind of work, but the Prisma adapter creates a String[] instead of a reference to an enum.
bekacru
bekacru3mo ago
you should this instead. It should be defined under addittional fields
user: {
additionalFields: {
role: {
type: ["ADMIN", "USER"]
}
}
}
user: {
additionalFields: {
role: {
type: ["ADMIN", "USER"]
}
}
}
and the cli doesn't currently support generating enum values
Mattèo
MattèoOP3mo ago
It's working like that thanks ! (Don't know why undefined is a possible value but that's enough I think)
No description
bekacru
bekacru3mo ago
undefined is there cause session it self could be undefined
Mattèo
MattèoOP3mo ago
oh yes I see Thanks a lot for your help @bekacru @Phantom ! ✅✅✅ With Prisma generated client
user: {
additionalFields: {
role: {
type: Object.values(Role),
default: ["USER"],
required: true,
},
},
},
user: {
additionalFields: {
role: {
type: Object.values(Role),
default: ["USER"],
required: true,
},
},
},
codecret | Software Engineer
how do you navigate from login page ? how do you obtain the role (user or admin) using sign-in method and redirect to the appropriate page according to role? for example when i click on login page the data returned doesnt contain and role object

Did you find this page helpful?