-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat(login): add hook for login + interceptor #22
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,43 @@ | ||
import React from 'react'; | ||
import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; | ||
import React, { useState } from 'react'; | ||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; | ||
import './App.scss'; | ||
import BaseLayout from './components/layouts/BaseLayout'; | ||
import IpfsList from './pages/ipfs/IpfsList'; | ||
import { storageNames } from './config'; | ||
import UserContext from './hooks/UserContext'; | ||
import Ipfs from './pages/ipfs/Ipfs'; | ||
|
||
import './App.scss'; | ||
import IpfsList from './pages/ipfs/IpfsList'; | ||
import Login from './pages/Login/Login'; | ||
import { ProtectedRoute } from './shared/ProtectedRoute'; | ||
|
||
function App(): JSX.Element { | ||
// Handle user state | ||
const accessToken = localStorage.getItem(storageNames.user) || ''; | ||
const [isLoggedIn, setLoggedIn] = useState(!!accessToken); | ||
|
||
// Handle user login by setting the storage and state | ||
const login = (token: string): void => { | ||
setLoggedIn(true); | ||
localStorage.setItem(storageNames.user, token); | ||
}; | ||
|
||
// Handle user logout | ||
const logout = (): void => { | ||
setLoggedIn(false); | ||
localStorage.removeItem(storageNames.user); | ||
}; | ||
|
||
return ( | ||
<Router> | ||
<BaseLayout> | ||
<Switch> | ||
<Route path="/ipfs/:hash" component={Ipfs} /> | ||
<Route path={['/', '/ipfs']} component={IpfsList} /> | ||
</Switch> | ||
</BaseLayout> | ||
</Router> | ||
<UserContext.Provider value={{ isLoggedIn, login, logout }}> | ||
<Router> | ||
<BaseLayout> | ||
<Switch> | ||
<Route path="/login" component={Login} /> | ||
<ProtectedRoute path="/ipfs/:hash" component={Ipfs} /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering if we couldn't just handle this at a higher level, defining all the routes as protected seems overkill when we only have one route for not logged in ( /login ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think both are good. But to make code cleaner, prefer to have 2 routers. |
||
<ProtectedRoute path={['/', '/ipfs']} component={IpfsList} /> | ||
</Switch> | ||
</BaseLayout> | ||
</Router> | ||
</UserContext.Provider> | ||
); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as NavigationBar } from './NavigationBar'; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const apis = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thought it would be good to handle everything in a config folder @NiiCoder |
||
// TODO: Replace this by the BE api urls we would be using. Probably storing this in a config file | ||
url: 'https://jsonplaceholder.typicode.com', | ||
timeout: 2 * 60 * 1000, // 2 minutes timeout | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { apis } from './api'; | ||
export { storageNames } from './storage-names'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const storageNames = { | ||
user: 'pl11', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is just random "paralink11" just so it's not easy to guess when looking |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { createContext } from 'react'; | ||
|
||
interface UserContextState { | ||
isLoggedIn: boolean; | ||
login: (token: string) => void; | ||
logout: () => void; | ||
} | ||
|
||
const UserContext = createContext({} as UserContextState); | ||
|
||
export default UserContext; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default as UserContext } from './UserContext'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
$elevation-card: 0 0 1px 0 rgba(8, 11, 14, 0.6), 0 16px 16px -1px rgba(8, 11, 14, 0.1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am gonna create a ticket to define a theme & variables to use accross the app & a global style. Right now this is using a lot of "custom" things. But if we can come up with a global styling ( for inputs, buttons, etc. ) this would make it easier |
||
$color-secondary: #e1e4e8; | ||
$error-color: red; | ||
|
||
.login { | ||
&__card { | ||
max-width: 400px; | ||
margin: auto; | ||
box-shadow: $elevation-card; | ||
padding: 30px; | ||
background: white; | ||
} | ||
|
||
&__title { | ||
font-size: 22px; | ||
line-height: 32px; | ||
font-weight: 500; | ||
text-align: center; | ||
margin: 8px 0 20px; | ||
font-weight: bold; | ||
} | ||
|
||
&__input-container { | ||
position: relative; | ||
margin-top: 20px; | ||
} | ||
|
||
&__input { | ||
border-radius: 100px; | ||
border: 1px solid $color-secondary; | ||
font-size: 14px; | ||
line-height: 24px; | ||
font-weight: 500; | ||
height: 40px; | ||
width: 100%; | ||
outline: none; | ||
padding: 0 15px; | ||
|
||
&--error { | ||
border-color: $error-color; | ||
} | ||
} | ||
|
||
&__input-error-msg { | ||
font-size: 12px; | ||
margin-left: 15px; | ||
color: $error-color; | ||
} | ||
|
||
&__input-icon { | ||
position: absolute; | ||
right: 10px; | ||
top: 50%; | ||
transform: translateY(-50%); | ||
background: transparent; | ||
border: none; | ||
outline: none; | ||
display: inline-block; | ||
padding: 10px; | ||
color: $color-secondary; | ||
} | ||
|
||
&__password-icon { | ||
color: black; | ||
cursor: pointer; | ||
} | ||
|
||
&__button { | ||
border-radius: 100px; | ||
font-size: 14px; | ||
line-height: 24px; | ||
background: black; | ||
color: white; | ||
padding: 0 10px; | ||
height: 40px; | ||
width: 100%; | ||
border: none; | ||
margin-top: 40px; | ||
cursor: pointer; | ||
padding: 0 20px; | ||
outline: none; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought this would be the best to handle the user state. Open to suggestions, if there is a better way to do it with React
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think saving user information in localStorage is not a safe way. We have to ask the user to save user info("Remember me" checkbox).
And for the states that need to be used in several places, prefer to use redux.