-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
38 lines (33 loc) · 1.35 KB
/
server.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
//include request middleware
var request = require('request');
/*
|--------------------------------------------------------------------------
| Login with Google
|--------------------------------------------------------------------------
*/
app.post('/auth/google', function(req, res) {
var accessTokenUrl = 'https://accounts.google.com/o/oauth2/token';
var peopleApiUrl = 'https://www.googleapis.com/plus/v1/people/me/openIdConnect';
var params = {
code: req.body.code,
client_id: req.body.clientId,
client_secret: "YOUR_SECRET_KEY",
redirect_uri: req.body.redirectUri,
grant_type: 'authorization_code'
};
// Step 1. Exchange authorization code for access token.
request.post(accessTokenUrl, { json: true, form: params }, function(err, response, token) {
var accessToken = token.access_token;
var headers = { Authorization: 'Bearer ' + accessToken };
// Step 2. Retrieve profile information about the current user.
request.get({ url: peopleApiUrl, headers: headers, json: true }, function(err, response, profile) {
if (profile.error) {
return res.status(500).send({message: profile.error.message});
}
console.log(response.body, "auth response");
var token = jwt.encode(response, params.client_secret);
// Step 3. Send response to front-end.
res.send({ token: token });
});
});
});