https://github.com/maurerkrisztian/authentication-middlewares
https://github.com/maurerkrisztian/authentication-middlewares
authentication authorisation login registration token
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/maurerkrisztian/authentication-middlewares
- Owner: MaurerKrisztian
- Created: 2021-03-28T11:56:45.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-28T19:59:02.000Z (about 4 years ago)
- Last Synced: 2025-01-12T18:52:10.970Z (4 months ago)
- Topics: authentication, authorisation, login, registration, token
- Language: TypeScript
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Authentication-Middlewares
### With these middlewares you can easily create login, registration and route protection, role handling.
## Authentication Middleware
this middleware will put the `roles[]` to the req.roles#### Keep in mind: this require to use `AuthenticationMiddlewares.signAccessToken` or just put a `roles: string[]` to the token
```typescript
AuthenticationMiddlewares.setup({
jwtSecret: 'secret',
isBearer: false, // your token starts with "bearer xx.yy.zz"
tokenPath: 'token' // token location in header req.headers[tokenPath]
});app.use(AuthenticationMiddlewares.authenticationMiddleware()); // get the roles every request
```## Password encryption
```typescript
AuthenticationMiddlewares(passwordField: string, bcryptPasswordPath: string)
```
it will get the `request.body[passwordField]` and encrypt it then put to `request[bcryptPasswordPath]````typescript
// REGISTRATION
// the req.body['password'] filed will be encrypted to req['bcryptedPassword'];
app.post('/registration', AuthenticationMiddlewares.passwordBcryptMiddleware('password', 'bcryptedPassword'), (req: any, res: any) => {
console.log(req.bcryptedPassword) // encrypted password
})```
## Login services
you can use:
* `BcryptService.isMatchHashPassword(password: string, hashPassword: string)` - correct password?
* `TokenService.signAccessToken({roles: string[]})` - crete token with role
```typescript
// LOGIN - this part is mostly your implementation.. but when you sign a token you must put in a roles: sting[] array
app.post('/login', async (req: any, res: any) => {
const isPassMatch = await BcryptService.isMatchHashPassword(req.body.password, "$2b$10$2wO3DGR9BufUnIhyOrhzIeFVpwO/UX8PJo08UBz5nkD2ZigJTPRxa") // the hash password what you get back by registration
const token = await TokenService.signAccessToken({roles: ['user']});
// ....
})
```## Authorisation middleware
`AuthenticationMiddlewares.authorisationMiddleware(requiredRole: string)`
* if the token role don't match with the "requiredRole" it will send back "access denied"```typescript
app.get('/secret', AuthenticationMiddlewares.authorisationMiddleware('admin'), (req: any, res: any) => {
console.log('access enabled')
})
```