Skip to content

Commit

Permalink
feat: replace blur/focus event to visibility API for getSession (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
lnikell authored and balazsorban44 committed Feb 1, 2021
1 parent ca06560 commit b1a99ec
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,20 @@ if (typeof window !== 'undefined') {
}
})

// Listen for window focus/blur events
window.addEventListener('focus', async (event) => __NEXTAUTH._getSession({ event: 'focus' }))
window.addEventListener('blur', async (event) => __NEXTAUTH._getSession({ event: 'blur' }))
// Listen for document visibilitychange events
let hidden, visibilityChange
if (typeof document.hidden !== 'undefined') { // Opera 12.10 and Firefox 18 and later support
hidden = 'hidden'
visibilityChange = 'visibilitychange'
} else if (typeof document.msHidden !== 'undefined') {
hidden = 'msHidden'
visibilityChange = 'msvisibilitychange'
} else if (typeof document.webkitHidden !== 'undefined') {
hidden = 'webkitHidden'
visibilityChange = 'webkitvisibilitychange'
}
const handleVisibilityChange = () => !document[hidden] && __NEXTAUTH._getSession({ event: visibilityChange })
document.addEventListener('visibilitychange', handleVisibilityChange, false)
}
}

Expand Down

0 comments on commit b1a99ec

Please sign in to comment.