-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.dev-server.ts
75 lines (62 loc) · 2.59 KB
/
webpack.dev-server.ts
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
import SingleSignOn from 'eve-sso';
import 'dotenv/config';
// Get the client ID and secret from the Eve developers section
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
// The callback URI as defined in the application in the developers section
const CALLBACK_URI = process.env.CALLBACK_URI || 'http://localhost:8080/';
console.log(process.env);
const ESI_SCOPES =
process.env.ESI_SCOPES ||
'esi-assets.read_assets.v1 esi-markets.structure_markets.v1 esi-markets.read_character_orders.v1 esi-characters.read_notifications.v1';
const sso = new SingleSignOn(CLIENT_ID, CLIENT_SECRET, CALLBACK_URI, {
endpoint: 'https://login.eveonline.com', // optional, defaults to this
// userAgent: 'my-user-agent' // optional
});
import systems from '@eve-data/systems';
import type { Configuration as DevServerConfiguration } from 'webpack-dev-server';
const devServer: DevServerConfiguration = {
static: './dist',
open: false,
setupMiddlewares: (middlewares, devServer) => {
if (!devServer) {
throw new Error('webpack-dev-server is not defined');
}
const { app } = devServer;
const scopes = ESI_SCOPES.split(' ');
app.get('/login-url', function (req, res) {
// TODO: generate and check state
res.send(sso.getRedirectUrl('my-state', scopes));
});
app.get('/sso', async function (req, res) {
const { code, refresh } = req.query;
// NOTE: usually you'd want to validate the state (ctx.query.state) as well
// Swap the one-time code for an access token
console.log(code);
console.log(refresh);
try {
const info = await sso.getAccessToken(code.toString(), refresh ? true : false);
// Usually you'd want to store the access token
// as well as the refresh token
console.log('info', info);
// Do whatever, for example, redirect to user page
res.json(info);
} catch (error) {
console.error(error);
console.error('Error!');
}
});
app.get('/systems', async function (req, res) {
const { val } = req.query;
const entries = systems.filter(system =>
system.name.toLowerCase().includes(val.toString().toLowerCase()),
);
return res.json({
entries,
count: entries.length,
});
});
return middlewares;
},
};
export default devServer;