@afnank19/express-auth-helper
Version:
Helper functions for rolling your own auth with bcrypt, jwts and expressjs
52 lines (33 loc) • 1.32 kB
Markdown
Express auth helpers are a set of middleware and helper functions that speed up rolling your own auth for small projects.
The documentation is still being worked on :p
Middleware function for authenticating a user that presents an bearer token.
Returns an express middleware function that can be used in the typical way.
`options: { secret: your-jwt-key }`
```javascript
const authenticateUser = authenticateAction({ secrets: "your-jwt-secret" });
app.post("/users/blogs", authenticateUser, ...OtherMiddleware);
```
Pass in a password to generate a hash using bcrypt, currently only 10 passes.
Returns the hash for storage in db
```javascript
const hash = generateHash("superstrongpassword");
```
Pass in the password provided by the user, along with the hash fetched from the DB
Returns a boolean isMatch which is true for success.
```javascript
const { password } = req.body;
const hash = userService.fetchUserHash(id);
const isMatch = await verifyPassword(password, hash);
if (!isMatch) {
res.json({ msg: "Wrong credentials" });
}
// rest of your code
```