This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
/
app.js
84 lines (71 loc) · 2.19 KB
/
app.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const path = require('path');
const feathers = require('feathers');
const rest = require('feathers-rest');
const socketio = require('feathers-socketio');
const hooks = require('feathers-hooks');
const memory = require('feathers-memory');
const bodyParser = require('body-parser');
const errorHandler = require('feathers-errors/handler');
const local = require('feathers-authentication-local');
const jwt = require('feathers-authentication-jwt');
const auth = require('../lib/index');
const app = feathers();
app.configure(rest())
.configure(socketio())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
.configure(auth({ secret: 'supersecret' }))
.configure(local())
.configure(jwt())
.use('/users', memory())
.use('/', feathers.static(path.resolve(__dirname, '/public')));
app.service('authentication').hooks({
before: {
create: [
// You can chain multiple strategies
auth.hooks.authenticate(['jwt', 'local'])
],
remove: [
auth.hooks.authenticate('jwt')
]
}
});
// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
app.service('users').hooks({
before: {
find: [
auth.hooks.authenticate('jwt')
],
create: [
local.hooks.hashPassword({ passwordField: 'password' })
]
}
});
// Custom Express routes
app.get('/protected', auth.express.authenticate('jwt'), (req, res, next) => {
res.json({ success: true });
});
app.get('/unprotected', (req, res, next) => {
res.json({ success: true });
});
// Custom route with custom redirects
app.post('/login', auth.express.authenticate('local', { successRedirect: '/app', failureRedirect: '/login' }));
app.get('/app', (req, res, next) => {
res.json({ success: true });
});
app.get('/login', (req, res, next) => {
res.json({ success: false });
});
var User = {
email: 'admin@feathersjs.com',
password: 'admin',
permissions: ['*']
};
app.service('users').create(User).then(user => {
console.log('Created default user', user);
}).catch(console.error);
app.use(errorHandler());
app.listen(3030);
console.log('Feathers authentication with local auth started on 127.0.0.1:3030');