Skip to content

Commit

Permalink
Merge pull request #1 from Bolado/dev
Browse files Browse the repository at this point in the history
feat: Discord Integration, Build Saving & Profile Features
  • Loading branch information
Bolado authored Nov 16, 2024
2 parents dda9f4e + 6308292 commit 737206c
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 39 deletions.
12 changes: 12 additions & 0 deletions backend/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import fetch from "node-fetch";

const router = express.Router();

// This route is used to redirect the user to Discord's login page
router.get("/login", (req, res) => {
const state = crypto.randomBytes(16).toString("hex");
req.session.state = state;
Expand All @@ -20,6 +21,7 @@ router.get("/login", (req, res) => {
res.redirect(`https://discord.com/api/oauth2/authorize?${params}`);
});

// This route is used to exchange the code for a token
router.get("/authorize", async (req, res) => {
if (req.query.state !== req.session.state) {
return res.status(400).json({ error: "State mismatch" });
Expand Down Expand Up @@ -64,4 +66,14 @@ router.get("/authorize", async (req, res) => {
}
});

// This route is used to check if the user is logged in (wip , have to check if the token is valid)
router.get("/status", (req, res) => {
const token = req.cookies.token;
if (!token) {
return res.status(401).json({ error: "Unauthorized" });
}

return res.json({ status: "logged in" });
});

export default router;
35 changes: 29 additions & 6 deletions backend/routes/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,28 @@ export default function (db) {
try {
if (user?.user_id && id) {
const existingBuild = await db.collection("builds").findOne({
_id: new ObjectId(id),
_id: ObjectId.createFromHexString(id),
});

if (existingBuild?.user_id === user.user_id) {
await db
.collection("builds")
.updateOne({ _id: new ObjectId(id) }, { $set: { build } });
await db.collection("builds").updateOne(
{ _id: ObjectId.createFromHexString(id) },
{
$set: {
build,
timestamp: new Date(),
},
}
);
return res.json({ status: "updated", id });
}
}

const result = await db.collection("builds").insertOne({
build,
...(user ? { user_id: user.user_id } : { timestamp: new Date() }),
...(user
? { user_id: user.user_id, timestamp: new Date() }
: { timestamp: new Date() }),
});

res.json({ status: "created", id: result.insertedId.toString() });
Expand All @@ -44,10 +52,25 @@ export default function (db) {
}
});

router.get("/saved-builds", async (req, res) => {
const user = getUser(req);
if (!user?.user_id) {
return res.status(401).json({ error: "Unauthorized" });
}

const builds = await db
.collection("builds")
.find({ user_id: user.user_id })
.sort({ timestamp: -1 })
.toArray();

res.json(builds);
});

router.get("/:id", async (req, res) => {
try {
const build = await db.collection("builds").findOne({
_id: new ObjectId(req.params.id),
_id: ObjectId.createFromHexString(req.params.id),
});

if (build) {
Expand Down
69 changes: 69 additions & 0 deletions frontend/src/components/global-header.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import React from "react";
import { Link, Outlet, useLoaderData } from "react-router-dom";

const GlobalHeader = () => {
// get loader data from the router
const logged = useLoaderData();
return (
<>
<header className="flex justify-between items-center p-3">
<h1 className="cinzel text-xl">SW:LC Rune Builder</h1>
{logged ? (
<Link to="/profile">
<ProfileButton />
</Link>
) : (
<Link to="/login">
<DiscordButton />
</Link>
)}
</header>
<Outlet />
</>
);
};

export default GlobalHeader;

// discord button
const DiscordButton = () => {
return (
<button class="flex items-center bg-white border border-gray-300 rounded-lg shadow-md px-3 py-1 text-sm font-medium text-gray-800 hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500">
<svg
class="h-6 w-6 mr-2"
xmlns="http://www.w3.org/2000/svg"
width="800px"
height="800px"
viewBox="0 -28.5 256 256"
version="1.1"
preserveAspectRatio="xMidYMid"
>
<g>
<path
d="M216.856339,16.5966031 C200.285002,8.84328665 182.566144,3.2084988 164.041564,0 C161.766523,4.11318106 159.108624,9.64549908 157.276099,14.0464379 C137.583995,11.0849896 118.072967,11.0849896 98.7430163,14.0464379 C96.9108417,9.64549908 94.1925838,4.11318106 91.8971895,0 C73.3526068,3.2084988 55.6133949,8.86399117 39.0420583,16.6376612 C5.61752293,67.146514 -3.4433191,116.400813 1.08711069,164.955721 C23.2560196,181.510915 44.7403634,191.567697 65.8621325,198.148576 C71.0772151,190.971126 75.7283628,183.341335 79.7352139,175.300261 C72.104019,172.400575 64.7949724,168.822202 57.8887866,164.667963 C59.7209612,163.310589 61.5131304,161.891452 63.2445898,160.431257 C105.36741,180.133187 151.134928,180.133187 192.754523,160.431257 C194.506336,161.891452 196.298154,163.310589 198.110326,164.667963 C191.183787,168.842556 183.854737,172.420929 176.223542,175.320965 C180.230393,183.341335 184.861538,190.991831 190.096624,198.16893 C211.238746,191.588051 232.743023,181.531619 254.911949,164.955721 C260.227747,108.668201 245.831087,59.8662432 216.856339,16.5966031 Z M85.4738752,135.09489 C72.8290281,135.09489 62.4592217,123.290155 62.4592217,108.914901 C62.4592217,94.5396472 72.607595,82.7145587 85.4738752,82.7145587 C98.3405064,82.7145587 108.709962,94.5189427 108.488529,108.914901 C108.508531,123.290155 98.3405064,135.09489 85.4738752,135.09489 Z M170.525237,135.09489 C157.88039,135.09489 147.510584,123.290155 147.510584,108.914901 C147.510584,94.5396472 157.658606,82.7145587 170.525237,82.7145587 C183.391518,82.7145587 193.761324,94.5189427 193.539891,108.914901 C193.539891,123.290155 183.391518,135.09489 170.525237,135.09489 Z"
fill="#5865F2"
fill-rule="nonzero"
></path>
</g>
</svg>

<span>Login with Discord</span>
</button>
);
};

// profile button (only visible when logged in)
const ProfileButton = () => {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
height="2rem"
viewBox="0 -960 960 960"
width="2rem"
fill="#e8eaed"
className="hover:fill-blue-500"
>
<path d="M234-276q51-39 114-61.5T480-360q69 0 132 22.5T726-276q35-41 54.5-93T800-480q0-133-93.5-226.5T480-800q-133 0-226.5 93.5T160-480q0 59 19.5 111t54.5 93Zm246-164q-59 0-99.5-40.5T340-580q0-59 40.5-99.5T480-720q59 0 99.5 40.5T620-580q0 59-40.5 99.5T480-440Zm0 360q-83 0-156-31.5T197-197q-54-54-85.5-127T80-480q0-83 31.5-156T197-763q54-54 127-85.5T480-880q83 0 156 31.5T763-763q54 54 85.5 127T880-480q0 83-31.5 156T763-197q-54 54-127 85.5T480-80Zm0-80q53 0 100-15.5t86-44.5q-39-29-86-44.5T480-280q-53 0-100 15.5T294-220q39 29 86 44.5T480-160Zm0-360q26 0 43-17t17-43q0-26-17-43t-43-17q-26 0-43 17t-17 43q0 26 17 43t43 17Zm0-60Zm0 360Z" />
</svg>
);
};
89 changes: 57 additions & 32 deletions frontend/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,69 @@ import reportWebVitals from "./reportWebVitals";
import ErrorPage from "./pages/error-page";
import Builder from "./pages/build/builder";
import Callback from "./pages/Callback";
import GlobalHeader from "./components/global-header";
import { hasTokenCookie } from "./utils/queries";
import Profile from "./pages/profile";

const router = createBrowserRouter([
{
path: "/",
element: <App />,
errorElement: <ErrorPage />,
},
{
path: "/login",
element: <GlobalHeader />,
loader: async () => {
// redirect to /api/login
window.location.href = "/api/login";
return null;
return hasTokenCookie();
},
element: null,
},
{
path: "/build/new",
element: <Builder />,
errorElement: <ErrorPage />,
},
{
path: "/build/:id",
loader: async ({ params }) => {
const response = await fetch(`/api/build/${params.id}`);
if (!response.ok) {
console.error("Build not found");
return redirect("/build/new");
}
const data = await response.json();
return data;
},
element: <Builder />,
errorElement: <ErrorPage />,
},
{
path: "/callback",
element: <Callback />,
children: [
{
path: "/",
element: <App />,
errorElement: <ErrorPage />,
},
{
path: "/login",
loader: async () => {
// redirect to /api/login
window.location.href = "/api/login";
return null;
},
element: null,
},
{
path: "/profile",
loader: async () => {
const response = await fetch("/api/build/saved-builds");
if (!response.ok) {
console.error("Unauthorized");
return redirect("/login");
}
const data = await response.json();
return data;
},
element: <Profile />,
},
{
path: "/build/new",
element: <Builder />,
errorElement: <ErrorPage />,
},
{
path: "/build/:id",
loader: async ({ params }) => {
const response = await fetch(`/api/build/${params.id}`);
if (!response.ok) {
console.error("Build not found");
return redirect("/build/new");
}
const data = await response.json();
return data;
},
element: <Builder />,
errorElement: <ErrorPage />,
},
{
path: "/callback",
element: <Callback />,
},
],
},
]);

Expand Down
14 changes: 14 additions & 0 deletions frontend/src/pages/build/builder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const PERCENTAGE_STATS = ["acc", "cdd", "cr", "cd", "res", "pen"];

// Initial state structure for a build
export const initialBuildState = {
name: "",
monster: null,
currentRuneSlot: 1,
runeSet: {
Expand Down Expand Up @@ -54,6 +55,7 @@ function Builder() {

const [build, setBuild] = React.useState({
...initialBuildState,
name: `build-${Date.now()}`,
currentRuneSlot: 1, // Ensure this exists in initial state
});

Expand Down Expand Up @@ -237,6 +239,18 @@ function Builder() {

{/* Rune Selection Panel */}
<div className="lg:basis-2/3 bg-slate-500/10 m-4 p-4 rounded-md flex flex-wrap justify-center">
<div className="w-full justify-center flex flex-col">
<p className="block font-bold text-lg text-center">Build Name:</p>
<input
className="p-2 rounded-md text-black font-bold mx-auto text-center"
type="text"
value={build.name}
placeholder="Enter build name"
onChange={(e) =>
setBuild((prev) => ({ ...prev, name: e.target.value }))
}
/>
</div>
<div className="flex flex-row gap-2 mb-4 w-full">
<div className="basis-1/2">
<p className="block font-bold text-lg">Rune Set:</p>
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/pages/error-page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export default function ErrorPage() {
console.error(error);

return (
<div id="error-page" className="h-full w-full flex justify-center items-center flex-col gap-6">
<div
id="error-page"
className="min-h-[100vh] flex justify-center items-center flex-col gap-6"
>
<h1 className="text-4xl">Oops!</h1>
<p className="text-xl">Sorry, an unexpected error has occurred.</p>
<p className="text-lg">
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/pages/profile.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Link, useLoaderData } from "react-router-dom";
import { deserializeBuildState } from "../utils/serializer";

const Profile = () => {
const builds = useLoaderData();
return (
<div className="max-w-7xl mx-auto">
<h2 className="cinzel text-4xl text-center my-8">Your Builds</h2>
<ul>
{builds.map((build) => {
// deserialize the build object
const b = deserializeBuildState(build.build);
return (
<li
className="p-2 m-2 hover:bg-blue-500/30 rounded-md"
key={build._id}
>
<Link to={`/build/${build._id}`}>
<h3 className="cinzel text-xl font-bold mr-4 ">{b.name}</h3>
<h3 className="text-md mr-4">Monster: {b.monster.name}</h3>
</Link>
</li>
);
})}
</ul>
</div>
);
};

export default Profile;
11 changes: 11 additions & 0 deletions frontend/src/utils/queries.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Use endpoint /api/status to check if the user is logged in
export async function hasTokenCookie() {
return fetch("/api/status")
.then((response) => {
if (response.ok) {
return true;
}
return false;
})
.catch(() => false);
}
8 changes: 8 additions & 0 deletions frontend/src/utils/serializer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { initialBuildState } from "../pages/build/builder";
export const serializeBuildState = (state) => {
// Create minimal representation of state
const compressedState = {
// Build name
n: state.name,

// Monster ID only
m: state.monster,

Expand Down Expand Up @@ -47,6 +50,11 @@ export const deserializeBuildState = (
// Create a deep clone of the base state
const state = JSON.parse(JSON.stringify(baseState));

// Update name if present in decoded data
if (decoded.n) {
state.name = decoded.n;
}

// Update monster if present in decoded data
if (decoded.m) {
state.monster = decoded.m;
Expand Down

0 comments on commit 737206c

Please sign in to comment.