Add authentication

This commit is contained in:
Isaac
2022-05-06 21:17:53 +01:00
parent 425e7ab151
commit a1823a750e
8 changed files with 105 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
module.exports.get = fastify => ({
handler: (req, res) => {
const { client } = res.context.config;
return client.guilds.cache.get(req.params.guild);
},
onRequest: [fastify.authenticate],
});

View File

@@ -0,0 +1,9 @@
module.exports.get = fastify => ({
handler: async (req, res) => {
const { client } = res.context.config;
const user = await client.users.fetch(req.user.payload.id);
console.log(req.user.payload.username, user?.tag);
res.send(client.guilds.cache);
},
onRequest: [fastify.authenticate],
});

View File

@@ -0,0 +1,29 @@
const fetch = require('node-fetch');
const { domain } = require('../../lib/http');
module.exports.get = () => ({
handler: async function (req, res) { // must NOT use arrow function syntax
const {
access_token, expires_in,
} = await this.discord.getAccessTokenFromAuthorizationCodeFlow(req);
const user = await (await fetch('https://discordapp.com/api/users/@me', { headers: { 'Authorization': `Bearer ${access_token}` } })).json();
const payload = {
avatar: `https://cdn.discordapp.com/avatars/${user.id}/${user.avatar}.png`,
discriminator: user.discriminator,
expiresAt: Date.now() + (expires_in * 1000),
id: user.id,
username: user.username,
};
const token = this.jwt.sign({ payload });
res
.setCookie('token', token, {
domain: domain,
httpOnly: true,
path: '/',
sameSite: true,
secure: false,
})
.redirect('/');
},
});

View File

@@ -1,6 +1,6 @@
module.exports.get = {
module.exports.get = () => ({
handler: (req, res) => {
const { client } = res.context.config;
return `Hello, I am ${client.user.username}!`;
},
};
});