Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
96090655
Commit
96090655
authored
Sep 16, 2019
by
Tommy Ljungberg
Committed by
Tim Neutkens
Sep 16, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix(examples): use hooks in with-cookie-auth example (#8729)
parent
583b4677
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
43 additions
and
57 deletions
+43
-57
auth.js
examples/with-cookie-auth/utils/auth.js
+43
-57
No files found.
examples/with-cookie-auth/utils/auth.js
View file @
96090655
import
{
Componen
t
}
from
'react'
import
{
useEffec
t
}
from
'react'
import
Router
from
'next/router'
import
Router
from
'next/router'
import
nextCookie
from
'next-cookies'
import
nextCookie
from
'next-cookies'
import
cookie
from
'js-cookie'
import
cookie
from
'js-cookie'
function
login
({
token
})
{
export
const
login
=
({
token
})
=>
{
cookie
.
set
(
'token'
,
token
,
{
expires
:
1
})
cookie
.
set
(
'token'
,
token
,
{
expires
:
1
})
Router
.
push
(
'/profile'
)
Router
.
push
(
'/profile'
)
}
}
function
logout
()
{
export
const
auth
=
ctx
=>
{
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
)
{
const
{
token
}
=
nextCookie
(
ctx
)
const
{
token
}
=
nextCookie
(
ctx
)
/*
/*
...
@@ -81,4 +28,43 @@ function auth (ctx) {
...
@@ -81,4 +28,43 @@ function auth (ctx) {
return
token
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment