I am trying to create a roled based login strategy using credentials. I was using nextauth roled-base login tutorial but it was not working.
/api/auth/[...nextauth.ts]
const authOptions: NextAuthOptions = {
providers: [
Credentials({
id: "credentials",
name: "Credentials",
credentials: {},
async authorize(credentials) {
const { email, password } = credentials as {
email: string;
password: string;
};
// perform login logic
// find user from db
if (email == "john@gmail.com" && password == "1234") {
return {
id: "1234",
name: "John Doe",
email: "john@gmail.com",
role: "admin",
};
}
throw new Error("Invalid credentials");
},
}),
],
callbacks: {
jwt: ({ token, user }) => {
console.log(token);
if (user) {token.id = user.id};
return token;
},
session: ({ session, token, user }) => {
if (token) {
session.id = token.id;
session.user.role = user.role; //not working
}
return session;
},
},
session: {
strategy: "jwt",
},
pages: {
signIn: "/login",
// error: '/auth/error',
// signOut: 'auth/signout'
},
secret: process.env.NEXT_PUBLIC_SECRET,
};
I thought of creating a custom adapter using nextauth adapter tutorial but it seems I can only define extra field for user if I am using OAuth provider. I can't seem to find any of the same in the documentation for credentials provider. My other possible solution is to use custom jwt sign in method instead of using NextAuth, but I can't seem to find a good example online.