mirror of
https://github.com/Hessenuk/DiscordTickets.git
synced 2025-09-06 18:21:25 +03:00
Add authentication
This commit is contained in:
@@ -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],
|
||||
});
|
@@ -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],
|
||||
});
|
29
src/routes/auth/callback.js
Normal file
29
src/routes/auth/callback.js
Normal 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('/');
|
||||
},
|
||||
});
|
@@ -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}!`;
|
||||
},
|
||||
};
|
||||
});
|
Reference in New Issue
Block a user