updateUserMetaData - How can I update meta data from useUser without login/logout?

Hi there,

I’m trying to update the users meta data in my next.js application, utilising the management API. The meta data is being updated successfully, but the user has to logout and login again in order for the changes to be reflected.

How can I update the metadata without needing the user to log back in?

API Code:

const userHandler = async (req, res) => {
  const { body } = req;

  const session = await getSession(req, res);
  const id = session.user.sub;
  const accessToken = session.accessToken;

  console.log('id', session);

  try {
    const params = body;

    const currentUserManagementClient = new ManagementClient({
      token: accessToken,
      domain: process.env.AUTH0_ISSUER_BASE_URL.replace('https://', ''),
      scope: process.env.AUTH0_SCOPE,
    });

    await currentUserManagementClient.updateUserMetadata({ id }, params);

    res.status(200).json(user);
  } catch (err) {
    console.log(err);
    res.status(500).json({ statusCode: 500, message: err.message });
  }
};

session console log:

id Session {
  user: {
    'REMOVED': {
      activeSubscription: true,
      stripeCustomerId: 'REMOVED',
      tokens: 0
    },
    given_name: 'Ben',
    family_name: 'Hayward',
    nickname: 'ben',
    name: 'Ben Hayward',
    picture: 'REMOVED',
    locale: 'en',
    updated_at: '2023-06-13T22:13:01.146Z',
    email: 'ben@REMOVED.com',
    email_verified: true,
    sub: 'google-oauth2|111663802953217651283',
    sid: 'qj0a432ZrWBGbFjVHmUd5oASQTibCDLA'
  },

Many thanks,

Ben

Hey there @bhbenhayward welcome to the community!

Have you looked into silent authentication at all? The following topic goes into a similar use case as yours:

Keep us posted!

Hey @tyf ,

I tried to implement silent authentication via the ‘prompt none’ param, but I received a ‘login required’ error.

import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';

export default handleAuth({
  async login(req, res) {
    try {
      await handleLogin(req, res, {
        authorizationParams: {
          audience: `${process.env.AUTH0_ISSUER_BASE_URL}/api/v2/`,
          prompt: 'none',
          scope:
            'openid profile email user_metadata read:current_user create:current_user_metadata update:current_user_metadata',
        },
      });
    } catch (error) {
      res.status(error.status || 400).end(error.message);
    }
  },
});

Sorry for the delayed response here - Do you have MFA enabled?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.