11# 03 Storage
22
3- In this example we will login an user using a JWT token in browser local storage and session storage.
3+ In this example we will login an user using a JWT token with headers + local storage and session storage.
44
5- # Steps to build it
5+ ## Install dependencies
66
7- - ` npm install ` to install packages:
7+ ` npm install ` to install packages:
88
99``` bash
10+ cd back
1011npm install
1112```
1213
13- - Start ` back ` app:
14+ ``` bash
15+ cd front
16+ npm install
17+ ```
18+
19+ ## Start apps
20+
21+ Start ` back ` app:
1422
1523``` bash
1624cd ./back
@@ -19,7 +27,7 @@ npm start
1927
2028> NOTE: We added ` .env ` file only for demo purpose. We should ignore this one and add a ` .env.example ` as example.
2129
22- - Start ` front ` app:
30+ Start ` front ` app:
2331
2432``` bash
2533cd ./front
@@ -52,52 +60,15 @@ npm start
5260
5361- Load client list in new tab.
5462
55- ## Login flow
63+ ## Code
5664
57- Backend:
65+ The backend is exactly the same as the ` 01-headers ` example.
5866
59- - ` back/src/core/servers/express.server.ts `
60- - ` back/src/app.ts `
61- - ` back/src/pods/security/security.api.ts `
62- - Check user credentials.
63- - Create ` jwt ` by user credentials.
64- - Return token in body.
67+ Frontend, check the following files:
6568
66- Frontend:
67-
68- - ` front/src/pods/login/login.container.tsx `
69- - ` front/src/pods/login/login.hooks.ts `
69+ - ` front/src/core/api/api.helpers.ts `
7070 - ` front/src/pods/login/api/login.api.ts `
71- - ` front/src/core/api/api/api.client.ts `
72- - ` front/src/core/api/api/api.helpers.ts `
73- - ` front/src/common-app/auth/auth.context.ts `
74- - ` front/src/common-app/app-abr/app-bar.component.tsx `
75-
76- ## Load list flow
77-
78- Backend:
79-
80- - ` back/src/app.ts `
81- - ` back/src/pods/security/security.middlewares.ts `
82- - ` back/src/pods/client/client.api.ts `
83- - ` back/src/pods/order/order.api.ts `
84-
85- Frontend:
86-
87- - ` front/src/pods/list/list.container.tsx `
88- - ` front/src/pods/list/api/list.api.tsx `
89-
90- ## Logout flow
91-
92- Backend:
93-
94- - ` back/src/app.ts ` : We are not using ` jwtMiddleware ` on root security api.
95- - ` back/src/pods/security/security.api.ts `
96-
97- Frontend:
98-
99- - ` front/src/common-app/app-bar/app-bar.component.tsx `
100- - ` front/src/common-app/app-bar/app-bar.api.tsx ` : clear header.
71+ - ` front/src/app.ts `
10172
10273## Session Storage
10374
@@ -106,17 +77,26 @@ We can use session storage for auto-clean this one when users will close browser
10677_ ./front/src/core/api/api.helpers.ts_
10778
10879``` diff
80+ import axios from 'axios';
81+
10982export const setHeader = (header: string, value: string) => {
83+ axios.defaults.headers.common[header] = value;
11084- localStorage.setItem(header, value);
11185+ sessionStorage.setItem(header, value);
11286};
11387
114- - export const getHeader = (header: string) => localStorage.getItem(header);
115- + export const getHeader = (header: string) => sessionStorage.getItem(header);
88+ export const restoreHeader = (header: string) => {
89+ - const value = localStorage.getItem(header);
90+ + const value = sessionStorage.getItem(header);
91+ if (value) {
92+ axios.defaults.headers.common[header] = value;
93+ }
94+ };
11695
11796```
11897
11998> NOTE: Session storage is not loaded in other tabs.
99+ >
120100> https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage
121101
122102# About Basefactor + Lemoncode
0 commit comments