Skip to content

Commit 21b59dc

Browse files
committed
Wire user auth state into JS runtime.
1 parent 0755444 commit 21b59dc

File tree

4 files changed

+51
-7
lines changed

4 files changed

+51
-7
lines changed

client/testfixture/trailbase.d.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ export type HeaderMapType = {
44
export type PathParamsType = {
55
[key: string]: string;
66
};
7+
export type UserType = {
8+
id: string;
9+
email: string;
10+
csrf: string;
11+
};
712
export type RequestType = {
813
uri: string;
914
params: PathParamsType;
1015
headers: HeaderMapType;
16+
user?: UserType;
1117
body?: Uint8Array;
1218
};
1319
export type ResponseType = {
@@ -88,6 +94,7 @@ export type StringRequestType = {
8894
uri: string;
8995
params: PathParamsType;
9096
headers: HeaderMapType;
97+
user?: UserType;
9198
body?: string;
9299
};
93100
export type StringResponseType = {
@@ -106,6 +113,7 @@ export type JsonRequestType = {
106113
uri: string;
107114
params: PathParamsType;
108115
headers: HeaderMapType;
116+
user?: UserType;
109117
body?: object | string;
110118
};
111119
export interface JsonResponseType {
@@ -115,7 +123,7 @@ export interface JsonResponseType {
115123
}
116124
export declare function jsonHandler(f: (req: JsonRequestType) => MaybeResponse<JsonRequestType | object>): CallbackType;
117125
export declare function addRoute(method: string, route: string, callback: CallbackType): void;
118-
export declare function dispatch(method: string, route: string, uri: string, pathParams: [string, string][], headers: [string, string][], body: Uint8Array): Promise<ResponseType>;
126+
export declare function dispatch(method: string, route: string, uri: string, pathParams: [string, string][], headers: [string, string][], user: UserType | undefined, body: Uint8Array): Promise<ResponseType>;
119127
export declare function query(queryStr: string, params: unknown[]): Promise<unknown[][]>;
120128
export declare function execute(queryStr: string, params: unknown[]): Promise<number>;
121129
export type ParsedPath = {

client/testfixture/trailbase.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ export function stringHandler(f) {
374374
uri: req.uri,
375375
params: req.params,
376376
headers: req.headers,
377+
user: req.user,
377378
body: body && decodeFallback(body),
378379
});
379380
if (resp === undefined) {
@@ -411,6 +412,7 @@ export function htmlHandler(f) {
411412
uri: req.uri,
412413
params: req.params,
413414
headers: req.headers,
415+
user: req.user,
414416
body: body && decodeFallback(body),
415417
});
416418
if (resp === undefined) {
@@ -449,6 +451,7 @@ export function jsonHandler(f) {
449451
uri: req.uri,
450452
params: req.params,
451453
headers: req.headers,
454+
user: req.user,
452455
body: body && decodeFallback(body),
453456
});
454457
if (resp === undefined) {
@@ -487,7 +490,7 @@ export function addRoute(method, route, callback) {
487490
callbacks.set(`${method}:${route}`, callback);
488491
console.debug("JS: Added route:", method, route);
489492
}
490-
export async function dispatch(method, route, uri, pathParams, headers, body) {
493+
export async function dispatch(method, route, uri, pathParams, headers, user, body) {
491494
const key = `${method}:${route}`;
492495
const cb = callbacks.get(key);
493496
if (!cb) {
@@ -497,9 +500,11 @@ export async function dispatch(method, route, uri, pathParams, headers, body) {
497500
uri,
498501
params: Object.fromEntries(pathParams),
499502
headers: Object.fromEntries(headers),
503+
user: user,
500504
body,
501505
})) ?? { status: StatusCodes.OK });
502506
}
507+
globalThis.__dispatch = dispatch;
503508
export async function query(queryStr, params) {
504509
return await rustyscript.async_functions.query(queryStr, params);
505510
}
@@ -658,4 +663,3 @@ export function encodeFallback(string) {
658663
// because the original array still exists.
659664
return target.slice ? target.slice(0, at) : target.subarray(0, at);
660665
}
661-
globalThis.__dispatch = dispatch;

trailbase-core/js/src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ declare var globalThis: any;
33

44
export type HeaderMapType = { [key: string]: string };
55
export type PathParamsType = { [key: string]: string };
6+
export type UserType = {
7+
/// Base64 encoded UUIDv7 user id.
8+
id: string;
9+
/// The user's email address.
10+
email: string;
11+
/// The user's CSRF token.
12+
csrf: string;
13+
};
614
export type RequestType = {
715
uri: string;
816
params: PathParamsType;
917
headers: HeaderMapType;
18+
user?: UserType;
1019
body?: Uint8Array;
1120
};
1221
export type ResponseType = {
@@ -397,6 +406,7 @@ export type StringRequestType = {
397406
uri: string;
398407
params: PathParamsType;
399408
headers: HeaderMapType;
409+
user?: UserType;
400410
body?: string;
401411
};
402412
export type StringResponseType = {
@@ -415,6 +425,7 @@ export function stringHandler(
415425
uri: req.uri,
416426
params: req.params,
417427
headers: req.headers,
428+
user: req.user,
418429
body: body && decodeFallback(body),
419430
});
420431

@@ -463,6 +474,7 @@ export function htmlHandler(
463474
uri: req.uri,
464475
params: req.params,
465476
headers: req.headers,
477+
user: req.user,
466478
body: body && decodeFallback(body),
467479
});
468480

@@ -500,6 +512,7 @@ export type JsonRequestType = {
500512
uri: string;
501513
params: PathParamsType;
502514
headers: HeaderMapType;
515+
user?: UserType;
503516
body?: object | string;
504517
};
505518
export interface JsonResponseType {
@@ -518,6 +531,7 @@ export function jsonHandler(
518531
uri: req.uri,
519532
params: req.params,
520533
headers: req.headers,
534+
user: req.user,
521535
body: body && decodeFallback(body),
522536
});
523537

@@ -571,6 +585,7 @@ export async function dispatch(
571585
uri: string,
572586
pathParams: [string, string][],
573587
headers: [string, string][],
588+
user: UserType | undefined,
574589
body: Uint8Array,
575590
): Promise<ResponseType> {
576591
const key = `${method}:${route}`;
@@ -584,11 +599,14 @@ export async function dispatch(
584599
uri,
585600
params: Object.fromEntries(pathParams),
586601
headers: Object.fromEntries(headers),
602+
user: user,
587603
body,
588604
})) ?? { status: StatusCodes.OK }
589605
);
590606
}
591607

608+
globalThis.__dispatch = dispatch;
609+
592610
export async function query(
593611
queryStr: string,
594612
params: unknown[],
@@ -771,5 +789,3 @@ export function encodeFallback(string: string): Uint8Array {
771789
// because the original array still exists.
772790
return target.slice ? target.slice(0, at) : target.subarray(0, at);
773791
}
774-
775-
globalThis.__dispatch = dispatch;

trailbase-core/src/js/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ use axum::Router;
66
use libsql::Connection;
77
use parking_lot::Mutex;
88
use rustyscript::{init_platform, json_args, Module, Runtime};
9-
use serde::Deserialize;
9+
use serde::{Deserialize, Serialize};
1010
use serde_json::from_value;
1111
use std::collections::HashSet;
1212
use std::str::FromStr;
1313
use std::sync::{Arc, LazyLock};
1414
use thiserror::Error;
1515

1616
use crate::assets::cow_to_string;
17+
use crate::auth::user::User;
1718
use crate::js::import_provider::JsRuntimeAssets;
1819
use crate::records::sql_to_json::rows_to_json_arrays;
1920
use crate::{AppState, DataDir};
@@ -357,7 +358,7 @@ fn add_route_to_router(
357358
let method_uppercase = method.to_uppercase();
358359

359360
let route_path = route.clone();
360-
let handler = move |params: RawPathParams, req: Request| async move {
361+
let handler = move |params: RawPathParams, user: Option<User>, req: Request| async move {
361362
let (parts, body) = req.into_parts();
362363

363364
let Ok(body_bytes) = axum::body::to_bytes(body, usize::MAX).await else {
@@ -383,6 +384,20 @@ fn add_route_to_router(
383384
})
384385
.collect();
385386

387+
#[derive(Serialize)]
388+
struct JsUser {
389+
// Base64 encoded user id.
390+
id: String,
391+
email: String,
392+
csrf: String,
393+
}
394+
395+
let js_user: Option<JsUser> = user.map(|u| JsUser {
396+
id: u.id,
397+
email: u.email,
398+
csrf: u.csrf_token,
399+
});
400+
386401
#[derive(Deserialize)]
387402
struct JsResponse {
388403
headers: Option<Vec<(String, String)>>,
@@ -404,6 +419,7 @@ fn add_route_to_router(
404419
uri.to_string(),
405420
path_params,
406421
headers,
422+
js_user,
407423
body_bytes
408424
),
409425
)

0 commit comments

Comments
 (0)