Skip to content

Commit

Permalink
Removed the custom getInitialProps from withAuthSync
Browse files Browse the repository at this point in the history
  • Loading branch information
lfades committed Jan 14, 2020
1 parent 3718552 commit e944e32
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions examples/with-cookie-auth-fauna/utils/auth.js
@@ -1,33 +1,11 @@
import { useEffect } from 'react'
import Router from 'next/router'
import cookie from 'cookie'
import { FAUNA_SECRET_COOKIE } from './fauna-auth'

export const login = ({ email }) => {
window.localStorage.setItem('loggedInUser', email)
Router.push('/profile')
}

export const auth = ctx => {
if (ctx.req) {
var cookies = cookie.parse(ctx.req.headers.cookie ?? '')
var faunaSecret = cookies[FAUNA_SECRET_COOKIE]
if (!faunaSecret) {
// If `ctx.req` is available it means we are on the server.
ctx.res.writeHead(302, { Location: '/login' })
ctx.res.end()
}
return faunaSecret
} else {
if (!window.localStorage.getItem('loggedInUser')) {
Router.push('/login')
}
// The user is logged in, the page will perform it's own
// authed API call.
return null
}
}

export const logout = async () => {
await fetch('/api/logout')

Expand All @@ -37,7 +15,7 @@ export const logout = async () => {
Router.push('/login')
}

export const withAuthSync = WrappedComponent => {
export const withAuthSync = Component => {
const Wrapper = props => {
const syncLogout = event => {
if (event.key === 'logout') {
Expand All @@ -55,17 +33,11 @@ export const withAuthSync = WrappedComponent => {
}
}, [])

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

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

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

return { ...componentProps, token }
if (Component.getInitialProps) {
Wrapper.getInitialProps = Component.getInitialProps
}

return Wrapper
Expand Down

0 comments on commit e944e32

Please sign in to comment.