This repository has been archived by the owner on Aug 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Install redux thunk * Rewrite login action to thunk * Add WIP on refresh token api client integration * Add WIP on refreshing token * Change config * Finish api client * Add sample error boundary * Add sample error handles * Rewrite logout to thunks * Add logout functionality * Fix Error Boundary * Move global styles, change logout logic * Update yarn.lock * Add README * Move tokens logic into login action * SignUp logic * Add video link
- Loading branch information
Showing
21 changed files
with
354 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Authentication & Routing in depth | ||
|
||
- [Presentation](https://docs.google.com/presentation/d/1JCvCloP-MmZAFA6oY0tCUs6jVs_pIzUavwJMl5UYtPY/edit) | ||
- [Video](https://www.youtube.com/watch?v=V5URcw_KYFM) | ||
|
||
## Homework | ||
|
||
- move logic to chosen async solution _(default: redux-thunks, advanced: redux-sagas, redux-observables)_ | ||
- handle refresh tokens | ||
- notify user about errors `server/network` and `success` _(product added to cart/deleted from cart/logged in)_ | ||
|
||
## Aditional info | ||
|
||
- To mock server responses `401/500` you can use https://www.charlesproxy.com/ | ||
- If you found homework too easy, please refer to `Up for the challenge?` slide for [problem inspiration](https://docs.google.com/presentation/d/1JCvCloP-MmZAFA6oY0tCUs6jVs_pIzUavwJMl5UYtPY/edit#slide=id.g56a65efc51_0_57) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { setRefreshToken, getRefreshToken } from '../../utils/refresh-token' | ||
import { setToken, getToken } from '../../utils/token' | ||
|
||
import config from '../../config' | ||
|
||
export const refreshCustomerToken = async () => { | ||
const refreshToken = getRefreshToken() | ||
const token = getToken() | ||
|
||
const response = await fetch(`${config.apiUrl}/oauth/token`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${token}`, | ||
}, | ||
body: JSON.stringify({ | ||
grant_type: 'refresh_token', | ||
refresh_token: refreshToken, | ||
client_id: config.clientId, | ||
client_secret: config.clientSecret, | ||
}), | ||
}) | ||
|
||
switch (response.status) { | ||
case 200: { | ||
const { refresh_token, access_token } = await response.json() | ||
setToken(access_token) | ||
setRefreshToken(refresh_token) | ||
|
||
return access_token | ||
} | ||
default: | ||
throw new Error('Cannot refresh customer token') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import React from 'react' | ||
import { toast } from 'react-toastify' | ||
|
||
export class ErrorBoundary extends React.Component { | ||
state = { | ||
error: false, | ||
} | ||
|
||
static getDerivedStateFromError() { | ||
return { | ||
error: true, | ||
} | ||
} | ||
|
||
componentDidCatch(error, info) { | ||
toast.error(`Error: ${error.message}`) | ||
console.error('Error boundary error', error, info) | ||
} | ||
|
||
render() { | ||
return this.state.error ? ( | ||
<h2> | ||
We are sorry! There was an error which we were not able to recover from. | ||
Please refresh the page | ||
</h2> | ||
) : ( | ||
this.props.children | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,37 @@ | ||
import React, { Fragment } from 'react' | ||
import { withRouter } from 'react-router-dom' | ||
import { connect } from 'react-redux' | ||
|
||
import * as customerActions from '../../store/customer/actions' | ||
import * as routes from '../../routes' | ||
|
||
import { removeToken } from '../../utils/token' | ||
import { removeCustomer } from '../../utils/customer' | ||
import { Wrapper, Header, HeaderSection, HeaderLink } from './styled' | ||
|
||
const Layout = ({ logout, isAuthenticated, history, children }) => { | ||
const handleLogout = () => { | ||
logout() | ||
removeToken() | ||
removeCustomer() | ||
history.push(routes.HOMEPAGE) | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<Header> | ||
<HeaderSection> | ||
<HeaderLink to={routes.PRODUCT_LIST}>All Products</HeaderLink> | ||
</HeaderSection> | ||
<HeaderSection> | ||
<HeaderLink to={routes.CART}>My Cart</HeaderLink>| | ||
{isAuthenticated ? ( | ||
<> | ||
<HeaderLink to={routes.ACCOUNT}>My Account</HeaderLink>| | ||
<HeaderLink as="button" onClick={handleLogout}> | ||
Logout | ||
</HeaderLink> | ||
</> | ||
) : ( | ||
<> | ||
<HeaderLink to={routes.LOGIN}>Log In</HeaderLink> | | ||
<HeaderLink to={routes.SIGN_UP}>Sign Up</HeaderLink> | ||
</> | ||
)} | ||
</HeaderSection> | ||
</Header> | ||
<Wrapper>{children}</Wrapper> | ||
</Fragment> | ||
) | ||
} | ||
const Layout = ({ isAuthenticated, children }) => ( | ||
<Fragment> | ||
<Header> | ||
<HeaderSection> | ||
<HeaderLink to={routes.PRODUCT_LIST}>All Products</HeaderLink> | ||
</HeaderSection> | ||
<HeaderSection> | ||
<HeaderLink to={routes.CART}>My Cart</HeaderLink>| | ||
{isAuthenticated ? ( | ||
<> | ||
<HeaderLink to={routes.ACCOUNT}>My Account</HeaderLink>| | ||
<HeaderLink to={routes.LOGOUT}>Logout</HeaderLink> | ||
</> | ||
) : ( | ||
<> | ||
<HeaderLink to={routes.LOGIN}>Log In</HeaderLink> | | ||
<HeaderLink to={routes.SIGN_UP}>Sign Up</HeaderLink> | ||
</> | ||
)} | ||
</HeaderSection> | ||
</Header> | ||
<Wrapper>{children}</Wrapper> | ||
</Fragment> | ||
) | ||
|
||
const mapStateToProps = state => ({ | ||
isAuthenticated: Object.keys(state.customer).length !== 0, | ||
}) | ||
|
||
const mapDispatchToProps = { | ||
logout: customerActions.logout, | ||
} | ||
|
||
export default withRouter( | ||
connect( | ||
mapStateToProps, | ||
mapDispatchToProps | ||
)(Layout) | ||
) | ||
export default connect(mapStateToProps)(Layout) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
export default { | ||
clientId: '1639340def6563ae1342dca16e5e9711e696ffe45f1c21fe4fba8e272a03f51a', | ||
scope: 'market:335', | ||
apiUrl: 'https://the-amber-brand-12.commercelayer.io', | ||
clientId: '1add790e25ab4f3b0724b593a09e1d1008fcce751d4c2b2f337353051f439eda', | ||
clientSecret: | ||
'eac6464b9c4840222b1258732cb51c5db35ab472be4ba305b24813c8c0076839', | ||
scope: 'market:710', | ||
apiUrl: 'https://the-brown-brand-23.commercelayer.io', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.