-
Notifications
You must be signed in to change notification settings - Fork 6
/
example.js
37 lines (27 loc) · 1.3 KB
/
example.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
const fastify = require('fastify')();
const err = require('http-errors');
fastify.register(require('.')({ secret: '11111111111111111111' }));
/** @param { import('fastify').FastifyInstance } fastify */
async function routes(fastify){
fastify.get('/', async (req, reply) => {
return { public: true };
});
fastify.post('/auth', async (req, reply) => {
const valid_credentials = req.body.user === 'John' && req.body.password === '123'; //never use this in production (unsafe). instead, use crypto.timingSafeEqual
if(!valid_credentials)
throw new err.Forbidden();
const token = await fastify.generateAuthToken({ user: req.body.user }); // you can add as much data as you want here, but keep in mind: the less, the faster
//Bearer 2ead6b62dec72e4c5c50a0e3578fc746e29eb2d374b9d21f655cc35710d09c99
return { token };
});
fastify.register(privateRoutes);
}
/** @param { import('fastify').FastifyInstance } fastify */
async function privateRoutes(fastify){
fastify.requireAuthentication(fastify);
fastify.get('/test', async (req, reply) => {
return { private: true, message: 'Hello, ' + req.auth.user + '!' };
});
}
fastify.register(routes);
fastify.listen(3000, '0.0.0.0', (err) => console.log(err ? err : 'Listening at 3000'));