This repository was archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathservice.js
62 lines (52 loc) · 1.66 KB
/
service.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import Debug from 'debug';
import merge from 'lodash.merge';
import { successRedirect, failureRedirect, setCookie, emitEvents } from './express';
const debug = Debug('feathers-authentication:authentication:service');
class Service {
constructor(app) {
this.app = app;
this.passport = app.passport;
}
create(data = {}, params = {}) {
const defaults = this.app.get('auth');
const payload = merge(data.payload, params.payload);
// create accessToken
// TODO (EK): Support refresh tokens
// TODO (EK): This should likely be a hook
// TODO (EK): This service can be datastore backed to support blacklists :)
return this.passport
.createJWT(payload, merge(defaults, params))
.then(accessToken => {
return { accessToken };
});
}
remove(id, params) {
const defaults = this.app.get('auth');
const accessToken = id !== null ? id : params.headers[defaults.header.toLowerCase()];
// TODO (EK): return error if token is missing?
return this.passport
.verifyJWT(accessToken, merge(defaults, params))
.then(payload => {
return { accessToken };
});
}
}
export default function init(options){
return function() {
const app = this;
const path = options.path;
if (typeof path !== 'string') {
throw new Error(`You must provide a 'path' in your authentication configuration or pass one explicitly.`);
}
debug('Configuring authentication service at path', path);
app.use(
path,
new Service(app, options),
emitEvents(options),
setCookie(options),
successRedirect(),
failureRedirect(options)
);
};
}
init.Service = Service;