cli: user: add token generation (#140)

* cli: user: add token generation

* cli: user: add token generation

* cli: user: check for value being cancel before continuing
This commit is contained in:
dswbx
2025-04-05 17:57:03 +02:00
committed by GitHub
parent de984fa101
commit ca86fa58ac
2 changed files with 65 additions and 36 deletions

View File

@@ -1,4 +1,4 @@
import { type DB, Exception } from "core";
import { type DB, Exception, type PrimaryFieldType } from "core";
import { addFlashMessage } from "core/server/flash";
import {
type Static,
@@ -14,6 +14,7 @@ import { deleteCookie, getSignedCookie, setSignedCookie } from "hono/cookie";
import { sign, verify } from "hono/jwt";
import type { CookieOptions } from "hono/utils/cookie";
import type { ServerEnv } from "modules/Controller";
import { pick } from "lodash-es";
type Input = any; // workaround
export type JWTPayload = Parameters<typeof sign>[0];
@@ -37,11 +38,10 @@ export interface Strategy {
}
export type User = {
id: number;
id: PrimaryFieldType;
email: string;
username: string;
password: string;
role: string;
role?: string | null;
};
export type ProfileExchange = {
@@ -158,13 +158,8 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
}
// @todo: add jwt tests
async jwt(user: Omit<User, "password">): Promise<string> {
const prohibited = ["password"];
for (const prop of prohibited) {
if (prop in user) {
throw new Error(`Property "${prop}" is prohibited`);
}
}
async jwt(_user: Omit<User, "password">): Promise<string> {
const user = pick(_user, this.config.jwt.fields);
const payload: JWTPayload = {
...user,