1.0.4 • Published 3 years ago
@yeldirium/express-bearer-authentication v1.0.4
Express Bearer Authentication
This library is no longer maintained
It is advised to use passport.js with the passport-http-bearer strategy instead.
An express.js middleware for customisazle bearer token authentication.
npm install @yeldirium/express-bearer-authentication
# or
yarn install @yeldirium/express-bearer-authentication
Status
Category | Status |
---|---|
Version | |
Dependencies | |
Dev dependencies | |
Build | |
License |
How it works
You pass it a function that turns a bearer token into a user object:
const withBearerAuth = require("@yeldirium/express-bearer-authentication");
const authenticate = async token => {
if (await database.isTokenValid(token)) {
return database.getUserForToken(token);
}
};
app.use(withBearerAuth(authenticate));
When the token is invalid or no user is found for it, the middleware will abort the request and return a status code of 401.
Otherwise it will attach the returned user object to request under req.user
.
You can override the field on wich the returned object is set via a second parameter:
app.use(withBearerAuth(authenticate, "authenticatedUser"));
Now the user object will be available as req.authenticatedUser
.