-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
522 additions
and
329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...es/server/src/query/leaderboard-router.ts → ...src/api/leaderboard/leaderboard-router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface JwtCreationToken { | ||
email: string | ||
iat: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface JwtToken { | ||
id: number | ||
iat: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface User { | ||
id: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/server/src/framework/helper/user-from-authorization-header.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import jwt from "@tsndr/cloudflare-worker-jwt" | ||
import { JwtToken } from "../../domain/auth/jwt-token" | ||
import { User } from "../../domain/auth/user" | ||
import { Env } from "../../env" | ||
|
||
export function userFromAuthorizationHeader(env: Env, authorization: string | null): User | null { | ||
try { | ||
if (authorization && authorization !== "undefined") { | ||
console.log("authorization", authorization) | ||
if (!jwt.verify(authorization, env.JWT_SECRET)) { | ||
return null | ||
} | ||
|
||
const payload = jwt.decode<JwtToken>(authorization).payload | ||
|
||
if (!payload) { | ||
return null | ||
} | ||
|
||
return { id: payload.id } | ||
} | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { leaderboardRouter } from "../api/leaderboard/leaderboard-router" | ||
import { validateReplayProcedure } from "../api/leaderboard/validate-replay" | ||
import { userRouter } from "../api/user/user-router" | ||
import { worldRouter } from "../api/world/world-router" | ||
import { router } from "./trpc" | ||
|
||
export const appRouter = router({ | ||
user: userRouter, | ||
world: worldRouter, | ||
replay: leaderboardRouter, | ||
validateReplay: validateReplayProcedure, | ||
}) | ||
|
||
export type AppRouter = typeof appRouter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { inferAsyncReturnType, initTRPC } from "@trpc/server" | ||
import { FetchCreateContextFnOptions } from "@trpc/server/adapters/fetch" | ||
import { drizzle } from "drizzle-orm/d1" | ||
import { OAuth2Client } from "oslo/oauth2" | ||
import superjson from "superjson" | ||
import { Env } from "../env" | ||
import { userFromAuthorizationHeader } from "./helper/user-from-authorization-header" | ||
|
||
export const createContext = | ||
(env: Env) => | ||
({ req }: FetchCreateContextFnOptions) => { | ||
const user = userFromAuthorizationHeader(env, req.headers.get("Authorization")) | ||
|
||
return { | ||
db: drizzle(env.DB), | ||
oauth: new OAuth2Client( | ||
env.AUTH_GOOGLE_CLIENT_ID, | ||
"https://accounts.google.com/o/oauth2/auth", | ||
"https://accounts.google.com/o/oauth2/token", | ||
{ | ||
redirectURI: `${env.CLIENT_URL}`, | ||
}, | ||
), | ||
env, | ||
user, | ||
} | ||
} | ||
|
||
type Context = inferAsyncReturnType<inferAsyncReturnType<typeof createContext>> | ||
|
||
const t = initTRPC.context<Context>().create({ | ||
transformer: superjson, | ||
}) | ||
|
||
export const middleware = t.middleware | ||
export const router = t.router | ||
|
||
export const logger = middleware(async opts => { | ||
try { | ||
// format date as HH:MM:SS | ||
console.log(`Req ${new Date().toISOString()}: ${opts.path}`) | ||
return await opts.next() | ||
} catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
}) | ||
|
||
export const publicProcedure = t.procedure.use(logger) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.