mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-15 20:17:22 +00:00
fixed auth strategy toggle, updated astro/remix
This commit is contained in:
@@ -15,7 +15,7 @@ if (clean) {
|
||||
|
||||
let types_running = false;
|
||||
function buildTypes() {
|
||||
if (types_running) return;
|
||||
if (types_running || !types) return;
|
||||
types_running = true;
|
||||
|
||||
Bun.spawn(["bun", "build:types"], {
|
||||
|
||||
@@ -128,15 +128,17 @@ export class Api {
|
||||
};
|
||||
}
|
||||
|
||||
async getVerifiedAuthState(force?: boolean): Promise<AuthState> {
|
||||
if (force === true || !this.verified) {
|
||||
async getVerifiedAuthState(): Promise<AuthState> {
|
||||
await this.verifyAuth();
|
||||
}
|
||||
|
||||
return this.getAuthState();
|
||||
}
|
||||
|
||||
async verifyAuth() {
|
||||
if (!this.token) {
|
||||
this.markAuthVerified(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const res = await this.auth.me();
|
||||
if (!res.ok || !res.body.user) {
|
||||
|
||||
@@ -226,8 +226,16 @@ export class AppAuth extends Module<typeof authConfigSchema> {
|
||||
private toggleStrategyValueVisibility(visible: boolean) {
|
||||
const field = this.getUsersEntity().field("strategy_value")!;
|
||||
|
||||
field.config.hidden = !visible;
|
||||
field.config.fillable = visible;
|
||||
if (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?
|
||||
}
|
||||
|
||||
|
||||
@@ -33,7 +33,6 @@ export class AuthController extends Controller {
|
||||
const name = strategy.getName();
|
||||
const { create, change } = actions;
|
||||
const em = this.auth.em;
|
||||
const mutator = em.mutator(this.auth.config.entity_name as "users");
|
||||
|
||||
if (create) {
|
||||
hono.post(
|
||||
@@ -46,10 +45,9 @@ export class AuthController extends Controller {
|
||||
skipMark: true
|
||||
});
|
||||
const processed = (await create.preprocess?.(valid)) ?? valid;
|
||||
console.log("processed", processed);
|
||||
|
||||
// @todo: check processed for "role" and check permissions
|
||||
|
||||
const mutator = em.mutator(this.auth.config.entity_name as "users");
|
||||
mutator.__unstable_toggleSystemEntityCreation(false);
|
||||
const { data: created } = await mutator.insertOne({
|
||||
...processed,
|
||||
@@ -98,7 +96,7 @@ export class AuthController extends Controller {
|
||||
|
||||
hono.get("/me", auth(), async (c) => {
|
||||
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);
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
@import "./main.css";
|
||||
@import "./components/form/json-schema/styles.css";
|
||||
@import "@xyflow/react/dist/style.css";
|
||||
@import "@mantine/core/styles.css";
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { createClient } from "@libsql/client/node";
|
||||
import { App, registries } from "./src";
|
||||
import { LibsqlConnection } from "./src/data";
|
||||
import { StorageLocalAdapter } from "./src/media/storage/adapters/StorageLocalAdapter";
|
||||
|
||||
registries.media.register("local", StorageLocalAdapter);
|
||||
@@ -12,17 +10,15 @@ const example = import.meta.env.VITE_EXAMPLE;
|
||||
const credentials = example
|
||||
? {
|
||||
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!
|
||||
};
|
||||
if (!credentials.url) {
|
||||
throw new Error("Missing VITE_DB_URL env variable. Add it to .env file");
|
||||
}
|
||||
|
||||
const connection = new LibsqlConnection(createClient(credentials));
|
||||
: {
|
||||
url: ":memory:"
|
||||
};
|
||||
|
||||
let initialConfig: any = undefined;
|
||||
if (example) {
|
||||
@@ -31,11 +27,17 @@ if (example) {
|
||||
}
|
||||
|
||||
let app: App;
|
||||
const recreate = true;
|
||||
const recreate = import.meta.env.VITE_APP_DISABLE_FRESH !== "1";
|
||||
export default {
|
||||
async fetch(request: Request) {
|
||||
if (!app || recreate) {
|
||||
app = App.create({ connection, initialConfig });
|
||||
app = App.create({
|
||||
connection: {
|
||||
type: "libsql",
|
||||
config: credentials
|
||||
},
|
||||
initialConfig
|
||||
});
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async () => {
|
||||
|
||||
@@ -5,6 +5,7 @@ import "bknd/dist/styles.css";
|
||||
import { getApi } from "bknd/adapter/astro";
|
||||
|
||||
const api = getApi(Astro, { mode: "dynamic" });
|
||||
await api.verifyAuth();
|
||||
const user = api.getUser();
|
||||
|
||||
export const prerender = false;
|
||||
|
||||
@@ -3,6 +3,8 @@ import { getApi } from "bknd/adapter/astro";
|
||||
import Card from "../components/Card.astro";
|
||||
import Layout from "../layouts/Layout.astro";
|
||||
const api = getApi(Astro, { mode: "dynamic" });
|
||||
await api.verifyAuth();
|
||||
|
||||
const { data } = await api.data.readMany("todos");
|
||||
const user = api.getUser();
|
||||
|
||||
|
||||
@@ -7,9 +7,9 @@ export const meta: MetaFunction = () => {
|
||||
|
||||
export const loader = async (args: LoaderFunctionArgs) => {
|
||||
const api = args.context.api;
|
||||
const user = (await api.getVerifiedAuthState(true)).user;
|
||||
await api.verifyAuth();
|
||||
const { data } = await api.data.readMany("todos");
|
||||
return { data, user };
|
||||
return { data, user: api.getUser() };
|
||||
};
|
||||
|
||||
export default function Index() {
|
||||
|
||||
Reference in New Issue
Block a user