Skip to content

Commit 47bac99

Browse files
committed
docs: 📝 Update README.md
1 parent bbf6651 commit 47bac99

File tree

1 file changed

+116
-84
lines changed

1 file changed

+116
-84
lines changed

README.md

Lines changed: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,162 @@
1-
## Project in self
1+
# TypeScript API with authentication
22

3-
API made with Typescript, MongoDB and JsonWebToken as main technologies.
4-
This API has an **no-sesion** authentication, made by passport-jwt, for endpoints. Based on **MVC** architecture.
3+
This project purpose is to learn about JWT auth flow, using TypeScript.
54

6-
## To start
5+
## Run Locally
76

8-
### `npm install`
7+
1. Install both:
98

10-
To install all dependencies before run any other script.
9+
- [Node.js](https://nodejs.org/es/download/)
10+
- [MongoDB](https://www.mongodb.com/try/download/community)
1111

12-
### `npm run dev`
12+
You will need to have MongoDB running on port 27017.
1313

14-
Script automatically compiles tsc and watch for changes. Only runs 1 time.
14+
2. Clone the project:
1515

16-
### `npm run build`
16+
```bash
17+
git clone https://github.com/AloisCRR/jwt-api-users-auth.git
18+
```
1719

18-
Compiles tsc code (this is more for production)
20+
3. Go to the project directory:
1921

20-
### `npm start`
22+
```bash
23+
cd jwt-api-users-auth
24+
```
2125

22-
Starts project after `npm run build`
26+
4. Install dependencies:
2327

24-
After that, you can open the project in...
25-
``` javascript
26-
http://localhost:3000
27-
```
28-
Or you can define a port in eviroment variables...
29-
``` javascript
30-
app.set('port',process.env.port || 3000);
31-
```
28+
```bash
29+
npm install
30+
```
3231

33-
## Screenshots
32+
5. Start the dev server:
3433

35-
- Fields validation
36-
<p align='center'>
37-
<img src='https://i.imgur.com/JQD2vth.png' alt='final-project-image'>
38-
</p>
34+
```bash
35+
npm run dev
36+
```
3937

40-
- Invalid password or email
41-
<p align='center'>
42-
<img src='https://i.imgur.com/B8Mzqk5.png' alt='final-project-image'>
43-
</p>
38+
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
4439

45-
- Successful sign in
46-
<p align='center'>
47-
<img src='https://i.imgur.com/hJoFb4B.png' alt='final-project-image'>
48-
</p>
40+
6. To compile TypeScript to JavaScript and run the project:
4941

50-
- Sending token on headers
42+
```bash
43+
npm run build && npm start
44+
```
5145

52-
<p align='center'>
53-
<img src='https://i.imgur.com/5r0cAo0.png' alt='final-project-image'>
54-
</p>
46+
## API Reference
5547

56-
- Authorization
48+
#### Sign up or register
5749

58-
<p align='center'>
59-
<img src='https://i.imgur.com/jcTFWIB.png' alt='final-project-image'>
60-
</p>
50+
```http
51+
POST /signup
52+
```
6153

62-
## Tutorials
54+
| Body | Type | Description |
55+
| :--------- | :------- | :------------------------------- |
56+
| `email` | `string` | **Required**. User email address |
57+
| `password` | `string` | **Required**. Account password |
6358

64-
- [JsonWebTokens #1](https://www.youtube.com/watch?v=qckBlIfOnlA)
65-
- [JsonWebTokens #2](https://www.youtube.com/watch?v=mbsmsi7l3r4)
66-
- [JsonWebTokens #3](https://www.youtube.com/watch?v=7nafaH9SddU)
67-
- [Passport JWT Strategy Flow](https://www.youtube.com/watch?v=o6mSdG09yOU)
59+
#### Sign in or login
6860

69-
## Explication
61+
```http
62+
POST /signin
63+
```
7064

71-
#### Route creation
65+
| Body | Type | Description |
66+
| :--------- | :------- | :------------------------------- |
67+
| `email` | `string` | **Required**. User email address |
68+
| `password` | `string` | **Required**. Account password |
7269

73-
``` javascript
74-
import { Router } from 'express';
75-
import { signIn, signUp } from '../controllers/user.controller';
70+
```http
71+
GET /auth
72+
```
7673

77-
const router = Router();
74+
| Headers | Type | Description |
75+
| :--------------- | :---- | :-------------------------------------------- |
76+
| `Authentication` | `JWT` | **Required**. Jwt gived on sign in or sign up |
7877

79-
router.post('/signup',signUp);
80-
router.post('/signin', signIn);
78+
## Screenshots
8179

82-
export default router;
83-
```
80+
Basic input validation
8481

85-
#### Route controller
82+
![Screenshot](https://i.imgur.com/JQD2vth.png)
8683

87-
``` javascript
88-
router.get('/auth', passport.authenticate('jwt', {session: false}), (req, res) => {
89-
res.status(200).json({msg: "Auth route succeeded"})
90-
})
91-
```
84+
Invalid password or email
9285

93-
#### Create token
86+
![Screenshot](https://i.imgur.com/B8Mzqk5.png)
9487

95-
``` javascript
96-
function createToken(user:Iuser) {
97-
return jwt.sign({id: user.id, email:user.email}, config.jwtSecret, {
98-
expiresIn: 86400
99-
})
100-
}
101-
```
88+
Successful sign in
10289

103-
Works in this way... With JWT obviously you can generate a token for authentication, a token have some public and private information. Public info is like the algorith used to sign token or the type of token, also included something called "payload" wich is content or body of token (this includes all data registered for token).
90+
![Screenshot](https://i.imgur.com/hJoFb4B.png)
10491

105-
To generate a token we use a function from jwt moduled sign, passing a "payload" that is information of token, and a secret used to sign token.
92+
Sending token on headers
10693

107-
Token is signed by a private key, and it is used to "decrypt" it and use to auth, passport takes his time in this, with passport-jwt we can use a function called passport.authenticate() and thats it.
94+
![Screenshot](https://i.imgur.com/5r0cAo0.png)
10895

96+
Authorization
10997

110-
## Architecture
98+
![Screenshot](https://i.imgur.com/jcTFWIB.png)
11199

112-
- [MVC](https://si.ua.es/es/documentacion/asp-net-mvc-3/1-dia/modelo-vista-controlador-mvc.html)
100+
## Tech Stack
113101

114-
## Modules used
102+
| Name | Description |
103+
| ---------------------------------------------------------- | ----------------------------------------------------------- |
104+
| [Node.js](https://nodejs.org/es/download/) | Business logic |
105+
| [MongoDB](https://www.mongodb.com/try/download/community) | Database |
106+
| [Express](https://expressjs.com/es/api.html) | HTTP Server |
107+
| [TypeScript](https://www.typescriptlang.org/) | JavaScript super-set to add static code analysis |
108+
| [JWT](https://jwt.io/) | Library to generate JWTs |
109+
| [Mongoose](https://mongoosejs.com/docs/api.html) | ODM (Object Data Modeling) |
110+
| [Passport JWT](https://www.npmjs.com/package/passport-jwt) | Passport strategy for authenticating with a JSON Web Token. |
111+
| [Bcrypt](https://www.npmjs.com/package/passport-jwt) | Passport strategy for authenticating with a JSON Web Token. |
115112

116-
- [bcrypt](https://github.com/kelektiv/node.bcrypt.js)
113+
## Lessons Learned
117114

118-
- [cors](https://github.com/expressjs/cors)
115+
### Route creation
119116

120-
- [express](https://github.com/expressjs/express)
117+
```typescript
118+
import { Router } from "express";
119+
import { signIn, signUp } from "../controllers/user.controller";
120+
121+
const router = Router();
122+
123+
router.post("/signup", signUp);
124+
router.post("/signin", signIn);
125+
126+
export default router;
127+
```
128+
129+
### Route controller
130+
131+
```typescript
132+
router.get(
133+
"/auth",
134+
passport.authenticate("jwt", { session: false }),
135+
(req, res) => {
136+
res.status(200).json({ msg: "Auth route succeeded" });
137+
}
138+
);
139+
```
140+
141+
### Create token
142+
143+
```typescript
144+
function createToken(user: Iuser) {
145+
return jwt.sign({ id: user.id, email: user.email }, config.jwtSecret, {
146+
expiresIn: 86400,
147+
});
148+
}
149+
```
121150

122-
- [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken)
151+
Works in this way... With JWT obviously you can generate a token for authentication, a token can hold public data in a stateless way. Public info is like the algorithm used to sign token or the type of token, also included something called "payload" which is content or body of token (this includes all data registered for token).
123152

124-
- [mongoose](https://github.com/Automattic/mongoose)
153+
To generate a token we use a function from jwt module called sign, passing a "payload" that is information that token will save, and a secret used to sign the token.
125154

126-
- [morgan](https://github.com/expressjs/morgan)
155+
Token is signed by a private key, and with the same key we can check if token is valid and use it to authenticate an user, passport takes his time in this, with passport-jwt we can use a function called passport.authenticate() which is a middleware that handles all the logic from getting the token from auth header to validate it and attach the token payload to the request object of express.
127156

128-
- [passport](https://github.com/jaredhanson/passport)
157+
## Roadmap
129158

130-
- [passport-jwt](https://github.com/mikenicholson/passport-jwt)
159+
- [x] App functionality
160+
- [ ] Testing
161+
- [ ] Hosting, domain, etc.
162+
- [ ] CI/CD

0 commit comments

Comments
 (0)