Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Session not being persisted when I close the browser #1131

Closed
pbteja1998 opened this issue Jan 16, 2021 · 16 comments
Closed

Session not being persisted when I close the browser #1131

pbteja1998 opened this issue Jan 16, 2021 · 16 comments
Labels
adapters Changes related to the core code concerning database adapters question Ask how to do something or how something works

Comments

@pbteja1998
Copy link

Hi, Is there a way to persist the session? As soon as I close the browser, the user is being logged out.

When I saw the cookies that are set, I see Expires/Max-Age value of next-auth.session-token set to Session. I am using the default configuration. By default, I assumed the max-age will be set to 30 days as per documentation. But that doesn't seem to be the case. Am I missing something?

@pbteja1998 pbteja1998 added the question Ask how to do something or how something works label Jan 16, 2021
@balazsorban44
Copy link
Member

balazsorban44 commented Jan 16, 2021

Hi there, could you create a reproduction for this, cause what you are saying does not hold up:
image

The session token WILL by default expire in 30 days. In addition, any time the /api/auth/session endpoint is invoked, the session will be updated to expire the next 30 days from the moment the session endpoint's response is received by the browser.

@pbteja1998
Copy link
Author

pbteja1998 commented Jan 16, 2021

@balazsorban44
Hi, you can take a look at https://github.com/pbteja1998/nextjs-starter

Screenshot 2021-01-17 at 4 07 43 AM

This might be related to the new Fauna adapter that is in the works.

Here is my fauna adapter file - https://github.com/pbteja1998/nextjs-starter/blob/master/src/adapters/fauna/index.ts

Copied directly from #708


EDIT:
You can find the demo of the site at https://next-starter.bhanuteja.dev

Edit2:
In the fauna db, the expiresAt is being set correctly. The only problem is with the cookie - in which the maxAge is being set to Session.

@balazsorban44
Copy link
Member

Yeah, I guess it is some kind of calculation issue on the session age. I think the session age is somehow set to a lower value than the current date, in that case, the max-age will be set to Session. I would start debugging where this goes wrong.

@pbteja1998
Copy link
Author

Where is this calculation being done? Is it in the fauna adapter file or in some other file in next-auth codebase?

@balazsorban44
Copy link
Member

balazsorban44 commented Jan 16, 2021

In the adapter, the one you just linked. I would check the updateSession function to begin with.

Also, here is the code being called when calling the /api/auth/session endpoint:

const { getUser, getSession, updateSession } = await adapter.getAdapter(options)
const session = await getSession(sessionToken)
if (session) {
// Trigger update to session object to update session expiry
await updateSession(session)
const user = await getUser(session.userId)
// By default, only exposes a limited subset of information to the client
// as needed for presentation purposes (e.g. "you are logged in as…").
const defaultSessionPayload = {
user: {
name: user.name,
email: user.email,
image: user.image
},
accessToken: session.accessToken,
expires: session.expires
}
// Pass Session through to the session callback
const sessionPayload = await callbacks.session(defaultSessionPayload, user)
// Return session payload as response
response = sessionPayload
// Set cookie again to update expiry
cookie.set(res, cookies.sessionToken.name, sessionToken, { expires: session.expires, ...cookies.sessionToken.options })
await dispatchEvent(events.session, { session: sessionPayload })
} else if (sessionToken) {
// If sessionToken was found set but it's not valid for a session then
// remove the sessionToken cookie from browser.
cookie.set(res, cookies.sessionToken.name, '', { ...cookies.sessionToken.options, maxAge: 0 })

As you can see, this should update the cookie expiry whenever session is accessed. In case the sessionToken was found but invalid, the cookie will be removed. So I would also check the getSession function.

(Hint: add debug: true to your NextAuth options to log more information while debugging)

@pbteja1998
Copy link
Author

pbteja1998 commented Jan 16, 2021

Ok, thanks. Will try to debug. If you are able to figure it out before me, please update it here.

@balazsorban44 balazsorban44 added the adapters Changes related to the core code concerning database adapters label Jan 16, 2021
@pbteja1998
Copy link
Author

Found it in the below line.
https://github.com/pbteja1998/nextjs-starter/blob/master/src/adapters/fauna/index.ts#L284

It should be session.data.expires not session.expires

@pbteja1998
Copy link
Author

Changing it like above did not fix it.

@pbteja1998
Copy link
Author

@balazsorban44 I was able to fix it. Thanks for pointing out the relevant code.

Here is the diff:
pbteja1998/nextjs-starter@9d885ae

@balazsorban44
Copy link
Member

Awesome!

@balazsorban44
Copy link
Member

@ndom91 Should this be dealt with in the Fauna adapter we introduced in #708?

@pbteja1998
Copy link
Author

@ndom91 Should this be dealt with in the Fauna adapter we introduced in #708?

Tag me if necessary. I am happy to open up PR with these additions.

@ndom91
Copy link
Member

ndom91 commented Jan 16, 2021

@balazsorban44 yeah I'd say update that file in the fauna adapter. This only affects that adapter, right?

Good work @pbteja1998!

@balazsorban44
Copy link
Member

As far as I know. (haven't tested other adapters). @pbteja1998 then we would be glad to accept a PR from you to fix this! 🙂

@pbteja1998
Copy link
Author

I'm on it.

@pbteja1998
Copy link
Author

@balazsorban44 Opened the PR at #1134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
adapters Changes related to the core code concerning database adapters question Ask how to do something or how something works
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants