mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 08:45:38 +02:00
feat: Add auth endpoint
This commit is contained in:
@@ -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: {
|
||||||
@@ -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,
|
||||||
|
|||||||
@@ -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>;
|
||||||
}>();
|
}>();
|
||||||
|
|
||||||
|
|||||||
1
cloud/packages/functions/sst-env.d.ts
vendored
1
cloud/packages/functions/sst-env.d.ts
vendored
@@ -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
1
sst-env.d.ts
vendored
@@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user