-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathauthenticate.js
More file actions
25 lines (24 loc) · 850 Bytes
/
Copy pathauthenticate.js
File metadata and controls
25 lines (24 loc) · 850 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const jwt = require('jsonwebtoken');
const config = require('config');
const accounts = require('./accounts.json');
// Method that attempts to authenticate a user and delivers a JWT on success
module.exports = function (identifier, password) {
for (let i = 0; i < accounts.length; i++) {
const account = accounts[i];
if (account.identifier == identifier) {
if (account.password == password) {
let payload = {};
payload.user = identifier;
const roles = config.get('roles');
for (let j = 0; j < roles.length; j++) {
const role = roles[j];
payload[role] = account.roles.includes(role);
}
return jwt.sign(payload, config.get('secret'));
} else {
throw new Error('Authentication failed.');
}
}
}
throw new Error('Authentication failed.');
};