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

fix(examples): use hooks in with-cookie-auth example #8729

Merged
merged 2 commits into from
Sep 16, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 43 additions & 57 deletions examples/with-cookie-auth/utils/auth.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,14 @@
import { Component } from 'react'
import { useEffect } from 'react'
import Router from 'next/router'
import nextCookie from 'next-cookies'
import cookie from 'js-cookie'

function login ({ token }) {
export const login = ({ token }) => {
cookie.set('token', token, { expires: 1 })
Router.push('/profile')
}

function logout () {
cookie.remove('token')
// to support logging out from all windows
window.localStorage.setItem('logout', Date.now())
Router.push('/login')
}

// Gets the display name of a JSX component for dev tools
const getDisplayName = Component =>
Component.displayName || Component.name || 'Component'

function withAuthSync (WrappedComponent) {
return class extends Component {
static displayName = `withAuthSync(${getDisplayName(WrappedComponent)})`

static async getInitialProps (ctx) {
const token = auth(ctx)

const componentProps =
WrappedComponent.getInitialProps &&
(await WrappedComponent.getInitialProps(ctx))

return { ...componentProps, token }
}

constructor (props) {
super(props)

this.syncLogout = this.syncLogout.bind(this)
}

componentDidMount () {
window.addEventListener('storage', this.syncLogout)
}

componentWillUnmount () {
window.removeEventListener('storage', this.syncLogout)
window.localStorage.removeItem('logout')
}

syncLogout (event) {
if (event.key === 'logout') {
console.log('logged out from storage!')
Router.push('/login')
}
}

render () {
return <WrappedComponent {...this.props} />
}
}
}

function auth (ctx) {
export const auth = ctx => {
const { token } = nextCookie(ctx)

/*
Expand All @@ -81,4 +28,43 @@ function auth (ctx) {
return token
}

export { login, logout, withAuthSync, auth }
export const logout = () => {
cookie.remove('token')
// to support logging out from all windows
window.localStorage.setItem('logout', Date.now())
Router.push('/login')
}

export const withAuthSync = WrappedComponent => {
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')
}
}, [null])

return <WrappedComponent {...props} />
}

Wrapper.getInitialProps = async ctx => {
const token = auth(ctx)

const componentProps =
WrappedComponent.getInitialProps &&
(await WrappedComponent.getInitialProps(ctx))

return { ...componentProps, token }
}

return Wrapper
}