Skip to content

Commit

Permalink
feat: keep user logged in if they close the bro wser
Browse files Browse the repository at this point in the history
this fixes #91
  • Loading branch information
marianzburlea committed Jul 25, 2019
1 parent 55d084d commit 72330f3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/component/top-menu/top-menu.component.jsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,49 @@
import React, { useState } from 'react'
import React from 'react'
import { StyledTopMenu, StyledLink, StyledButton } from './top-menu.style'
import { WebInfoState } from '../web-info/web-info.context';
import { auth, GitHubProvider } from '../data/firebase';

const TopMenu = () => {
const { toggleChat, updateToggleChat } = WebInfoState()
const [ loggedIn, setLoggedIn ] = useState(false)
const { toggleChat, updateToggleChat, user, updateUser } = WebInfoState()

const handleToggleChat = () => {
updateToggleChat({ type: 'TOGGLE_CHAT' });
}

const handleLogInAndOut = () => {
if (loggedIn) {
if (user) {
auth.signOut();
updateUser({
type: 'USER_AUTHENTICATE',
user: null
})
}
else {
auth
.signInWithPopup(GitHubProvider)
.then(user => {
// console.log(user)
.then(({ user: { uid, displayName, photoURL }}) => {
updateUser({
type: 'USER_AUTHENTICATE',
user: {
uid,
displayName,
photoURL
}
})
})
.catch(error => {
// console.error(error)
})
}
}

const getLogInOutLabel = () => loggedIn ? 'Ba Bye' : 'Let me in!'
const getLogInOutLabel = () => user ? 'Ba Bye' : 'Let me in!'

return (
<StyledTopMenu>
<StyledLink to="/">Home</StyledLink>
<StyledLink to="/dashboard">Dashboard</StyledLink>
<div>{user && user.displayName}</div>
<button onClick={handleLogInAndOut}>
{getLogInOutLabel()}
</button>
Expand Down
1 change: 1 addition & 0 deletions src/component/user/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { userReducer, defaultUser } from './user.reducer'
11 changes: 11 additions & 0 deletions src/component/user/user.reducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const defaultUser = JSON.parse(window.localStorage.getItem('user'))

export const userReducer = (state = defaultUser, action) => {
switch(action.type) {
case 'USER_AUTHENTICATE':
window.localStorage.setItem('user', JSON.stringify(action.user))
return action.user
default:
return state
}
}
33 changes: 22 additions & 11 deletions src/component/web-info/web-info.context.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,35 @@ import React, { createContext, useContext, useReducer } from 'react'
import { courseListReducer } from '../course-panel/course-panel.reducer'
import { sectionListReducer } from '../course/section.reducer'
import { topMenuReducer, intialToggleMenu } from '../top-menu/top-menu.reducer'
import { userReducer, defaultUser } from '../user';

const WebInfoContext = createContext()

export const WebInfoProvider = ({ children }) => {
const [ courseList, updateCourseList ] = useReducer(courseListReducer, [])
const [ sectionList, updateSectionList ] = useReducer(sectionListReducer, [])
const [ toggleChat, updateToggleChat ] = useReducer(topMenuReducer, intialToggleMenu)
const [ user, updateUser ] = useReducer(userReducer, defaultUser)

return (
<WebInfoContext.Provider value={{
courseList,
sectionList,
updateCourseList,
updateSectionList,
toggleChat,
updateToggleChat
}}>
{children}
</WebInfoContext.Provider>
)}
<WebInfoContext.Provider value={
{
user,
updateUser,

courseList,
sectionList,

updateCourseList,
updateSectionList,

toggleChat,
updateToggleChat
}
}>
{children}
</WebInfoContext.Provider>
)
}

export const WebInfoState = () => useContext(WebInfoContext)

0 comments on commit 72330f3

Please sign in to comment.