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'17mo ago
barry
barry17mo 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'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
I changed thta but still get these errors
'tonyyprints'
'tonyyprints'17mo ago
barry
barry17mo ago
reload ts server
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
I did 😭 lemme restart my ide yea the issue still stands
barry
barry17mo ago
what if you console log session and see what it includes for you
'tonyyprints'
'tonyyprints'17mo ago
sure
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
it's printing out role but giving it null
barry
barry17mo ago
do you have a role?
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
yikes does this not work lol
barry
barry17mo ago
you did db push?
'tonyyprints'
'tonyyprints'17mo ago
yea I did
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo 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
barry17mo ago
give me a minute ima try doing role stuff myself
'tonyyprints'
'tonyyprints'17mo ago
I think I found the issue trying my solution rn nvm didin't work smh
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
barry
barry17mo 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'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
ah I didn't do this part that worked thanks 🙂 any chance you know why this error occurs?>
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
'tonyyprints'
'tonyyprints'17mo ago
when I use an enum for the default value of role
barry
barry17mo ago
🤷
'tonyyprints'
'tonyyprints'17mo ago
all good, thanks 🙏
barry
barry17mo ago
have a good time making what you making
'tonyyprints'
'tonyyprints'17mo ago
appreciate it 🙏