# User info not refreshed on getSession from nextjs sdk

**URL:** <https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779>\
**Category:** Get Help\
**Tags:** login-experience, new-universal-login-experience\
**Created:** [April 25, 2024, 1:45pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779 "2024-04-25T13:45:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![adriano](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/adriano/32/49981_2.png) [@adriano](https://community.auth0.com/u/adriano)\
**Post date:** [April 25, 2024, 1:45pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/1 "2024-04-25T13:45:28Z")

</div>

I’m using auth0 universal login with nextjs-auth0 SDK to manage authentication in my app.

If a User object has its information updated whilst the user is already logged in, that information will remain stale and the getSession method from the SDK will not update it.  
To fix this, I call the ‘/userinfo’ endpoint as an workaround to get the user refreshed data, but sometimes I’m getting rate-limited. ( I do this in the middleware of my app - code below )

 ![auth0](https://global.discourse-cdn.com/auth0/original/3X/f/9/f961dd8efb851f13cc4b1ca29a268461928f718f.png)

I’ve deployed my app on CF pages, and there’s some issues while using that and fetch API cache for data management, so I have implemented caching with an timeout of 30 seconds as can be seen here:

 ![auth02](https://global.discourse-cdn.com/auth0/original/3X/7/7/77fcc77914b281e8e15ac55c2703f013fe589d71.png)

Is there another way to work around this to refresh the user data?

---

<div class="post-metadata">

**Author:** ![stefan6](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/stefan6/32/51003_2.png) [@stefan6](https://community.auth0.com/u/stefan6)\
**Post date:** [April 29, 2024, 10:43am UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/2 "2024-04-29T10:43:06Z")

</div>

I am also running into this issue, any ideas?

---

<div class="post-metadata">

**Author:** ![leomc](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/leomc/32/52764_2.png) [@leomc](https://community.auth0.com/u/leomc)\
**Post date:** [April 30, 2024, 2:40pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/3 "2024-04-30T14:40:55Z")

</div>

This also happened with me, I’m now using your workaround but I get rate limited still ☹

---

<div class="post-metadata">

**Author:** ![devbydixon](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/devbydixon/32/59388_2.png) [@devbydixon](https://community.auth0.com/u/devbydixon)\
**Post date:** [August 24, 2024, 10:34pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/5 "2024-08-24T22:34:36Z")

</div>

The following approach has worked for me. This is NextJS middleware for app routes :  
@leomc @stefan6 @adriano

```auto
// middleware.js
import { withMiddlewareAuthRequired, getSession, handleProfile } from '@auth0/nextjs-auth0/edge';
import { NextResponse } from "next/server";

export default withMiddlewareAuthRequired(async function middleware(req) {
    const res = NextResponse.next();

    try {

        // This fixed it
        // @ts-ignore 
        await handleProfile(req, res, {
            refetch: true,
        })

        const session = await getSession(req, res);

        console.log('Email verified status:', session?.user.email_verified);

        if (!session) {
            console.log('No session found');
            return NextResponse.redirect(new URL('/', req.url));
        }

        if (!session.user.email_verified) {
            console.log('Email not verified');
            return NextResponse.redirect(new URL('/verify-email', req.url));
        }

        return res;
    } catch (error) {
        console.error('Error in middleware:', error);
        return NextResponse.redirect(new URL('/error', req.url));
    }
});

// Matcher config remains the same

// Specify the paths where this middleware should apply
...

```

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [August 26, 2024, 3:14pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/6 "2024-08-26T15:14:37Z")

</div>

Thanks for sharing @devbydixon !

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [September 9, 2024, 3:15pm UTC](https://community.auth0.com/t/user-info-not-refreshed-on-getsession-from-nextjs-sdk/132779/7 "2024-09-09T15:15:26Z")

</div>

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