feat-wip: steam sign in provided by ctx
This commit is contained in:
parent
8e77e76069
commit
fe02a090d3
2
.gitignore
vendored
2
.gitignore
vendored
@ -22,3 +22,5 @@ dist-ssr
|
|||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
*.sln
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
|
.env
|
||||||
|
|||||||
92
api/main.ts
92
api/main.ts
@ -1,9 +1,80 @@
|
|||||||
import { Application, Router } from "@oak/oak";
|
/// <reference lib="deno.ns" />
|
||||||
import { oakCors } from "@tajpouria/cors";
|
|
||||||
|
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 data from "./data.json" with { type: "json" };
|
||||||
import routeStaticFilesFrom from "./util/routeStaticFilesFrom.ts";
|
import routeStaticFilesFrom from "./util/routeStaticFilesFrom.ts";
|
||||||
|
|
||||||
const router = new Router();
|
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) => {
|
router.get("/api/dinosaurs", (context) => {
|
||||||
context.response.body = data;
|
context.response.body = data;
|
||||||
@ -21,8 +92,17 @@ router.get("/api/dinosaurs/:dinosaur", (context) => {
|
|||||||
context.response.body = dinosaur ? dinosaur : "No dinosaur found.";
|
context.response.body = dinosaur ? dinosaur : "No dinosaur found.";
|
||||||
});
|
});
|
||||||
|
|
||||||
const app = new Application();
|
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(oakCors());
|
||||||
|
app.use(Session.initMiddleware(store) as any);
|
||||||
app.use(router.routes());
|
app.use(router.routes());
|
||||||
app.use(router.allowedMethods());
|
app.use(router.allowedMethods());
|
||||||
app.use(routeStaticFilesFrom([
|
app.use(routeStaticFilesFrom([
|
||||||
@ -30,4 +110,8 @@ app.use(routeStaticFilesFrom([
|
|||||||
`${Deno.cwd()}/public`,
|
`${Deno.cwd()}/public`,
|
||||||
]));
|
]));
|
||||||
|
|
||||||
|
app.addEventListener("error", (evt) => {
|
||||||
|
console.log(evt.error);
|
||||||
|
});
|
||||||
|
|
||||||
await app.listen({ port: 8000 });
|
await app.listen({ port: 8000 });
|
||||||
|
|||||||
41
src/App.tsx
41
src/App.tsx
@ -1,15 +1,48 @@
|
|||||||
|
import { createContext, useEffect, useState } from "react";
|
||||||
import React, { BrowserRouter, Route, Routes } from "react-router-dom";
|
import React, { BrowserRouter, Route, Routes } from "react-router-dom";
|
||||||
import Index from "./pages/index.tsx";
|
import Index from "./pages/index.tsx";
|
||||||
import Dinosaur from "./pages/Dinosaur.tsx";
|
import Dinosaur from "./pages/Dinosaur.tsx";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
import AuthSuccess from "./pages/AuthSuccess.tsx";
|
||||||
|
import type { SteamUser } from "../api/main.ts";
|
||||||
|
import Profile from "./pages/Profile.tsx";
|
||||||
|
|
||||||
|
export const UserContext = createContext<{
|
||||||
|
user: SteamUser | undefined;
|
||||||
|
setUser: any;
|
||||||
|
}>({ user: undefined, setUser: () => {} });
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [user, setUser] = useState<SteamUser>();
|
||||||
|
const value = { user, setUser };
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/session", {
|
||||||
|
credentials: "include",
|
||||||
|
});
|
||||||
|
const res = await response.json();
|
||||||
|
if (res.user && res.user.steamid) {
|
||||||
|
setUser(res.user as SteamUser);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching user:", error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Routes>
|
<UserContext.Provider value={value}>
|
||||||
<Route path="/" element={<Index />} />
|
<h1>Welcome to the Dinosaur App</h1>
|
||||||
<Route path="/:selectedDinosaur" element={<Dinosaur />} />
|
<Routes>
|
||||||
</Routes>
|
<Route path="/" element={<Index />} />
|
||||||
|
<Route path="/signedIn" element={<AuthSuccess />} />
|
||||||
|
<Route path="/users/:steamId" element={<Profile />} />
|
||||||
|
<Route path="/:selectedDinosaur" element={<Dinosaur />} />
|
||||||
|
</Routes>
|
||||||
|
</UserContext.Provider>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
14
src/pages/AuthSuccess.tsx
Normal file
14
src/pages/AuthSuccess.tsx
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
import React, { useEffect } from "react";
|
||||||
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
||||||
|
const AuthSuccess = () => {
|
||||||
|
const nav = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
nav("/");
|
||||||
|
}, [nav]);
|
||||||
|
|
||||||
|
return <div>Signing you in...</div>;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default AuthSuccess;
|
||||||
53
src/pages/Profile.tsx
Normal file
53
src/pages/Profile.tsx
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import React, { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
import { Link } from "react-router-dom";
|
||||||
|
import type { SteamUser } from "../../api/main.ts";
|
||||||
|
|
||||||
|
const Profile = () => {
|
||||||
|
const [user, setUser] = useState<SteamUser>();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/session", {
|
||||||
|
credentials: "include",
|
||||||
|
});
|
||||||
|
const res = await response.json();
|
||||||
|
if (res.user && res.user.steamid) {
|
||||||
|
setUser(res.user as SteamUser);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching user:", error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const printUser = async () => {
|
||||||
|
console.log(user);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLogin = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:8000/auth/login");
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to fetch authorization URL");
|
||||||
|
}
|
||||||
|
const { url } = await response.json();
|
||||||
|
globalThis.location.href = url;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during login:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main>
|
||||||
|
<h1>Profile</h1>
|
||||||
|
<button onClick={user ? printUser : handleLogin}>
|
||||||
|
Sign in with Steam
|
||||||
|
</button>
|
||||||
|
{user && <p>Welcome, {user.personaname}!</p>}
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Profile;
|
||||||
@ -1,9 +1,12 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React, { useContext, useEffect, useState } from "react";
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
import { Dino } from "../types.ts";
|
import { Dino } from "../types.ts";
|
||||||
|
import type { SteamUser } from "../../api/main.ts";
|
||||||
|
import { UserContext } from "../App.tsx";
|
||||||
|
|
||||||
export default function Index() {
|
export default function Index() {
|
||||||
const [dinosaurs, setDinosaurs] = useState<Dino[]>([]);
|
const [dinosaurs, setDinosaurs] = useState<Dino[]>([]);
|
||||||
|
const { user } = useContext(UserContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
@ -13,10 +16,47 @@ export default function Index() {
|
|||||||
})();
|
})();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
/*useEffect(() => {
|
||||||
|
(async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("/api/session", {
|
||||||
|
credentials: "include",
|
||||||
|
});
|
||||||
|
const res = await response.json();
|
||||||
|
if (res.user && res.user.steamid) {
|
||||||
|
setUser(res.user as SteamUser);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error fetching user:", error);
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
}, []);*/
|
||||||
|
|
||||||
|
const printUser = async () => {
|
||||||
|
console.log(user);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleLogin = async () => {
|
||||||
|
try {
|
||||||
|
const response = await fetch("http://localhost:8000/auth/login");
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error("Failed to fetch authorization URL");
|
||||||
|
}
|
||||||
|
const { url } = await response.json();
|
||||||
|
globalThis.location.href = url;
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error during login:", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<h1>Dinosaur Home</h1>
|
<h1>Dinosaur Home</h1>
|
||||||
<p>Click on a dinosaur below to learn more.</p>
|
<p>Click on a dinosaur below to learn more.</p>
|
||||||
|
<button onClick={user ? printUser : handleLogin}>
|
||||||
|
Sign in with Steam
|
||||||
|
</button>
|
||||||
|
{user && <p>Welcome, {user.personaname}!</p>}
|
||||||
{dinosaurs.map((dinosaur: Dino) => {
|
{dinosaurs.map((dinosaur: Dino) => {
|
||||||
return (
|
return (
|
||||||
<Link
|
<Link
|
||||||
|
|||||||
@ -10,6 +10,10 @@ export default defineConfig({
|
|||||||
target: "http://localhost:8000",
|
target: "http://localhost:8000",
|
||||||
changeOrigin: true,
|
changeOrigin: true,
|
||||||
},
|
},
|
||||||
|
"/auth": {
|
||||||
|
target: "http://localhost:8000",
|
||||||
|
changeOrigin: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user