Timo Hocker
573ca9e633
update-scanner: automatic update
@sapphirecode/auth-client-helper: 1.0.51 ==> 1.0.52 minor @sapphirecode/consts: 1.1.22 ==> 1.1.23 minor @sapphirecode/crypto-helper: 1.1.50 ==> 1.1.51 minor @sapphirecode/eslint-config: 2.1.9 ==> 2.1.10 minor @sapphirecode/password-helper: 1.0.41 ==> 1.0.42 minor eslint: 7.2.0 ==> 7.3.0 minor
@sapphirecode/auth-server-helper
version: 1.0.x
authentication middleware for express
Installation
npm:
npm i --save @sapphirecode/auth-server-helper
yarn:
yarn add @sapphirecode/auth-server-helper
Usage
const auth = require('@sapphirecode/auth-server-helper');
const password_helper = require('@sapphirecode/password_helper');
const users = {
foo: {
id: 0
password: await password_helper.hash('bar'),
salt: '123'
}
}
// add cookieParser to allow session management via cookies
app.use(cookieParser());
// 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];
}));
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)
Excluding routes
exceptions to the auth module can be added by adding an array of regular expressions a specific method can also be filtered for by giving an object instead of a plain regular expression.
auth(..., [/no-auth/, {regex: '/no-auth-post/', method: 'POST'}]);
License
MIT © Timo Hocker timo@scode.ovh
Description
Languages
TypeScript
99.3%
JavaScript
0.7%