-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
92 lines (68 loc) · 2.61 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
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
var express = require('express');
const urlModule = require('url');
const axios = require('axios')
var { graphqlHTTP } = require('express-graphql');
var opn = require('opn');
// Construct a schema, using GraphQL schema language
var schema = require('./typeDefs');
var resolvers = require('./resolvers');
var app = express();
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: resolvers,
graphiql: true,
}));
// Code Grant Flow
app.post('/loginCodeGrant', (req, res) => {
const url = new urlModule.URL('http://localhost:3333/code-grant');
const redirectUrl = new urlModule.URL('http://localhost:5000/checkCode');
url.searchParams.append('response_type', 'code');
url.searchParams.append('client_id', '47f9d065-e457-4b87-8ed8-a6bade87755a');
url.searchParams.append('redirect_uri', redirectUrl);
url.searchParams.append('state', 'REQUIRED');
res.redirect(307, url);
})
app.post('/checkCode', (req, res) => {
const url = new urlModule.URL('http://localhost:3333/access-token');
const redirectUrl = new urlModule.URL('http://localhost:5000/getToken');
url.searchParams.append('grant_type', 'authorization_code');
url.searchParams.append('code', req.query.code);
url.searchParams.append('client_id', '47f9d065-e457-4b87-8ed8-a6bade87755a');
url.searchParams.append('client_secret', '64372572-870b-43e0-9aae-f080b14ddfa4');
url.searchParams.append('redirect_uri', redirectUrl);
res.redirect(307, url);
})
// Implicit Grant Flow
app.post('/loginImplicitGrant', (req, res) => {
const url = new urlModule.URL('http://localhost:3333/implicit-grant');
const redirectUrl = new urlModule.URL('http://localhost:5000/checkToken');
url.searchParams.append('response_type', 'token');
url.searchParams.append('client_id', '47f9d065-e457-4b87-8ed8-a6bade87755a');
url.searchParams.append('redirect_uri', redirectUrl);
url.searchParams.append('state', 'REQUIRED');
res.redirect(307, url);
})
app.post('/checkToken', (req, res) => {
const config = {
headers: { Authorization: `Bearer ${req.query.access_token}` }
};
const bodyParameters = {
key: "value"
};
axios.post(
'http://localhost:3333/check-token',
bodyParameters,
config
).then(opn('http://localhost:5000/graphql')).catch(console.log);
res.json(req.query);
});
app.post('/getToken', (req, res) => {
if (req.query.access_token) {
opn('http://localhost:5000/graphql');
}
res.json(req.query);
})
const PORT = 5000;
app.listen(PORT, () => {
console.log(`Running a GraphQL API server at localhost:${PORT}/graphql`);
});