From c7bd0a636bcef9ffdcda8fa311424f7ec36b8d9b Mon Sep 17 00:00:00 2001 From: dswbx Date: Tue, 14 Jan 2025 11:55:24 +0100 Subject: [PATCH] fix admin redirection on default role matched only --- app/src/modules/server/AdminController.tsx | 7 ++- examples/nextjs/src/pages/api/[...route].ts | 52 +++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/src/modules/server/AdminController.tsx b/app/src/modules/server/AdminController.tsx index 8b8ae9a..7aeb4bb 100644 --- a/app/src/modules/server/AdminController.tsx +++ b/app/src/modules/server/AdminController.tsx @@ -87,8 +87,13 @@ export class AdminController extends Controller { hono.get( authRoutes.login, permission([SystemPermissions.accessAdmin, SystemPermissions.schemaRead], { + // @ts-ignore onGranted: async (c) => { - return c.redirect(authRoutes.success); + // @todo: add strict test to permissions middleware? + if (auth.authenticator.isUserLoggedIn()) { + console.log("redirecting to success"); + return c.redirect(authRoutes.success); + } } }), async (c) => { diff --git a/examples/nextjs/src/pages/api/[...route].ts b/examples/nextjs/src/pages/api/[...route].ts index 0495f1d..ef93433 100644 --- a/examples/nextjs/src/pages/api/[...route].ts +++ b/examples/nextjs/src/pages/api/[...route].ts @@ -1,4 +1,7 @@ +import { App } from "bknd"; import { serve } from "bknd/adapter/nextjs"; +import { boolean, em, entity, text } from "bknd/data"; +import { secureRandomString } from "bknd/utils"; export const config = { runtime: "edge", @@ -9,11 +12,60 @@ export const config = { unstable_allowDynamic: ["**/*.js"] }; +// the em() function makes it easy to create an initial schema +const schema = em({ + todos: entity("todos", { + title: text(), + done: boolean() + }) +}); + +// register your schema to get automatic type completion +type Database = (typeof schema)["DB"]; +declare module "bknd/core" { + interface DB extends Database {} +} + export default serve({ + // we can use any libsql config, and if omitted, uses in-memory connection: { type: "libsql", config: { url: "http://localhost:8080" } + }, + // an initial config is only applied if the database is empty + initialConfig: { + data: schema.toJSON(), + // we're enabling auth ... + auth: { + enabled: true, + jwt: { + secret: secureRandomString(64) + } + } + }, + options: { + // the seed option is only executed if the database was empty + seed: async (ctx) => { + await ctx.em.mutator("todos").insertMany([ + { title: "Learn bknd", done: true }, + { title: "Build something cool", done: false } + ]); + } + }, + // here we can hook into the app lifecycle events ... + beforeBuild: async (app) => { + app.emgr.onEvent( + App.Events.AppFirstBoot, + async () => { + // ... to create an initial user + await app.module.auth.createUser({ + email: "ds@bknd.io", + password: "12345678" + }); + }, + "sync" + ); } });