Skip to content

Commit

Permalink
Merge pull request #2 from WaifuAPI/staging
Browse files Browse the repository at this point in the history
Added a Beta Tester check
  • Loading branch information
kyrea authored Aug 9, 2023
2 parents 4d04d07 + 73c19d9 commit 7897999
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const nextConfig = {
API_URL: process.env.API_URL,
BOT_TOKEN: process.env.BOT_TOKEN,
GUILD_ID: process.env.GUILD_ID,
BETA_ROLE_ID: process.env.BETA_ROLE_ID
},
};

Expand Down
64 changes: 39 additions & 25 deletions src/pages/api/auth/discord.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import axios from "axios";

async function checkGuildMembershipAndRole(accessToken) {
const guildsResponse = await axios.get(
"https://discord.com/api/v10/users/@me/guilds",
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);

const targetGuildId = process.env.GUILD_ID; // Replace with the actual guild ID
const userGuild = guildsResponse.data.find(
(guild) => guild.id === targetGuildId
);

if (!userGuild) {
// User is not in the guild, add them
await axios.put(
`https://discord.com/api/v10/guilds/${targetGuildId}/members/@me`,
{},
{
headers: {
Authorization: `Bot ${process.env.BOT_TOKEN}`, // Replace with your bot token
},
}
);
}

// Check if the user has the required role in the guild
const requiredRole = process.env.BETA_ROLE_ID; // Replace with the actual role name
const hasRequiredRole = userGuild?.roles.includes(requiredRole);

return hasRequiredRole;
}

export default async function handler(req, res) {
if (req.method !== "POST") {
return res.status(405).end(); // Method Not Allowed
Expand Down Expand Up @@ -27,32 +62,11 @@ export default async function handler(req, res) {

const accessToken = response.data.access_token;

// Step 2: Get user's guilds (servers) using the access token
const guildsResponse = await axios.get(
"https://discord.com/api/v10/users/@me/guilds",
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
}
);

// Find the specific guild by its ID
const targetGuildId = process.env.GUILD_ID; // Replace with the actual guild ID
const userGuild = guildsResponse.data.find(
(guild) => guild.id === targetGuildId
);
const hasRequiredRole = await checkGuildMembershipAndRole(accessToken);

if (!userGuild) {
await axios.put(
`https://discord.com/api/v10/guilds/${targetGuildId}/members/@me`,
{},
{
headers: {
Authorization: `Bot ${process.env.BOT_TOKEN}`, // Replace with your bot token
},
}
);
if (!hasRequiredRole) {
// Redirect the user to a page indicating they are not a beta tester
return res.redirect("/not-beta-tester");
}

res.status(200).json({ access_token: accessToken });
Expand Down
17 changes: 17 additions & 0 deletions src/pages/not-beta-tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Link from 'next/link';

const NotBetaTester = () => {
return (
<div className="flex flex-col items-center justify-center min-h-screen bg-white">
<div className="max-w-md p-8 mx-auto bg-white rounded-lg shadow-lg">
<h1 className="text-2xl font-semibold mb-4">You are not a beta tester</h1>
<p className="text-gray-700 mb-6">Sorry, you do not have the required role to access this page.</p>
<Link href="/">
<a className="bg-blue-500 text-white px-4 py-2 rounded-lg hover:bg-blue-600 transition duration-300">Home</a>
</Link>
</div>
</div>
);
};

export default NotBetaTester;

0 comments on commit 7897999

Please sign in to comment.