|
| 1 | +## Project in self |
| 2 | + |
| 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. |
| 5 | + |
| 6 | +## To start |
| 7 | + |
| 8 | +### `npm install` |
| 9 | + |
| 10 | +To install all dependencies before run any other script. |
| 11 | + |
| 12 | +### `npm run dev` |
| 13 | + |
| 14 | +Script automatically compiles tsc and watch for changes. Only runs 1 time. |
| 15 | + |
| 16 | +### `npm run build` |
| 17 | + |
| 18 | +Compiles tsc code (this is more for production) |
| 19 | + |
| 20 | +### `npm start` |
| 21 | + |
| 22 | +Starts project after `npm run build` |
| 23 | + |
| 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 | +``` |
| 32 | + |
| 33 | +## Screenshots |
| 34 | + |
| 35 | +- Fields validation |
| 36 | +<p align='center'> |
| 37 | +<img src='https://i.imgur.com/JQD2vth.png' alt='final-project-image'> |
| 38 | +</p> |
| 39 | + |
| 40 | +- Invalid password or email |
| 41 | +<p align='center'> |
| 42 | +<img src='https://i.imgur.com/B8Mzqk5.png' alt='final-project-image'> |
| 43 | +</p> |
| 44 | + |
| 45 | +- Successful sign in |
| 46 | +<p align='center'> |
| 47 | +<img src='https://i.imgur.com/hJoFb4B.png' alt='final-project-image'> |
| 48 | +</p> |
| 49 | + |
| 50 | +- Sending token on headers |
| 51 | + |
| 52 | +<p align='center'> |
| 53 | +<img src='https://i.imgur.com/5r0cAo0.png' alt='final-project-image'> |
| 54 | +</p> |
| 55 | + |
| 56 | +- Authorization |
| 57 | + |
| 58 | +<p align='center'> |
| 59 | +<img src='https://i.imgur.com/jcTFWIB.png' alt='final-project-image'> |
| 60 | +</p> |
| 61 | + |
| 62 | +## Tutorials |
| 63 | + |
| 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) |
| 68 | + |
| 69 | +## Explication |
| 70 | + |
| 71 | +#### Route creation |
| 72 | + |
| 73 | +``` javascript |
| 74 | +import { Router } from 'express'; |
| 75 | +import { signIn, signUp } from '../controllers/user.controller'; |
| 76 | + |
| 77 | +const router = Router(); |
| 78 | + |
| 79 | +router.post('/signup',signUp); |
| 80 | +router.post('/signin', signIn); |
| 81 | + |
| 82 | +export default router; |
| 83 | +``` |
| 84 | + |
| 85 | +#### Route controller |
| 86 | + |
| 87 | +``` javascript |
| 88 | +router.get('/auth', passport.authenticate('jwt', {session: false}), (req, res) => { |
| 89 | + res.status(200).json({msg: "Auth route succeeded"}) |
| 90 | +}) |
| 91 | +``` |
| 92 | + |
| 93 | +#### Create token |
| 94 | + |
| 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 | +``` |
| 102 | + |
| 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). |
| 104 | + |
| 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. |
| 106 | + |
| 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. |
| 108 | + |
| 109 | + |
| 110 | +## Architecture |
| 111 | + |
| 112 | +- [MVC](https://si.ua.es/es/documentacion/asp-net-mvc-3/1-dia/modelo-vista-controlador-mvc.html) |
| 113 | + |
1 | 114 | ## Modules used |
2 | 115 |
|
3 | | -- bcrypt |
| 116 | +- [bcrypt](https://github.com/kelektiv/node.bcrypt.js) |
4 | 117 |
|
5 | | -- cors |
| 118 | +- [cors](https://github.com/expressjs/cors) |
6 | 119 |
|
7 | | -- express |
| 120 | +- [express](https://github.com/expressjs/express) |
8 | 121 |
|
9 | | -- jsonwebtoken |
| 122 | +- [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) |
10 | 123 |
|
11 | | -- mongoose |
| 124 | +- [mongoose](https://github.com/Automattic/mongoose) |
12 | 125 |
|
13 | | -- morgan |
| 126 | +- [morgan](https://github.com/expressjs/morgan) |
14 | 127 |
|
15 | | -- passport |
| 128 | +- [passport](https://github.com/jaredhanson/passport) |
16 | 129 |
|
17 | | -- passport-jwt |
| 130 | +- [passport-jwt](https://github.com/mikenicholson/passport-jwt) |
0 commit comments