Feat: 2fa (#1783)
* preplan otp, better qrcode library * add 2fa as feature * add totp generation * working totp lifecycle * don't allow disabled user to log in not a security issue as permission handler would fail anyway * require 2fa on login if enabled * update packages * fix typo * remove console.logs
This commit is contained in:
@@ -1,21 +1,42 @@
|
||||
import type { NitroFetchRequest, NitroFetchOptions } from 'nitropack/types';
|
||||
import type {
|
||||
NitroFetchRequest,
|
||||
NitroFetchOptions,
|
||||
TypedInternalResponse,
|
||||
ExtractedRouteMethod,
|
||||
} from 'nitropack/types';
|
||||
import { FetchError } from 'ofetch';
|
||||
|
||||
type RevertFn = (success: boolean) => Promise<void>;
|
||||
type RevertFn<
|
||||
R extends NitroFetchRequest,
|
||||
T = unknown,
|
||||
O extends NitroFetchOptions<R> = NitroFetchOptions<R>,
|
||||
> = (
|
||||
success: boolean,
|
||||
data:
|
||||
| TypedInternalResponse<
|
||||
R,
|
||||
T,
|
||||
NitroFetchOptions<R> extends O ? 'get' : ExtractedRouteMethod<R, O>
|
||||
>
|
||||
| undefined
|
||||
) => Promise<void>;
|
||||
|
||||
type SubmitOpts = {
|
||||
revert: RevertFn;
|
||||
type SubmitOpts<
|
||||
R extends NitroFetchRequest,
|
||||
T = unknown,
|
||||
O extends NitroFetchOptions<R> = NitroFetchOptions<R>,
|
||||
> = {
|
||||
revert: RevertFn<R, T, O>;
|
||||
successMsg?: string;
|
||||
errorMsg?: string;
|
||||
noSuccessToast?: boolean;
|
||||
};
|
||||
|
||||
export function useSubmit<
|
||||
R extends NitroFetchRequest,
|
||||
O extends NitroFetchOptions<R> & { body?: never },
|
||||
>(url: R, options: O, opts: SubmitOpts) {
|
||||
T = unknown,
|
||||
>(url: R, options: O, opts: SubmitOpts<R, T, O>) {
|
||||
const toast = useToast();
|
||||
const { t: $t } = useI18n();
|
||||
|
||||
return async (data: unknown) => {
|
||||
try {
|
||||
@@ -24,11 +45,6 @@ export function useSubmit<
|
||||
body: data,
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
if (!(res as any).success) {
|
||||
throw new Error(opts.errorMsg || $t('toast.errored'));
|
||||
}
|
||||
|
||||
if (!opts.noSuccessToast) {
|
||||
toast.showToast({
|
||||
type: 'success',
|
||||
@@ -36,7 +52,8 @@ export function useSubmit<
|
||||
});
|
||||
}
|
||||
|
||||
await opts.revert(true);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
await opts.revert(true, res as any);
|
||||
} catch (e) {
|
||||
if (e instanceof FetchError) {
|
||||
toast.showToast({
|
||||
@@ -51,7 +68,7 @@ export function useSubmit<
|
||||
} else {
|
||||
console.error(e);
|
||||
}
|
||||
await opts.revert(false);
|
||||
await opts.revert(false, undefined);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user