trying to add role based authentication

I added a role parameter to my User schema, but it's still not catching it. Any one know why this may be?
35 Replies
'tonyyprints'
'tonyyprints'OP3y ago
barry
barry3y ago
TypeScript | NextAuth.js
NextAuth.js has its own type definitions to use in your TypeScript projects safely. Even if you don't use TypeScript, IDEs like VSCode will pick this up to provide you with a better developer experience. While you are typing, you will get suggestions about what certain objects/functions look like, and sometimes links to documentation, examples, ...
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
I changed thta but still get these errors
'tonyyprints'
'tonyyprints'OP3y ago
barry
barry3y ago
reload ts server
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
I did 😭 lemme restart my ide yea the issue still stands
barry
barry3y ago
what if you console log session and see what it includes for you
'tonyyprints'
'tonyyprints'OP3y ago
sure
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
it's printing out role but giving it null
barry
barry3y ago
do you have a role?
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
yikes does this not work lol
barry
barry3y ago
you did db push?
'tonyyprints'
'tonyyprints'OP3y ago
yea I did
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
just did it again lemme try again lol yea it's still being funny I don't think I can do the default thing like that
barry
barry3y ago
give me a minute ima try doing role stuff myself
'tonyyprints'
'tonyyprints'OP3y ago
I think I found the issue trying my solution rn nvm didin't work smh
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
barry
barry3y ago
works for me lol
// src/types/next-auth.d.ts
import { type DefaultSession, type DefaultUser } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
role: string;
} & DefaultSession["user"];
}

interface User extends DefaultUser {
role: string;
}
}
// src/types/next-auth.d.ts
import { type DefaultSession, type DefaultUser } from "next-auth";

declare module "next-auth" {
/**
* Returned by `useSession`, `getSession` and received as a prop on the `SessionProvider` React Context
*/
interface Session {
user?: {
id: string;
role: string;
} & DefaultSession["user"];
}

interface User extends DefaultUser {
role: string;
}
}
// src/pages/api/[...next-auth].ts
import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db";

export const authOptions: NextAuthOptions = {
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.role = user.role;
}

return session;
},
},
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
};

export default NextAuth(authOptions);
// src/pages/api/[...next-auth].ts
import NextAuth, { type NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";
import { PrismaAdapter } from "@next-auth/prisma-adapter";

import { env } from "../../../env/server.mjs";
import { prisma } from "../../../server/db";

export const authOptions: NextAuthOptions = {
callbacks: {
session({ session, user }) {
if (session.user) {
session.user.id = user.id;
session.user.role = user.role;
}

return session;
},
},
adapter: PrismaAdapter(prisma),
providers: [
DiscordProvider({
clientId: env.DISCORD_CLIENT_ID,
clientSecret: env.DISCORD_CLIENT_SECRET,
}),
],
};

export default NextAuth(authOptions);
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
ah I didn't do this part that worked thanks 🙂 any chance you know why this error occurs?>
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
'tonyyprints'
'tonyyprints'OP3y ago
when I use an enum for the default value of role
barry
barry3y ago
🤷
'tonyyprints'
'tonyyprints'OP3y ago
all good, thanks 🙏
barry
barry3y ago
have a good time making what you making
'tonyyprints'
'tonyyprints'OP3y ago
appreciate it 🙏

Did you find this page helpful?