118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
/// <reference lib="deno.ns" />
|
|
|
|
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
|
|
import { oakCors } from "https://deno.land/x/cors/mod.ts";
|
|
import { CookieStore, Session } from "https://deno.land/x/oak_sessions/mod.ts";
|
|
import data from "./data.json" with { type: "json" };
|
|
import routeStaticFilesFrom from "./util/routeStaticFilesFrom.ts";
|
|
|
|
import SteamAuth from "https://deno.land/x/deno_steam_openid@0.0.1/mod.ts";
|
|
import "https://deno.land/x/dotenv/load.ts";
|
|
|
|
type AppState = {
|
|
session: Session;
|
|
};
|
|
|
|
const router = new Router<AppState>();
|
|
|
|
const hostname = "localhost";
|
|
const authPath = "/auth";
|
|
const port = 8000;
|
|
|
|
export interface SteamUser {
|
|
steamid: string;
|
|
personaname: string;
|
|
profileurl: string;
|
|
avatar: string;
|
|
avatarmedium: string;
|
|
avatarfull: string;
|
|
personastate: number;
|
|
communityvisibilitystate: number;
|
|
profilestate: number;
|
|
lastlogoff: number;
|
|
commentpermission: number;
|
|
realname: string;
|
|
primaryclanid: string;
|
|
timecreated: number;
|
|
personastateflags: number;
|
|
loccountrycode: string;
|
|
locstatecode: string;
|
|
loccityid: number;
|
|
}
|
|
|
|
export const Steam = new SteamAuth({
|
|
realm: `http://${hostname}:${port}`,
|
|
returnUrl: `http://${hostname}:${port}${authPath}/return`,
|
|
apiKey: Deno.env.get("STEAM_API_KEY"),
|
|
});
|
|
|
|
router.get(`${authPath}/login`, async (ctx) => {
|
|
try {
|
|
const redirectUrl = await Steam.getRedirectUrl() as string;
|
|
ctx.response.body = { url: redirectUrl };
|
|
} catch (e) {
|
|
ctx.response.body = `Error: ${e}`;
|
|
}
|
|
});
|
|
|
|
router.get(`${authPath}/return`, async (ctx) => {
|
|
try {
|
|
const user = await Steam.authenticate(ctx) as SteamUser;
|
|
ctx.state.session.set("steamid", user.steamid);
|
|
ctx.state.session.set("user", user);
|
|
ctx.response.redirect(`http://localhost:5173/signedIn`);
|
|
} catch (e) {
|
|
ctx.response.body = `Error: ${e}`;
|
|
}
|
|
});
|
|
|
|
router.get("/api/session", async (ctx) => {
|
|
if (ctx.state.session.has("user")) {
|
|
const user = await ctx.state.session.get("user") as SteamUser;
|
|
ctx.response.body = { user };
|
|
} else {
|
|
ctx.response.status = 401;
|
|
ctx.response.body = { error: "Not authenticated" };
|
|
}
|
|
});
|
|
|
|
router.get("/api/dinosaurs", (context) => {
|
|
context.response.body = data;
|
|
});
|
|
|
|
router.get("/api/dinosaurs/:dinosaur", (context) => {
|
|
if (!context?.params?.dinosaur) {
|
|
context.response.body = "No dinosaur name provided.";
|
|
}
|
|
|
|
const dinosaur = data.find((item) =>
|
|
item.name.toLowerCase() === context.params.dinosaur.toLowerCase()
|
|
);
|
|
|
|
context.response.body = dinosaur ? dinosaur : "No dinosaur found.";
|
|
});
|
|
|
|
const store = new CookieStore(Deno.env.get("SESSION_COOKIE_KEY") as string, {
|
|
sessionDataCookieName: "warbforums_sessionData",
|
|
cookieSetDeleteOptions: {
|
|
sameSite: "none",
|
|
secure: true,
|
|
},
|
|
});
|
|
|
|
const app = new Application<AppState>();
|
|
app.use(oakCors());
|
|
app.use(Session.initMiddleware(store) as any);
|
|
app.use(router.routes());
|
|
app.use(router.allowedMethods());
|
|
app.use(routeStaticFilesFrom([
|
|
`${Deno.cwd()}/dist`,
|
|
`${Deno.cwd()}/public`,
|
|
]));
|
|
|
|
app.addEventListener("error", (evt) => {
|
|
console.log(evt.error);
|
|
});
|
|
|
|
await app.listen({ port: 8000 });
|