'How to expires session automatically after 1 hour in nextjs auth?

I am trying to expire the session automatically after 1 hour without success.

Currently, I am trying to set up a custom session.expires in session callback based on backend JWT token:

        async session({ session, token }) {
            const userId = token.sub

            const user = [...logic to populate the 'user' variable]

            session.accessToken = token.accessToken as string
            const payload: { exp: number } = jwt_decode(session.accessToken as string)

            session.user = user
            session.expires = new Date(payload.exp * 1000).toISOString()
            return session
        },

If I keep the browser window open after login, after 1 hour the user seems to disconnect because you can see that the object received from the endpoint "/api/auth/session" is empty, however, when reloading the page the session is repopulated with the expires property in the past.

{
    "user":{},
    "expires":"2022-05-19T07:21:07.000Z",
    "accessToken":"ACCESS_TOKEN_GOES_HERE"}

PS: The "user" and "accessToken" properties are correctly populated. I omitted it for security reasons.



Solution 1:[1]

Could you try adding allow the Access-Control-Allow-Headers: Origin and see if that helps you ?

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BsGhaz