fixed auth strategy toggle, updated astro/remix

This commit is contained in:
dswbx
2025-01-17 18:08:23 +01:00
parent baab70f9da
commit 7ddcfc89b4
9 changed files with 42 additions and 28 deletions

View File

@@ -15,7 +15,7 @@ if (clean) {
let types_running = false; let types_running = false;
function buildTypes() { function buildTypes() {
if (types_running) return; if (types_running || !types) return;
types_running = true; types_running = true;
Bun.spawn(["bun", "build:types"], { Bun.spawn(["bun", "build:types"], {

View File

@@ -128,15 +128,17 @@ export class Api {
}; };
} }
async getVerifiedAuthState(force?: boolean): Promise<AuthState> { async getVerifiedAuthState(): Promise<AuthState> {
if (force === true || !this.verified) { await this.verifyAuth();
await this.verifyAuth();
}
return this.getAuthState(); return this.getAuthState();
} }
async verifyAuth() { async verifyAuth() {
if (!this.token) {
this.markAuthVerified(false);
return;
}
try { try {
const res = await this.auth.me(); const res = await this.auth.me();
if (!res.ok || !res.body.user) { if (!res.ok || !res.body.user) {

View File

@@ -226,8 +226,16 @@ export class AppAuth extends Module<typeof authConfigSchema> {
private toggleStrategyValueVisibility(visible: boolean) { private toggleStrategyValueVisibility(visible: boolean) {
const field = this.getUsersEntity().field("strategy_value")!; const field = this.getUsersEntity().field("strategy_value")!;
field.config.hidden = !visible; if (visible) {
field.config.fillable = visible; field.config.hidden = false;
field.config.fillable = true;
} else {
// reset to normal
const template = AppAuth.usersFields.strategy_value.config;
field.config.hidden = template.hidden;
field.config.fillable = template.fillable;
}
// @todo: think about a PasswordField that automatically hashes on save? // @todo: think about a PasswordField that automatically hashes on save?
} }

View File

@@ -33,7 +33,6 @@ export class AuthController extends Controller {
const name = strategy.getName(); const name = strategy.getName();
const { create, change } = actions; const { create, change } = actions;
const em = this.auth.em; const em = this.auth.em;
const mutator = em.mutator(this.auth.config.entity_name as "users");
if (create) { if (create) {
hono.post( hono.post(
@@ -46,10 +45,9 @@ export class AuthController extends Controller {
skipMark: true skipMark: true
}); });
const processed = (await create.preprocess?.(valid)) ?? valid; const processed = (await create.preprocess?.(valid)) ?? valid;
console.log("processed", processed);
// @todo: check processed for "role" and check permissions // @todo: check processed for "role" and check permissions
const mutator = em.mutator(this.auth.config.entity_name as "users");
mutator.__unstable_toggleSystemEntityCreation(false); mutator.__unstable_toggleSystemEntityCreation(false);
const { data: created } = await mutator.insertOne({ const { data: created } = await mutator.insertOne({
...processed, ...processed,
@@ -98,7 +96,7 @@ export class AuthController extends Controller {
hono.get("/me", auth(), async (c) => { hono.get("/me", auth(), async (c) => {
if (this.auth.authenticator.isUserLoggedIn()) { if (this.auth.authenticator.isUserLoggedIn()) {
return c.json({ user: await this.auth.authenticator.getUser() }); return c.json({ user: this.auth.authenticator.getUser() });
} }
return c.json({ user: null }, 403); return c.json({ user: null }, 403);

View File

@@ -1,3 +1,4 @@
@import "./main.css";
@import "./components/form/json-schema/styles.css"; @import "./components/form/json-schema/styles.css";
@import "@xyflow/react/dist/style.css"; @import "@xyflow/react/dist/style.css";
@import "@mantine/core/styles.css"; @import "@mantine/core/styles.css";

View File

@@ -1,8 +1,6 @@
import { readFile } from "node:fs/promises"; import { readFile } from "node:fs/promises";
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import { createClient } from "@libsql/client/node";
import { App, registries } from "./src"; import { App, registries } from "./src";
import { LibsqlConnection } from "./src/data";
import { StorageLocalAdapter } from "./src/media/storage/adapters/StorageLocalAdapter"; import { StorageLocalAdapter } from "./src/media/storage/adapters/StorageLocalAdapter";
registries.media.register("local", StorageLocalAdapter); registries.media.register("local", StorageLocalAdapter);
@@ -12,17 +10,15 @@ const example = import.meta.env.VITE_EXAMPLE;
const credentials = example const credentials = example
? { ? {
url: `file:.configs/${example}.db` url: `file:.configs/${example}.db`
//url: ":memory:"
} }
: { : import.meta.env.VITE_DB_URL
url: import.meta.env.VITE_DB_URL!, ? {
authToken: import.meta.env.VITE_DB_TOKEN! url: import.meta.env.VITE_DB_URL!,
}; authToken: import.meta.env.VITE_DB_TOKEN!
if (!credentials.url) { }
throw new Error("Missing VITE_DB_URL env variable. Add it to .env file"); : {
} url: ":memory:"
};
const connection = new LibsqlConnection(createClient(credentials));
let initialConfig: any = undefined; let initialConfig: any = undefined;
if (example) { if (example) {
@@ -31,11 +27,17 @@ if (example) {
} }
let app: App; let app: App;
const recreate = true; const recreate = import.meta.env.VITE_APP_DISABLE_FRESH !== "1";
export default { export default {
async fetch(request: Request) { async fetch(request: Request) {
if (!app || recreate) { if (!app || recreate) {
app = App.create({ connection, initialConfig }); app = App.create({
connection: {
type: "libsql",
config: credentials
},
initialConfig
});
app.emgr.onEvent( app.emgr.onEvent(
App.Events.AppBuiltEvent, App.Events.AppBuiltEvent,
async () => { async () => {

View File

@@ -5,6 +5,7 @@ import "bknd/dist/styles.css";
import { getApi } from "bknd/adapter/astro"; import { getApi } from "bknd/adapter/astro";
const api = getApi(Astro, { mode: "dynamic" }); const api = getApi(Astro, { mode: "dynamic" });
await api.verifyAuth();
const user = api.getUser(); const user = api.getUser();
export const prerender = false; export const prerender = false;

View File

@@ -3,6 +3,8 @@ import { getApi } from "bknd/adapter/astro";
import Card from "../components/Card.astro"; import Card from "../components/Card.astro";
import Layout from "../layouts/Layout.astro"; import Layout from "../layouts/Layout.astro";
const api = getApi(Astro, { mode: "dynamic" }); const api = getApi(Astro, { mode: "dynamic" });
await api.verifyAuth();
const { data } = await api.data.readMany("todos"); const { data } = await api.data.readMany("todos");
const user = api.getUser(); const user = api.getUser();

View File

@@ -7,9 +7,9 @@ export const meta: MetaFunction = () => {
export const loader = async (args: LoaderFunctionArgs) => { export const loader = async (args: LoaderFunctionArgs) => {
const api = args.context.api; const api = args.context.api;
const user = (await api.getVerifiedAuthState(true)).user; await api.verifyAuth();
const { data } = await api.data.readMany("todos"); const { data } = await api.data.readMany("todos");
return { data, user }; return { data, user: api.getUser() };
}; };
export default function Index() { export default function Index() {