fix admin redirection on default role matched only

This commit is contained in:
dswbx
2025-01-14 11:55:24 +01:00
parent a9b2c613e1
commit c7bd0a636b
2 changed files with 58 additions and 1 deletions

View File

@@ -87,9 +87,14 @@ export class AdminController extends Controller {
hono.get( hono.get(
authRoutes.login, authRoutes.login,
permission([SystemPermissions.accessAdmin, SystemPermissions.schemaRead], { permission([SystemPermissions.accessAdmin, SystemPermissions.schemaRead], {
// @ts-ignore
onGranted: async (c) => { onGranted: async (c) => {
// @todo: add strict test to permissions middleware?
if (auth.authenticator.isUserLoggedIn()) {
console.log("redirecting to success");
return c.redirect(authRoutes.success); return c.redirect(authRoutes.success);
} }
}
}), }),
async (c) => { async (c) => {
return c.html(c.get("html")!); return c.html(c.get("html")!);

View File

@@ -1,4 +1,7 @@
import { App } from "bknd";
import { serve } from "bknd/adapter/nextjs"; import { serve } from "bknd/adapter/nextjs";
import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils";
export const config = { export const config = {
runtime: "edge", runtime: "edge",
@@ -9,11 +12,60 @@ export const config = {
unstable_allowDynamic: ["**/*.js"] 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({ export default serve({
// we can use any libsql config, and if omitted, uses in-memory
connection: { connection: {
type: "libsql", type: "libsql",
config: { config: {
url: "http://localhost:8080" 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"
);
} }
}); });