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

256
Views
Email provider: How to preserve value in `signIn` callback between sending email and verification calls?

I want to implement role based authentication in my app.
After researching for a while I found a simple solution - to pass role to signIn function

signIn('email', { email, role: 'user' });

Then I can unpack it from req's body

import NextAuth from 'next-auth'
export default async function auth(req, res) {
  return NextAuth(req, res, {
    // providers, adapters, etc.
    callbacks: {
      signIn: async ({ user }) => {
        const { body: { role } } = req;

        if (role) {
          user.role = role;
        }
        return true;
      },
      session: async ({ user, session }) => {
        session.user = {...session.user, ...user };
        return session
      }
    }
  }
});

Or so I thought. Magic link sign in flow has two steps: sending email and confirming it.

The role I provide to signIn function is only available during the first step and saving user to users collection only happens after user confirms email, so when user confirms email there's no role.

I tried storing role in cookies

if (role) res.setHeader('Set-Cookie', 'user-role=' + role + ';Max-Age=600');

But cookies get overriden after I confirm email and the role is lost.
I don't want to create additional collections and/or records in my database.

How can I preserve role inside signIn callback without storing it in a separate database collection? Maybe there's some other solution you can think of?

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!