5bed435176
* Remove getInitialProps * added swr * Update examples/with-cookie-auth-fauna/pages/profile.js Co-authored-by: Luis Alvarez D. <luis@vercel.com> * Update examples/with-cookie-auth-fauna/pages/profile.js Co-authored-by: Luis Alvarez D. <luis@vercel.com> * no longer needed * new .env support * redirect if not found * always use latest next version * no longer needed * Updated readme * Updated profile page * Fixed readme and added deploy button Co-authored-by: Luis Alvarez D. <luis@vercel.com>
38 lines
806 B
JavaScript
38 lines
806 B
JavaScript
import { useEffect } from 'react'
|
|
import Router from 'next/router'
|
|
|
|
export const login = ({ email }) => {
|
|
Router.push('/profile')
|
|
}
|
|
|
|
export const logout = async () => {
|
|
await fetch('/api/logout')
|
|
|
|
window.localStorage.setItem('logout', Date.now())
|
|
|
|
Router.push('/login')
|
|
}
|
|
|
|
export const withAuthSync = (Component) => {
|
|
const Wrapper = (props) => {
|
|
const syncLogout = (event) => {
|
|
if (event.key === 'logout') {
|
|
console.log('logged out from storage!')
|
|
Router.push('/login')
|
|
}
|
|
}
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('storage', syncLogout)
|
|
|
|
return () => {
|
|
window.removeEventListener('storage', syncLogout)
|
|
window.localStorage.removeItem('logout')
|
|
}
|
|
}, [])
|
|
|
|
return <Component {...props} />
|
|
}
|
|
|
|
return Wrapper
|
|
}
|