-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
81 lines (67 loc) · 2.08 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
var express = require('express');
var bodyParser = require('body-parser');
var session = require('express-session');
var cookieParser = require('cookie-parser');
/* Requiring the lib */
var oauth = require('oauthio');
var app = express();
app.use(express.static('public'));
app.use(bodyParser());
app.use(cookieParser());
app.use(session({secret: 'keyboard cat', key: 'sid'}));
/* Initialization */
try {
var config = require('./config');
} catch (e) {
// Create a config.js file returning an object like the following if you haven't done it yet
var config = {
key: 'your_key',
secret: 'your_secret'
};
}
oauth.initialize(config.key, config.secret);
/* Endpoints */
app.get('/oauth/token', function (req, res) {
var token = oauth.generateStateToken(req.session);
res.json({
token: token
});
});
app.post('/oauth/signin', function (req, res) {
var code = req.body.code;
oauth.auth('google', req.session, {
code: code
})
.then(function (request_object) {
// Here the user is authenticated, and the access token
// for the requested provider is stored in the session.
// Continue the tutorial or checkout the step-4 to get
// the code for the request
res.send(200, 'The user is authenticated');
})
.fail(function (e) {
console.log(e);
res.send(400, 'Code is incorrect');
});
});
app.get('/me', function (req, res) {
// Here we first build a request object from the session with the auth method.
// Then we perform a request using the .me() method.
// This retrieves a unified object representing the authenticated user.
// You could also use .get('/me') and map the results to fields usable from
// the front-end (which waits for the fields 'name', 'email' and 'avatar').
oauth.auth('google', req.session)
.then(function (request_object) {
return request_object.me();
})
.then(function (user_data) {
res.json(user_data);
})
.fail(function (e) {
console.log(e);
res.send(400, 'An error occured');
});
});
app.listen(process.env.NODEJS_PORT || 3000, function () {
console.log('OAuth.io Tutorial server running on port ' + (process.env.NODEJS_PORT || 3000));
});