Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

167
Views
Next-Auth Admin Permissions

I would like to implement permissions authorisation in my nextjs app, which uses nextauth and Google oAuth with mongodb for authentication. I am finding issues in assigning user roles on login, for example 'User' or 'Admin' so some routes may be protected. Below is my [...next-auth].js file.

import NextAuth from 'next-auth';
import Providers from 'next-auth/providers';

export default(req, res) => NextAuth(req, res, {
  session: {
    jwt: true,
  },
  providers: [
    Providers.Google({
      clientId: process.env.GOOGLE_IDENTITY,
      clientSecret: process.env.GOOGLE_SECRET_KEY,
    }),
  ],
  callbacks: {
    async jwt(token, user, account, profile, isNewUser) {
      if (account?.accessToken) {
        token.accessToken = account.accessToken
      }
      if (user?.roles) {
        token.roles = user.roles
      }
      return token
    },
    async session(session, token) {
      if(token?.accessToken) {
        session.accessToken = token.accessToken
      }
      if (token?.roles) {
        session.user.roles = token.roles
      }
      return session
    }
  }
});

Below is my mongoose model.

import mongoose from 'mongoose';

const UserSchema = new mongoose.Schema({
    name: {
        type: String,
    },
    email: {
        type: String,
    },
    permissions: {
        type: String,
        default: 'User'
    }
});

module.exports = mongoose.models.User || mongoose.model('User', UserSchema)

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!