feat: Add auth endpoint

This commit is contained in:
Wanjohi
2025-09-20 04:49:31 +03:00
parent a045fc9c91
commit 49286d7e72
4 changed files with 17 additions and 16 deletions

View File

@@ -1,9 +1,8 @@
import { Log } from "./utils"; import { Log } from "./utils";
import { createContext } from "./context"; import { Context } from "./context";
import { ErrorCodes, VisibleError } from "./error"; import { ErrorCodes, VisibleError } from "./error";
export namespace Actor { export namespace Actor {
export interface User { export interface User {
type: "user"; type: "user";
properties: { properties: {
@@ -11,7 +10,7 @@ export namespace Actor {
email: string; email: string;
}; };
} }
export interface Steam { export interface Steam {
type: "steam"; type: "steam";
properties: { properties: {
@@ -42,10 +41,10 @@ export namespace Actor {
export type Info = User | Public | Token | Machine | Steam; export type Info = User | Public | Token | Machine | Steam;
export const Context = createContext<Info>(); export const CurrentContext = Context.create<Info>();
export function userID() { export function userID() {
const actor = Context.use(); const actor = CurrentContext.use();
if ("userID" in actor.properties) return actor.properties.userID; if ("userID" in actor.properties) return actor.properties.userID;
throw new VisibleError( throw new VisibleError(
"authentication", "authentication",
@@ -55,7 +54,7 @@ export namespace Actor {
} }
export function steamID() { export function steamID() {
const actor = Context.use(); const actor = CurrentContext.use();
if ("steamID" in actor.properties) return actor.properties.steamID; if ("steamID" in actor.properties) return actor.properties.steamID;
throw new VisibleError( throw new VisibleError(
"authentication", "authentication",
@@ -65,7 +64,7 @@ export namespace Actor {
} }
export function user() { export function user() {
const actor = Context.use(); const actor = CurrentContext.use();
if (actor.type == "user") return actor.properties; if (actor.type == "user") return actor.properties;
throw new VisibleError( throw new VisibleError(
"authentication", "authentication",
@@ -75,7 +74,7 @@ export namespace Actor {
} }
export function teamID() { export function teamID() {
const actor = Context.use(); const actor = CurrentContext.use();
if ("teamID" in actor.properties) return actor.properties.teamID; if ("teamID" in actor.properties) return actor.properties.teamID;
throw new VisibleError( throw new VisibleError(
"authentication", "authentication",
@@ -85,7 +84,7 @@ export namespace Actor {
} }
export function fingerprint() { export function fingerprint() {
const actor = Context.use(); const actor = CurrentContext.use();
if ("fingerprint" in actor.properties) return actor.properties.fingerprint; if ("fingerprint" in actor.properties) return actor.properties.fingerprint;
throw new VisibleError( throw new VisibleError(
"authentication", "authentication",
@@ -96,7 +95,7 @@ export namespace Actor {
export function use() { export function use() {
try { try {
return Context.use(); return CurrentContext.use();
} catch { } catch {
return { type: "public", properties: {} } as Public; return { type: "public", properties: {} } as Public;
} }
@@ -117,7 +116,7 @@ export namespace Actor {
T extends Info["type"], T extends Info["type"],
Next extends (...args: any) => any, Next extends (...args: any) => any,
>(type: T, properties: Extract<Info, { type: T }>["properties"], fn: Next) { >(type: T, properties: Extract<Info, { type: T }>["properties"], fn: Next) {
return Context.provide({ type, properties } as any, () => return CurrentContext.provide({ type, properties } as any, () =>
Log.provide( Log.provide(
{ {
actor: type, actor: type,
@@ -127,4 +126,4 @@ export namespace Actor {
), ),
); );
} }
} }

View File

@@ -1,7 +1,7 @@
import { createContext } from "../context"; import { Context } from "../context";
export namespace Log { export namespace Log {
const ctx = createContext<{ const ctx = Context.create<{
tags: Record<string, any>; tags: Record<string, any>;
}>(); }>();
@@ -42,7 +42,7 @@ export namespace Log {
return result; return result;
}, },
tag(key: string, value: string) { tag(key: string, value: string) {
// Immutable update: return a fresh logger with updated tags // Immutable update: return a fresh logger with updated tags
return Log.create({ ...tags, [key]: value }); return Log.create({ ...tags, [key]: value });
}, },
clone() { clone() {
@@ -73,4 +73,4 @@ export namespace Log {
return { tags: {} }; return { tags: {} };
} }
} }
} }

View File

@@ -31,6 +31,7 @@ declare module "sst" {
import * as cloudflare from "@cloudflare/workers-types"; import * as cloudflare from "@cloudflare/workers-types";
declare module "sst" { declare module "sst" {
export interface Resource { export interface Resource {
"Auth": cloudflare.Service
"AuthStorage": cloudflare.KVNamespace "AuthStorage": cloudflare.KVNamespace
} }
} }

1
sst-env.d.ts vendored
View File

@@ -31,6 +31,7 @@ declare module "sst" {
import * as cloudflare from "@cloudflare/workers-types"; import * as cloudflare from "@cloudflare/workers-types";
declare module "sst" { declare module "sst" {
export interface Resource { export interface Resource {
"Auth": cloudflare.Service
"AuthStorage": cloudflare.KVNamespace "AuthStorage": cloudflare.KVNamespace
} }
} }