forked from resource-watch/resource-watch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
101 lines (95 loc) · 3.16 KB
/
auth.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
require('isomorphic-fetch');
const passport = require('passport');
const Strategy = require('passport-control-tower');
const LocalStrategy = require('passport-local').Strategy;
const queryString = require('query-string');
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session.
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
module.exports = (() => {
const strategy = new Strategy({
controlTowerUrl: process.env.CONTROL_TOWER_URL,
callbackUrl: process.env.CALLBACK_URL,
applications: process.env.APPLICATIONS || 'rw'
});
const localStrategy = new LocalStrategy(
{ usernameField: 'email', passwordField: 'password', session: true },
(email, password, done) => {
const queryParams = queryString.stringify({
callbackUrl: process.env.CALLBACK_URL,
applications: 'rw',
token: true,
origin: 'rw'
});
fetch(`${process.env.CONTROL_TOWER_URL}/auth/login?${queryParams}`, {
method: 'POST',
body: JSON.stringify({ email, password }),
headers: { 'Content-Type': 'application/json' }
})
.then((response) => {
if (response.ok) return response.json();
throw response;
})
.then(({ data }) => done(null, data))
.catch(err => done(err));
},
);
passport.use(strategy);
passport.use('local-signin', localStrategy);
return {
initialize: (server) => {
server.use(passport.initialize());
server.use(passport.session());
},
authenticate: authOptions => passport.authenticate('control-tower', authOptions),
login: (req, res) => strategy.login(req, res),
// local sign-in
signin: (req, res, done) =>
passport.authenticate('local-signin', (err, user) => {
if (err) {
return res.status(401).json({ status: 'error', message: err.statusText });
}
if (!user) {
return res
.status(401)
.json({ status: 'error', message: 'Invalid Login' });
}
return req.login(user, {}, (loginError) => {
if (loginError) {
return res.status(401).json({ status: 'error', message: loginError });
}
return res.json(req.user);
});
})(req, res, done),
updateUser: (req, res) => {
const { body } = req;
const { userObj, token } = body;
fetch(`${process.env.CONTROL_TOWER_URL}/auth/user/me`, {
method: 'PATCH',
body: JSON.stringify(userObj),
headers: {
'Content-Type': 'application/json',
Authorization: token
}
})
.then((response) => {
if (response.status >= 400) throw new Error(response.statusText);
return response.json();
})
.then(user =>
req.login({ ...user, token }, {}, (err) => {
if (err) return res.status(401).json({ status: 'error', message: err });
return res.json({
...user,
token: userObj.token
});
}));
}
};
})();