Skip to content

Commit 6d614ee

Browse files
committed
update 02-cookies
1 parent fb80c8e commit 6d614ee

76 files changed

Lines changed: 7850 additions & 448 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: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@
22

33
In this example we will login an user using a JWT token in Cookies and load client and order collections.
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,25 +27,25 @@ 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
2634
npm start
2735
```
2836

29-
- We don't need CORS because we are using `webpack's proxy`, so it looks like same domain:
37+
We don't need CORS because we are using a `proxy`, so it looks like same domain:
3038

31-
_./front/config/webpack/dev.js_
39+
_./front/vite.config.ts_
3240

3341
```javascript
3442
...
35-
devServer: {
36-
...
43+
server: {
3744
proxy: {
38-
'/api': 'http://localhost:8081',
45+
'/api': 'http://localhost:3000',
3946
},
4047
},
48+
...
4149
```
4250

4351
## Demo
@@ -67,14 +75,15 @@ _./front/config/webpack/dev.js_
6775
- Load client list in new tab.
6876

6977
> NOTE: Right now, the cookie expires when we close all brower's tabs.
78+
>
7079
> Check Browser lock icon > Cookies
7180
7281
## Login flow
7382

7483
Backend:
7584

76-
- `back/src/core/servers/express.server.ts`
77-
- `back/src/app.ts`
85+
- `back/src/core/servers/rest-api.server.ts`
86+
- `back/src/index.ts`
7887
- `back/src/pods/security/security.api.ts`
7988
- `back/src/pods/security/security.constants.ts`
8089
- Check user credentials.
@@ -86,15 +95,15 @@ Frontend:
8695
- `front/src/pods/login/login.container.tsx`
8796
- `front/src/pods/login/login.hooks.ts`
8897
- `front/src/pods/login/api/login.api.ts`
89-
- `front/src/core/api/api/api.helpers.ts`: Deleted
98+
- `front/src/core/api/api.helpers.ts`: Deleted
9099
- `front/src/common-app/auth/auth.context.ts`
91100
- `front/src/common-app/app-abr/app-bar.component.tsx`
92101

93102
## Load list flow
94103

95104
Backend:
96105

97-
- `back/src/app.ts`
106+
- `back/src/index.ts`
98107
- `back/src/pods/security/security.middlewares.ts`: `req.cookies`.
99108
- `back/src/pods/client/client.api.ts`
100109
- `back/src/pods/order/order.api.ts`
@@ -108,7 +117,7 @@ Frontend:
108117

109118
Backend:
110119

111-
- `back/src/app.ts`: We are not using `jwtMiddleware` on root security api.
120+
- `back/src/index.ts`: We are not using `jwtMiddleware` on root security api.
112121
- `back/src/pods/security/security.api.ts`: clear cookie
113122

114123
Frontend:
@@ -124,18 +133,18 @@ _./back/src/pods/security/security.constants.ts_
124133

125134
```diff
126135
import { CookieOptions } from 'express';
127-
import { envConstants } from 'core/constants';
136+
import { ENV } from '#core/constants/index.js';
128137

129-
export const jwtSignAlgorithm = 'HS256';
138+
export const JWT_SIGN_ALGORITHM = 'HS256';
130139

131-
- export const cookieOptions: CookieOptions = {
140+
- export const COOKIE_OPTIONS: CookieOptions = {
132141
- httpOnly: true,
133-
- secure: envConstants.isProduction
142+
- secure: ENV.IS_PRODUCTION,
134143
- };
135144

136145
+ export const getCookieOptions = (expires: Date): CookieOptions => ({
137146
+ httpOnly: true,
138-
+ secure: envConstants.isProduction,
147+
+ secure: ENV.IS_PRODUCTION,
139148
+ expires,
140149
+ });
141150

@@ -145,8 +154,8 @@ _./back/src/pods/security/security.api.ts_
145154

146155
```diff
147156
...
148-
- import { jwtSignAlgorithm, cookieOptions } from './security.constants';
149-
+ import { jwtSignAlgorithm, getCookieOptions } from './security.constants';
157+
- import { JWT_SIGN_ALGORITHM, COOKIE_OPTIONS } from './security.constants.js';
158+
+ import { JWT_SIGN_ALGORITHM, getCookieOptions } from './security.constants.js';
150159

151160
...
152161

@@ -161,18 +170,16 @@ _./back/src/pods/security/security.api.ts_
161170
const token = createToken(currentUser);
162171
+ const expires = new Date();
163172
+ expires.setDate(new Date().getDate() + 1); // Add one day
164-
173+
165174
res.cookie(
166-
headerConstants.authorization,
175+
HEADERS.AUTHORIZATION,
167176
token,
168-
- cookieOptions
177+
- COOKIE_OPTIONS
169178
+ getCookieOptions(expires)
170179
);
171180
res.send(userSession);
172181
} else {
173-
res.sendStatus(401);
174-
}
175-
})
182+
...
176183

177184
```
178185

@@ -184,15 +191,16 @@ _./back/src/pods/security/security.constants.ts_
184191

185192
```diff
186193
import { CookieOptions } from 'express';
187-
import { envConstants } from 'core/constants';
194+
import { ENV } from '#core/constants/index.js';
188195

189-
export const jwtSignAlgorithm = 'HS256';
196+
export const JWT_SIGN_ALGORITHM = 'HS256';
190197

191-
export const cookieOptions: CookieOptions = {
198+
export const getCookieOptions = (expires: Date): CookieOptions => ({
192199
- httpOnly: true,
193200
+ httpOnly: false,
194-
secure: envConstants.isProduction,
195-
};
201+
secure: ENV.IS_PRODUCTION,
202+
expires,
203+
});
196204

197205
```
198206

06-rest-api/02-auth/02-cookies/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/02-cookies/back/.gitignore

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

0 commit comments

Comments
 (0)