-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathoauthServerMiddlewares.js
More file actions
55 lines (47 loc) · 1.56 KB
/
Copy pathoauthServerMiddlewares.js
File metadata and controls
55 lines (47 loc) · 1.56 KB
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
const OauthServer = require('oauth2-server');
const model = require('./model');
const oauth = new OauthServer({ model });
const Request = OauthServer.Request;
const Response = OauthServer.Response;
/*
TODO: Use this authenticateHandler to authenticate the user by other means
https://github.com/oauthjs/node-oauth2-server/issues/314
https://github.com/oauthjs/node-oauth2-server/issues/264
*/
const authenticateHandler = {
handle(req, res) {
// get authenticated user or return falsy value, e.g.:
return req.session.user;
}
};
module.exports.token = (req, res, next) => {
const request = new Request(req);
const response = new Response(res);
oauth.token(request, response)
.then((token) => {
console.log('generated token data', token);
res.set(response.headers);
res.json(response.body);
}).catch(err => next(err));
};
module.exports.authorize = (req, res, next) => {
const request = new Request(req);
const response = new Response(res);
oauth.authorize(request, response).then((authorizationCode) => {
// TODO: Here i get a redirect response
console.log(authorizationCode);
res.status(response.status).set(response.headers).end();
}).catch(err => next(err));
};
module.exports.authenticate = (req, res, next) => {
const request = new Request(req);
const response = new Response(res);
oauth.authenticate(request, response)
.then((token) => {
console.log('token data', token)
// Request is authorized.
Object.assign(req, { user: token });
next();
})
.catch(err => next(err));
};