In the example above, we use isAuthenticated to decide if we should render the page. If you do not need this functionality, calling useAuth(true) should be sufficient. This hook will log out the user when his token expires while being on that page.
Thanks Mateusz! I got it, but my boss wants me to call auto logout inside nextauth.js.
`
events: {
session: async ({ session }) => {
// if RefreshAccessTokenError then logout
if (session?.error === 'RefreshAccessTokenError') {
signOut()
}
// if refreshTokenExpiresIn then signOut
if (
session?.refreshTokenExpiresIn &&
Date.now() > new Date(session.refreshTokenExpiresIn).getTime()
) {
console.log('I am logging out')
signOut()
}
}
}
`
I kanda figure one way out which is using session event like below. He don't want to call hooks on pages. However with event I am getting a error message also like error - unhandledRejection: ReferenceError: window is not defined .That's why it's a bit complicated in my case!
You are getting "window is not defined" because you are trying to call a signOut function (which requires a browser window) in a session callback inside [...nextauth].js.
[...nextauth].js lives in pages/api/auth, and pages/api in Next.js are the server functions. You can look up the documentation on signOut.
If you want to logout a user from the session callback, you should probably use POST /api/auth/signout. Call it as you would a regular endpoint. This is used by signOut() internally.
I'm not sure if it's gonna work, but it's worth exploring. Let me know how you did :)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can call the
useAuthhook directly from the page (pages folder).In the example above, we use
isAuthenticatedto decide if we should render the page. If you do not need this functionality, callinguseAuth(true)should be sufficient. This hook will log out the user when his token expires while being on that page.Thanks Mateusz! I got it, but my boss wants me to call auto logout inside nextauth.js.
`
events: {
session: async ({ session }) => {
// if RefreshAccessTokenError then logout
if (session?.error === 'RefreshAccessTokenError') {
signOut()
}
}
`
I kanda figure one way out which is using session event like below. He don't want to call hooks on pages. However with event I am getting a error message also like
error - unhandledRejection: ReferenceError: window is not defined.That's why it's a bit complicated in my case!You are getting "window is not defined" because you are trying to call a
signOutfunction (which requires a browser window) in asessioncallback inside[...nextauth].js.[...nextauth].jslives inpages/api/auth, andpages/apiin Next.js are the server functions. You can look up the documentation on signOut.If you want to logout a user from the session callback, you should probably use POST /api/auth/signout. Call it as you would a regular endpoint. This is used by
signOut()internally.I'm not sure if it's gonna work, but it's worth exploring. Let me know how you did :)