Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Beta Tester check #2

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Loading