rsnext/examples/with-cookie-auth-fauna/utils/auth.js
Todor Totev 5bed435176
[Examples] Remove getInitialProps from with-cookie-auth-fauna (#13887)
* 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>
2020-06-20 10:59:53 -05:00

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
}