generated from nhcarrigan/template
feat: base64 uploads, reusable forms, Discord roles, and UX improvements #66
@@ -102,6 +102,53 @@ const authRoutes: FastifyPluginAsync = async (app) => {
|
|||||||
request
|
request
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Assign library member role if user is in Discord server but doesn't have it
|
||||||
|
const libraryRoleId = process.env.LIBRARY_ROLE_ID;
|
||||||
|
if (inDiscord && guildId && libraryRoleId) {
|
||||||
|
try {
|
||||||
|
const memberResponse = await fetch(
|
||||||
|
`https://discord.com/api/users/@me/guilds/${guildId}/member`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${tokenResult.token.access_token}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (memberResponse.ok) {
|
||||||
|
const memberData = await memberResponse.json() as { roles: string[] };
|
||||||
|
const hasLibraryRole = memberData.roles.includes(libraryRoleId);
|
||||||
|
|
||||||
|
if (!hasLibraryRole) {
|
||||||
|
const botToken = process.env.DISCORD_BOT_TOKEN;
|
||||||
|
if (botToken) {
|
||||||
|
const assignRoleResponse = await fetch(
|
||||||
|
`https://discord.com/api/v10/guilds/${guildId}/members/${userData.id}/roles/${libraryRoleId}`,
|
||||||
|
{
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bot ${botToken}`,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
if (assignRoleResponse.ok || assignRoleResponse.status === 204) {
|
||||||
|
app.log.info(`Assigned library role to user ${user.username} (${user.id})`);
|
||||||
|
} else {
|
||||||
|
app.log.error(
|
||||||
|
`Failed to assign library role to user ${user.username}: ${assignRoleResponse.status}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Don't fail the login if role assignment fails
|
||||||
|
app.log.error({ err: error }, "Error assigning library role");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set signed cookies and redirect to frontend
|
// Set signed cookies and redirect to frontend
|
||||||
reply
|
reply
|
||||||
.setCookie("auth-token", accessToken, {
|
.setCookie("auth-token", accessToken, {
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ DISCORD_GUILD_ID="op://Environment Variables - Naomi/Library/discord server id"
|
|||||||
SPONSOR_ROLE_ID="op://Environment Variables - Naomi/Library/sponsor role id"
|
SPONSOR_ROLE_ID="op://Environment Variables - Naomi/Library/sponsor role id"
|
||||||
MOD_ROLE_ID="op://Environment Variables - Naomi/Library/mod role id"
|
MOD_ROLE_ID="op://Environment Variables - Naomi/Library/mod role id"
|
||||||
STAFF_ROLE_ID="op://Environment Variables - Naomi/Library/staff role id"
|
STAFF_ROLE_ID="op://Environment Variables - Naomi/Library/staff role id"
|
||||||
|
DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Library/discord bot token"
|
||||||
|
LIBRARY_ROLE_ID="op://Environment Variables - Naomi/Library/library role id"
|
||||||
|
|
||||||
# Application URL
|
# Application URL
|
||||||
BASE_URL="op://Environment Variables - Naomi/Library/localhost url"
|
BASE_URL="op://Environment Variables - Naomi/Library/localhost url"
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ DISCORD_GUILD_ID="op://Environment Variables - Naomi/Library/discord server id"
|
|||||||
SPONSOR_ROLE_ID="op://Environment Variables - Naomi/Library/sponsor role id"
|
SPONSOR_ROLE_ID="op://Environment Variables - Naomi/Library/sponsor role id"
|
||||||
MOD_ROLE_ID="op://Environment Variables - Naomi/Library/mod role id"
|
MOD_ROLE_ID="op://Environment Variables - Naomi/Library/mod role id"
|
||||||
STAFF_ROLE_ID="op://Environment Variables - Naomi/Library/staff role id"
|
STAFF_ROLE_ID="op://Environment Variables - Naomi/Library/staff role id"
|
||||||
|
DISCORD_BOT_TOKEN="op://Environment Variables - Naomi/Library/discord bot token"
|
||||||
|
LIBRARY_ROLE_ID="op://Environment Variables - Naomi/Library/library role id"
|
||||||
|
|
||||||
# Application URL
|
# Application URL
|
||||||
BASE_URL="op://Environment Variables - Naomi/Library/base url"
|
BASE_URL="op://Environment Variables - Naomi/Library/base url"
|
||||||
|
|||||||
Reference in New Issue
Block a user