Skip to content

Commit

Permalink
Uses localStorage to extract authentication status
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinczenko committed Jun 10, 2020
1 parent a8e4532 commit 897e58a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 10 deletions.
9 changes: 8 additions & 1 deletion workspaces/homepage/src/components/header/Header.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ const Wrapper = styled.div({
// opacity: '0.84'
})

const logout = () => {
const isSSR = typeof window === 'undefined'
if (!isSSR) {
return localStorage.setItem('authenticated', false)
}
}

const Header = () => (
<HyperWrapper>
<Wrapper>
Expand All @@ -67,7 +74,7 @@ const Header = () => (
<MenuLink
css={{
margin: '5px 20px'
}} to='/' state={{ authenticated: false }}
}} onClick={logout} to='/login'
>Logout
</MenuLink>
{/* <MenuLink
Expand Down
22 changes: 17 additions & 5 deletions workspaces/homepage/src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import React, { useEffect } from 'react'
import React, { useEffect, useState } from 'react'
import { graphql, navigate } from 'gatsby'

// import { PrivateRoute } from 'src/components/private-route'
import { Crypton } from 'src/components/crypton'

const isAuthenticated = state => {
return (state && state.authenticated)
const isAuthenticated = () => {
const isSSR = typeof window === 'undefined'
if (!isSSR) {
return localStorage.getItem('authenticated') === 'true'
}
return false
}

const Home = ({ data, location }) => {
const [authenticated, setAuthenticated] = useState(false)
console.log('************* Welcome to CRYPTON *************')

useEffect(() => {
if (!isAuthenticated(location.state)) {
if (!isAuthenticated()) {
setAuthenticated(false)
navigate('/login')
} else {
setAuthenticated(true)
}
}, [location.state])
}, [])

if (!authenticated) {
return null
}

return (
<Crypton data={data} />
Expand Down
36 changes: 32 additions & 4 deletions workspaces/homepage/src/pages/login.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
import React, { useCallback } from 'react'
import React, { useCallback, useState, useEffect } from 'react'
import { navigate } from 'gatsby'

import { FadingValueBox } from 'src/components/animations'

const isAuthenticated = () => {
const isSSR = typeof window === 'undefined'
if (!isSSR) {
return localStorage.getItem('authenticated') === 'true'
}
return false
}

const Login = ({ data, location }) => {
const [authenticated, setAuthenticated] = useState(true)

useEffect(() => {
if (!isAuthenticated()) {
setAuthenticated(false)
} else {
navigate('/')
setAuthenticated(true)
}
}, [])
const onClick = useCallback(() => {
navigate('/', { state: { authenticated: true } })
const isSSR = typeof window === 'undefined'
if (!isSSR) {
localStorage.setItem('authenticated', true)
}
navigate('/')
}, [])

if (authenticated) {
return null
}

return (
<div>
<FadingValueBox alignItems='center'>
<button onClick={onClick}>Login...</button>
</div>
</FadingValueBox>
)
}

Expand Down

0 comments on commit 897e58a

Please sign in to comment.