Skip to content

Commit 90d760b

Browse files
committed
update 03-storage
1 parent 6d614ee commit 90d760b

76 files changed

Lines changed: 7807 additions & 480 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

06-rest-api/02-auth/02-cookies/README.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ cd ./front
3434
npm start
3535
```
3636

37-
We don't need CORS because we are using a `proxy`, so it looks like same domain:
38-
39-
_./front/vite.config.ts_
40-
41-
```javascript
42-
...
43-
server: {
44-
proxy: {
45-
'/api': 'http://localhost:3000',
46-
},
47-
},
48-
...
49-
```
50-
5137
## Demo
5238

5339
- Open Chrome Dev tools > Network tab.

06-rest-api/02-auth/03-storage/README.md

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
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
1011
npm 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
1624
cd ./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
2533
cd ./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+
10982
export 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

06-rest-api/02-auth/03-storage/back/.babelrc

Lines changed: 0 additions & 32 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
NODE_ENV=development
2-
PORT=8081
2+
PORT=3000
33
TOKEN_AUTH_SECRET=MY_TOKEN_AUTH_SECRET
44
ACCESS_TOKEN_EXPIRES_IN=1d

06-rest-api/02-auth/03-storage/back/.gitignore

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)