2020-03-11 16:03:49 +01:00
|
|
|
# Auth Server Helper
|
2020-03-06 12:06:10 +01:00
|
|
|
|
2020-03-11 16:03:49 +01:00
|
|
|
Authentication middleware for express
|
2020-03-06 12:06:10 +01:00
|
|
|
|
|
|
|
## Usage
|
|
|
|
|
|
|
|
```js
|
2020-03-11 16:03:49 +01:00
|
|
|
const auth = require('@scode/auth-server-helper');
|
|
|
|
const password_helper = require('@scode/password_helper');
|
|
|
|
|
|
|
|
const users = {
|
|
|
|
foo: {
|
|
|
|
id: 0
|
|
|
|
password: await password_helper.hash('bar'),
|
|
|
|
salt: '123'
|
|
|
|
}
|
|
|
|
}
|
2020-03-06 12:06:10 +01:00
|
|
|
|
|
|
|
// add cookieParser to allow session management via cookies
|
|
|
|
app.use(cookieParser());
|
|
|
|
|
2020-03-11 16:03:49 +01:00
|
|
|
// the middleware needs a function to determine user data
|
|
|
|
// this function can also return a promise
|
|
|
|
app.use(auth((user_name) => {
|
|
|
|
if (!users[user_name])
|
|
|
|
return null;
|
|
|
|
return users[user_name];
|
|
|
|
}));
|
2020-03-06 12:06:10 +01:00
|
|
|
|
|
|
|
```
|
|
|
|
|
2020-03-11 16:03:49 +01:00
|
|
|
when a client logs in, it will set a header called 'session' that the client can use to authorize the following requests.
|
|
|
|
it also sets a cookie to make requesting from the client more simple. (cookie parser is needed to make authentication with cookies possible)
|