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

View File

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