@mesob/auth-react 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/auth/{auth-page-layout.d.ts → auth-layout.d.ts} +3 -3
- package/dist/components/auth/{auth-page-layout.js → auth-layout.js} +4 -4
- package/dist/components/auth/auth-layout.js.map +1 -0
- package/dist/components/auth/countdown.js +15 -6
- package/dist/components/auth/countdown.js.map +1 -1
- package/dist/components/auth/forgot-password.d.ts +1 -9
- package/dist/components/auth/forgot-password.js +267 -43
- package/dist/components/auth/forgot-password.js.map +1 -1
- package/dist/components/auth/reset-password-form.d.ts +2 -10
- package/dist/components/auth/reset-password-form.js +366 -118
- package/dist/components/auth/reset-password-form.js.map +1 -1
- package/dist/components/auth/sign-in.d.ts +2 -10
- package/dist/components/auth/sign-in.js +358 -130
- package/dist/components/auth/sign-in.js.map +1 -1
- package/dist/components/auth/sign-up.d.ts +2 -10
- package/dist/components/auth/sign-up.js +379 -131
- package/dist/components/auth/sign-up.js.map +1 -1
- package/dist/components/auth/verification-form.d.ts +2 -16
- package/dist/components/auth/verification-form.js +15 -6
- package/dist/components/auth/verification-form.js.map +1 -1
- package/dist/components/auth/verify-email.d.ts +10 -0
- package/dist/components/auth/{pages/verify-email-page.js → verify-email.js} +54 -34
- package/dist/components/auth/verify-email.js.map +1 -0
- package/dist/components/auth/verify-phone.d.ts +11 -0
- package/dist/components/auth/{pages/verify-phone-page.js → verify-phone.js} +53 -46
- package/dist/components/auth/verify-phone.js.map +1 -0
- package/dist/components/error-boundary.d.ts +2 -2
- package/dist/components/iam/permissions.d.ts +5 -0
- package/dist/components/iam/{permissions/permissions-page.js → permissions.js} +29 -13
- package/dist/components/iam/permissions.js.map +1 -0
- package/dist/components/iam/roles.d.ts +5 -0
- package/dist/components/iam/{roles/roles-page.js → roles.js} +29 -13
- package/dist/components/iam/roles.js.map +1 -0
- package/dist/components/iam/sessions.d.ts +5 -0
- package/dist/components/iam/{sessions/sessions-page.js → sessions.js} +13 -11
- package/dist/components/iam/sessions.js.map +1 -0
- package/dist/components/iam/tenants.d.ts +5 -0
- package/dist/components/iam/{tenants/tenants-page.js → tenants.js} +13 -11
- package/dist/components/iam/tenants.js.map +1 -0
- package/dist/components/iam/users.d.ts +5 -0
- package/dist/components/iam/{users/users-page.js → users.js} +13 -11
- package/dist/components/iam/users.js.map +1 -0
- package/dist/components/profile/account.d.ts +5 -0
- package/dist/components/profile/account.js +66 -0
- package/dist/components/profile/account.js.map +1 -0
- package/dist/components/profile/change-email-form.d.ts +5 -0
- package/dist/components/profile/change-email-form.js +721 -0
- package/dist/components/profile/change-email-form.js.map +1 -0
- package/dist/components/profile/change-password-form.d.ts +5 -0
- package/dist/components/profile/change-password-form.js +256 -0
- package/dist/components/profile/change-password-form.js.map +1 -0
- package/dist/components/profile/change-phone-form.d.ts +5 -0
- package/dist/components/profile/change-phone-form.js +758 -0
- package/dist/components/profile/change-phone-form.js.map +1 -0
- package/dist/components/profile/change-profile.d.ts +9 -0
- package/dist/components/profile/change-profile.js +37 -0
- package/dist/components/profile/change-profile.js.map +1 -0
- package/dist/components/profile/otp-verification-modal.d.ts +15 -0
- package/dist/components/profile/otp-verification-modal.js +261 -0
- package/dist/components/profile/otp-verification-modal.js.map +1 -0
- package/dist/components/profile/request-change-email-form.d.ts +10 -0
- package/dist/components/profile/request-change-email-form.js +293 -0
- package/dist/components/profile/request-change-email-form.js.map +1 -0
- package/dist/components/profile/request-change-phone-form.d.ts +10 -0
- package/dist/components/profile/request-change-phone-form.js +331 -0
- package/dist/components/profile/request-change-phone-form.js.map +1 -0
- package/dist/components/profile/security.d.ts +5 -0
- package/dist/components/profile/security.js +1439 -0
- package/dist/components/profile/security.js.map +1 -0
- package/dist/components/profile/verify-change-email-form.d.ts +11 -0
- package/dist/components/profile/verify-change-email-form.js +402 -0
- package/dist/components/profile/verify-change-email-form.js.map +1 -0
- package/dist/components/profile/verify-change-phone-form.d.ts +11 -0
- package/dist/components/profile/verify-change-phone-form.js +406 -0
- package/dist/components/profile/verify-change-phone-form.js.map +1 -0
- package/dist/components/shared/{data-table/data-table.js → data-table.js} +2 -2
- package/dist/components/shared/data-table.js.map +1 -0
- package/dist/index.d.ts +42 -88
- package/dist/index.js +2297 -1299
- package/dist/index.js.map +1 -1
- package/dist/types-D3s9oE-5.d.ts +75 -0
- package/dist/verification-form-ipSRTtQB.d.ts +22 -0
- package/package.json +3 -2
- package/dist/components/auth/auth-page-layout.js.map +0 -1
- package/dist/components/auth/pages/forgot-password-page.d.ts +0 -6
- package/dist/components/auth/pages/forgot-password-page.js +0 -362
- package/dist/components/auth/pages/forgot-password-page.js.map +0 -1
- package/dist/components/auth/pages/reset-password-page.d.ts +0 -9
- package/dist/components/auth/pages/reset-password-page.js +0 -514
- package/dist/components/auth/pages/reset-password-page.js.map +0 -1
- package/dist/components/auth/pages/sign-in-page.d.ts +0 -8
- package/dist/components/auth/pages/sign-in-page.js +0 -565
- package/dist/components/auth/pages/sign-in-page.js.map +0 -1
- package/dist/components/auth/pages/sign-up-page.d.ts +0 -9
- package/dist/components/auth/pages/sign-up-page.js +0 -518
- package/dist/components/auth/pages/sign-up-page.js.map +0 -1
- package/dist/components/auth/pages/verify-email-page.d.ts +0 -10
- package/dist/components/auth/pages/verify-email-page.js.map +0 -1
- package/dist/components/auth/pages/verify-phone-page.d.ts +0 -11
- package/dist/components/auth/pages/verify-phone-page.js.map +0 -1
- package/dist/components/iam/permissions/permissions-page.d.ts +0 -5
- package/dist/components/iam/permissions/permissions-page.js.map +0 -1
- package/dist/components/iam/roles/roles-page.d.ts +0 -5
- package/dist/components/iam/roles/roles-page.js.map +0 -1
- package/dist/components/iam/sessions/sessions-page.d.ts +0 -5
- package/dist/components/iam/sessions/sessions-page.js.map +0 -1
- package/dist/components/iam/tenants/tenants-page.d.ts +0 -5
- package/dist/components/iam/tenants/tenants-page.js.map +0 -1
- package/dist/components/iam/users/users-page.d.ts +0 -5
- package/dist/components/iam/users/users-page.js.map +0 -1
- package/dist/components/profile/profile-page.d.ts +0 -8
- package/dist/components/profile/profile-page.js +0 -163
- package/dist/components/profile/profile-page.js.map +0 -1
- package/dist/components/shared/data-table/data-table.js.map +0 -1
- package/dist/handle-error-BqDMxnQZ.d.ts +0 -8
- /package/dist/components/shared/{data-table/data-table.d.ts → data-table.d.ts} +0 -0
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import { ReactNode } from 'react';
|
|
3
3
|
|
|
4
|
-
type
|
|
4
|
+
type AuthLayoutProps = {
|
|
5
5
|
title: string;
|
|
6
6
|
description?: string;
|
|
7
7
|
children: ReactNode;
|
|
8
8
|
footer?: ReactNode;
|
|
9
9
|
logoImage?: string;
|
|
10
10
|
};
|
|
11
|
-
declare const
|
|
11
|
+
declare const AuthLayout: ({ title, description, children, footer, logoImage, }: AuthLayoutProps) => react_jsx_runtime.JSX.Element;
|
|
12
12
|
|
|
13
|
-
export {
|
|
13
|
+
export { AuthLayout };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
-
// src/components/auth/auth-
|
|
3
|
+
// src/components/auth/auth-layout.tsx
|
|
4
4
|
import { jsx, jsxs } from "react/jsx-runtime";
|
|
5
|
-
var
|
|
5
|
+
var AuthLayout = ({
|
|
6
6
|
title,
|
|
7
7
|
description,
|
|
8
8
|
children,
|
|
@@ -20,6 +20,6 @@ var AuthPageLayout = ({
|
|
|
20
20
|
] });
|
|
21
21
|
};
|
|
22
22
|
export {
|
|
23
|
-
|
|
23
|
+
AuthLayout
|
|
24
24
|
};
|
|
25
|
-
//# sourceMappingURL=auth-
|
|
25
|
+
//# sourceMappingURL=auth-layout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/components/auth/auth-layout.tsx"],"sourcesContent":["'use client';\n\nimport type { ReactNode } from 'react';\n\ntype AuthLayoutProps = {\n title: string;\n description?: string;\n children: ReactNode;\n footer?: ReactNode;\n logoImage?: string;\n};\n\nexport const AuthLayout = ({\n title,\n description,\n children,\n footer,\n logoImage,\n}: AuthLayoutProps) => {\n return (\n <div className=\"space-y-4\">\n <div className=\"flex size-8 mb-6 w-full items-center justify-center rounded-md\">\n {/** biome-ignore lint/performance/noImgElement: logo image */}\n <img src={logoImage || ''} alt=\"Mesob\" width={42} height={42} />\n </div>\n <div className=\"text-center\">\n <h1 className=\"text-2xl font-bold tracking-tight\">{title}</h1>\n {description && (\n <p className=\"mt-2 text-sm text-muted-foreground\">{description}</p>\n )}\n </div>\n\n {children}\n\n <div className=\"mt-2 w-full\">\n {footer && (\n <div className=\"w-full text-center text-sm text-muted-foreground\">\n {footer}\n </div>\n )}\n </div>\n </div>\n );\n};\n"],"mappings":";;;AAuBQ,cAEF,YAFE;AAXD,IAAM,aAAa,CAAC;AAAA,EACzB;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAAuB;AACrB,SACE,qBAAC,SAAI,WAAU,aACb;AAAA,wBAAC,SAAI,WAAU,kEAEb,8BAAC,SAAI,KAAK,aAAa,IAAI,KAAI,SAAQ,OAAO,IAAI,QAAQ,IAAI,GAChE;AAAA,IACA,qBAAC,SAAI,WAAU,eACb;AAAA,0BAAC,QAAG,WAAU,qCAAqC,iBAAM;AAAA,MACxD,eACC,oBAAC,OAAE,WAAU,sCAAsC,uBAAY;AAAA,OAEnE;AAAA,IAEC;AAAA,IAED,oBAAC,SAAI,WAAU,eACZ,oBACC,oBAAC,SAAI,WAAU,oDACZ,kBACH,GAEJ;AAAA,KACF;AAEJ;","names":[]}
|
|
@@ -3,7 +3,10 @@
|
|
|
3
3
|
// src/components/auth/countdown.tsx
|
|
4
4
|
import { Button } from "@mesob/ui/components/button";
|
|
5
5
|
import { Spinner } from "@mesob/ui/components/spinner";
|
|
6
|
-
import { useEffect
|
|
6
|
+
import { useEffect, useState as useState2 } from "react";
|
|
7
|
+
|
|
8
|
+
// src/hooks/use-translator.ts
|
|
9
|
+
import { useMesob } from "@mesob/ui/components/mesob-context";
|
|
7
10
|
|
|
8
11
|
// src/lib/translations.ts
|
|
9
12
|
function createTranslator(messages, namespace) {
|
|
@@ -36,7 +39,12 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
36
39
|
import { deepmerge } from "deepmerge-ts";
|
|
37
40
|
import createFetchClient from "openapi-fetch";
|
|
38
41
|
import createClient from "openapi-react-query";
|
|
39
|
-
import { createContext, useContext,
|
|
42
|
+
import { createContext, useContext, useMemo, useState } from "react";
|
|
43
|
+
|
|
44
|
+
// src/utils/cookie.ts
|
|
45
|
+
var isProduction = typeof process !== "undefined" && process.env.NODE_ENV === "production";
|
|
46
|
+
|
|
47
|
+
// src/provider.tsx
|
|
40
48
|
import { jsx } from "react/jsx-runtime";
|
|
41
49
|
var SessionContext = createContext(null);
|
|
42
50
|
var ApiContext = createContext(null);
|
|
@@ -44,9 +52,6 @@ var ConfigContext = createContext(null);
|
|
|
44
52
|
var queryClient = new QueryClient({
|
|
45
53
|
defaultOptions: {
|
|
46
54
|
queries: {
|
|
47
|
-
staleTime: 1e3 * 60 * 5,
|
|
48
|
-
gcTime: 1e3 * 60 * 10,
|
|
49
|
-
retry: 1,
|
|
50
55
|
refetchOnWindowFocus: false
|
|
51
56
|
}
|
|
52
57
|
}
|
|
@@ -61,7 +66,11 @@ function useConfig() {
|
|
|
61
66
|
|
|
62
67
|
// src/hooks/use-translator.ts
|
|
63
68
|
function useTranslator(namespace) {
|
|
69
|
+
const mesob = useMesob();
|
|
64
70
|
const { config } = useConfig();
|
|
71
|
+
if (mesob?.t) {
|
|
72
|
+
return (key, params) => mesob.t(namespace ? `${namespace}.${key}` : key, params);
|
|
73
|
+
}
|
|
65
74
|
return createTranslator(config.messages || {}, namespace);
|
|
66
75
|
}
|
|
67
76
|
|
|
@@ -75,7 +84,7 @@ var Countdown = ({
|
|
|
75
84
|
const t = useTranslator("Common");
|
|
76
85
|
const [seconds, setSeconds] = useState2(initialSeconds);
|
|
77
86
|
const [isResending, setIsResending] = useState2(false);
|
|
78
|
-
|
|
87
|
+
useEffect(() => {
|
|
79
88
|
if (seconds <= 0) {
|
|
80
89
|
return;
|
|
81
90
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/components/auth/countdown.tsx","../../../src/lib/translations.ts","../../../src/provider.tsx","../../../src/hooks/use-translator.ts"],"sourcesContent":["'use client';\n\nimport { Button } from '@mesob/ui/components/button';\nimport { Spinner } from '@mesob/ui/components/spinner';\nimport { useEffect, useState } from 'react';\nimport { useTranslator } from '../../hooks/use-translator';\n\ntype CountdownProps = {\n initialSeconds?: number;\n onResend: () => Promise<void> | void;\n resending?: boolean;\n};\n\nexport const Countdown = ({\n initialSeconds = 60,\n onResend,\n resending = false,\n}: CountdownProps) => {\n const t = useTranslator('Common');\n const [seconds, setSeconds] = useState(initialSeconds);\n const [isResending, setIsResending] = useState(false);\n\n useEffect(() => {\n if (seconds <= 0) {\n return;\n }\n\n const timer = setInterval(() => {\n setSeconds((prev) => {\n if (prev <= 1) {\n clearInterval(timer);\n return 0;\n }\n return prev - 1;\n });\n }, 1000);\n\n return () => clearInterval(timer);\n }, [seconds]);\n\n const handleResend = async () => {\n setIsResending(true);\n try {\n await onResend();\n setSeconds(initialSeconds);\n } catch (_error) {\n // Error handling is done by parent\n } finally {\n setIsResending(false);\n }\n };\n\n if (seconds > 0) {\n return (\n <Button variant=\"ghost\" disabled>\n {t('resendIn', { seconds })}\n </Button>\n );\n }\n\n return (\n <Button\n variant=\"ghost\"\n onClick={handleResend}\n disabled={isResending || resending}\n >\n {isResending || (resending && <Spinner />)}\n {t('resend')}\n </Button>\n );\n};\n","type Messages = Record<string, unknown>;\n\nexport function createTranslator(messages: Messages, namespace?: string) {\n return (key: string, params?: Record<string, string | number>): string => {\n const fullKey = namespace ? `${namespace}.${key}` : key;\n const keys = fullKey.split('.');\n\n let value: unknown = messages;\n for (const k of keys) {\n if (value && typeof value === 'object' && value !== null) {\n value = (value as Record<string, unknown>)[k];\n } else {\n return fullKey;\n }\n }\n\n if (typeof value !== 'string') {\n return fullKey;\n }\n\n // Simple parameter replacement\n if (params) {\n return value.replace(/\\{(\\w+)\\}/g, (_, param) =>\n String(params[param] ?? `{${param}}`),\n );\n }\n\n return value;\n };\n}\n","'use client';\n\nimport { QueryClient, QueryClientProvider } from '@tanstack/react-query';\nimport { deepmerge } from 'deepmerge-ts';\nimport createFetchClient from 'openapi-fetch';\nimport createClient from 'openapi-react-query';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useEffect, useState } from 'react';\nimport type { paths } from './data/openapi';\nimport { createTranslator } from './lib/translations';\nimport {\n type AuthClientConfig,\n type AuthResponse,\n defaultAuthClientConfig,\n type Session,\n type User,\n} from './types';\nimport { createCustomFetch } from './utils/custom-fetch';\n\ntype OpenApiHooks = any;\n\ntype SessionState = {\n user: User | null;\n session: Session | null;\n isLoading: boolean;\n isAuthenticated: boolean;\n error: Error | null;\n};\n\ntype SessionContextValue = SessionState & {\n refresh: () => Promise<void>;\n signOut: () => Promise<void>;\n};\n\ntype ApiContextValue = {\n hooks: OpenApiHooks;\n setAuth: (auth: AuthResponse) => void;\n clearAuth: () => void;\n refresh: () => Promise<void>;\n};\n\ntype ConfigContextValue = {\n config: AuthClientConfig;\n t: (key: string, params?: Record<string, string | number>) => string;\n};\n\nconst SessionContext = createContext<SessionContextValue | null>(null);\nconst ApiContext = createContext<ApiContextValue | null>(null);\nconst ConfigContext = createContext<ConfigContextValue | null>(null);\n\nconst queryClient = new QueryClient({\n defaultOptions: {\n queries: {\n staleTime: 1000 * 60 * 5,\n gcTime: 1000 * 60 * 10,\n retry: 1,\n refetchOnWindowFocus: false,\n },\n },\n});\n\nexport function useSession() {\n const context = useContext(SessionContext);\n if (!context) {\n throw new Error('useSession must be used within MesobAuthProvider');\n }\n return context;\n}\n\nexport function useApi() {\n const context = useContext(ApiContext);\n if (!context) {\n throw new Error('useApi must be used within MesobAuthProvider');\n }\n return context;\n}\n\nexport function useConfig() {\n const context = useContext(ConfigContext);\n if (!context) {\n throw new Error('useConfig must be used within MesobAuthProvider');\n }\n return context;\n}\n\ntype MesobAuthProviderProps = {\n config: AuthClientConfig;\n children: ReactNode;\n};\n\nexport function MesobAuthProvider({\n config,\n children,\n}: MesobAuthProviderProps) {\n const mergedConfig = deepmerge(\n { ...defaultAuthClientConfig } as Partial<AuthClientConfig>,\n config,\n ) as AuthClientConfig;\n\n const api = createFetchClient<paths>({\n baseUrl: mergedConfig.baseURL,\n fetch: createCustomFetch(mergedConfig),\n });\n\n const hooks = createClient(api);\n\n return (\n <QueryClientProvider client={queryClient}>\n <AuthStateProvider config={mergedConfig} hooks={hooks}>\n {children}\n </AuthStateProvider>\n </QueryClientProvider>\n );\n}\n\ntype AuthStateProviderProps = {\n config: AuthClientConfig;\n hooks: OpenApiHooks;\n children: ReactNode;\n};\n\nfunction AuthStateProvider({\n config,\n hooks,\n children,\n}: AuthStateProviderProps) {\n const [authState, setAuthState] = useState<{\n user: User | null;\n session: Session | null;\n isLoading: boolean;\n error: Error | null;\n }>({\n user: null,\n session: null,\n isLoading: false,\n error: null,\n });\n\n const {\n data: sessionData,\n isLoading: sessionLoading,\n error: sessionError,\n refetch,\n } = hooks.useQuery(\n 'get',\n '/session',\n {},\n {\n enabled: true,\n refetchOnMount: true,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n retry: false,\n },\n );\n\n useEffect(() => {\n if (sessionLoading) {\n setAuthState((prev) => ({ ...prev, isLoading: true }));\n return;\n }\n\n if (sessionError) {\n setAuthState({\n user: null,\n session: null,\n isLoading: false,\n error: sessionError as Error,\n });\n return;\n }\n\n if (sessionData) {\n setAuthState({\n user: sessionData.user,\n session: sessionData.session,\n isLoading: false,\n error: null,\n });\n return;\n }\n\n setAuthState({\n user: null,\n session: null,\n isLoading: false,\n error: null,\n });\n }, [sessionData, sessionLoading, sessionError]);\n\n const refresh = async () => {\n await refetch();\n };\n\n const setAuth = (auth: AuthResponse) => {\n setAuthState({\n user: auth.user,\n session: auth.session,\n isLoading: false,\n error: null,\n });\n };\n\n const clearAuth = () => {\n setAuthState({\n user: null,\n session: null,\n isLoading: false,\n error: null,\n });\n };\n\n const signOutMutation = hooks.useMutation('post', '/sign-out');\n\n const signOut = async () => {\n try {\n await signOutMutation.mutateAsync({});\n } finally {\n clearAuth();\n }\n };\n\n const t = createTranslator(config.messages || {});\n\n return (\n <ConfigContext.Provider value={{ config, t }}>\n <ApiContext.Provider value={{ hooks, setAuth, clearAuth, refresh }}>\n <SessionContext.Provider\n value={{\n user: authState.user,\n session: authState.session,\n isLoading: authState.isLoading,\n isAuthenticated: !!authState.user && !!authState.session,\n error: authState.error,\n refresh,\n signOut,\n }}\n >\n {children}\n </SessionContext.Provider>\n </ApiContext.Provider>\n </ConfigContext.Provider>\n );\n}\n","import { createTranslator } from '../lib/translations';\nimport { useConfig } from '../provider';\n\nexport function useTranslator(namespace?: string) {\n const { config } = useConfig();\n return createTranslator(config.messages || {}, namespace);\n}\n"],"mappings":"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","names":["useEffect","useState","jsx","useState","useEffect"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/components/auth/countdown.tsx","../../../src/hooks/use-translator.ts","../../../src/lib/translations.ts","../../../src/provider.tsx","../../../src/utils/cookie.ts"],"sourcesContent":["'use client';\n\nimport { Button } from '@mesob/ui/components/button';\nimport { Spinner } from '@mesob/ui/components/spinner';\nimport { useEffect, useState } from 'react';\nimport { useTranslator } from '../../hooks/use-translator';\n\ntype CountdownProps = {\n initialSeconds?: number;\n onResend: () => Promise<void> | void;\n resending?: boolean;\n};\n\nexport const Countdown = ({\n initialSeconds = 60,\n onResend,\n resending = false,\n}: CountdownProps) => {\n const t = useTranslator('Common');\n const [seconds, setSeconds] = useState(initialSeconds);\n const [isResending, setIsResending] = useState(false);\n\n useEffect(() => {\n if (seconds <= 0) {\n return;\n }\n\n const timer = setInterval(() => {\n setSeconds((prev) => {\n if (prev <= 1) {\n clearInterval(timer);\n return 0;\n }\n return prev - 1;\n });\n }, 1000);\n\n return () => clearInterval(timer);\n }, [seconds]);\n\n const handleResend = async () => {\n setIsResending(true);\n try {\n await onResend();\n setSeconds(initialSeconds);\n } catch (_error) {\n // Error handling is done by parent\n } finally {\n setIsResending(false);\n }\n };\n\n if (seconds > 0) {\n return (\n <Button variant=\"ghost\" disabled>\n {t('resendIn', { seconds })}\n </Button>\n );\n }\n\n return (\n <Button\n variant=\"ghost\"\n onClick={handleResend}\n disabled={isResending || resending}\n >\n {isResending || (resending && <Spinner />)}\n {t('resend')}\n </Button>\n );\n};\n","import { useMesob } from '@mesob/ui/components/mesob-context';\nimport { createTranslator } from '../lib/translations';\nimport { useConfig } from '../provider';\n\nexport function useTranslator(namespace?: string) {\n const mesob = useMesob();\n const { config } = useConfig();\n\n if (mesob?.t) {\n return (key: string, params?: Record<string, string | number>) =>\n mesob.t!(namespace ? `${namespace}.${key}` : key, params);\n }\n\n return createTranslator(config.messages || {}, namespace);\n}\n","type Messages = Record<string, unknown>;\n\nexport function createTranslator(messages: Messages, namespace?: string) {\n return (key: string, params?: Record<string, string | number>): string => {\n const fullKey = namespace ? `${namespace}.${key}` : key;\n const keys = fullKey.split('.');\n\n let value: unknown = messages;\n for (const k of keys) {\n if (value && typeof value === 'object' && value !== null) {\n value = (value as Record<string, unknown>)[k];\n } else {\n return fullKey;\n }\n }\n\n if (typeof value !== 'string') {\n return fullKey;\n }\n\n // Simple parameter replacement\n if (params) {\n return value.replace(/\\{(\\w+)\\}/g, (_, param) =>\n String(params[param] ?? `{${param}}`),\n );\n }\n\n return value;\n };\n}\n","'use client';\n\nimport { QueryClient, QueryClientProvider } from '@tanstack/react-query';\nimport { deepmerge } from 'deepmerge-ts';\nimport createFetchClient from 'openapi-fetch';\nimport createClient from 'openapi-react-query';\nimport type { ReactNode } from 'react';\nimport { createContext, useContext, useMemo, useState } from 'react';\nimport type { paths } from './data/openapi';\nimport { createTranslator } from './lib/translations';\nimport {\n type AuthClientConfig,\n type AuthResponse,\n defaultAuthClientConfig,\n type Session,\n type User,\n} from './types';\nimport { getSessionCookieName } from './utils/cookie';\nimport { createCustomFetch } from './utils/custom-fetch';\n\n// biome-ignore lint/suspicious/noExplicitAny: OpenAPI hooks type\ntype OpenApiHooks = any;\n\n// --- Utility: Check if running on server ---\nfunction isServer(): boolean {\n return typeof document === 'undefined';\n}\n\n/**\n * @deprecated Cookie is httpOnly and cannot be read client-side.\n * Use `useSession().isAuthenticated` instead.\n * This function always returns false on client.\n */\nexport function hasAuthCookie(_cookieName: string): boolean {\n // Cookie is httpOnly, can't check client-side\n // Always return false - use useSession() for auth status\n return false;\n}\n\n// --- Types ---\nexport type AuthStatus = 'loading' | 'authenticated' | 'unauthenticated';\n\ntype AuthState = {\n user: User | null;\n session: Session | null;\n status: AuthStatus;\n error: Error | null;\n};\n\ntype SessionContextValue = AuthState & {\n isLoading: boolean;\n isAuthenticated: boolean;\n refresh: () => Promise<void>;\n signOut: () => Promise<void>;\n};\n\ntype ApiContextValue = {\n hooks: OpenApiHooks;\n setAuth: (auth: AuthResponse) => void;\n clearAuth: () => void;\n refresh: () => Promise<void>;\n};\n\ntype ConfigContextValue = {\n config: AuthClientConfig;\n cookieName: string;\n t: (key: string, params?: Record<string, string | number>) => string;\n};\n\nconst SessionContext = createContext<SessionContextValue | null>(null);\nconst ApiContext = createContext<ApiContextValue | null>(null);\nconst ConfigContext = createContext<ConfigContextValue | null>(null);\n\nconst queryClient = new QueryClient({\n defaultOptions: {\n queries: {\n refetchOnWindowFocus: false,\n },\n },\n});\n\n// --- Hooks ---\n\n/**\n * Get session state including user, session, and auth status.\n * - `status`: 'loading' | 'authenticated' | 'unauthenticated'\n * - `isLoading`: true while fetching session\n * - `isAuthenticated`: true if user and session exist\n */\nexport function useSession(): SessionContextValue {\n const context = useContext(SessionContext);\n if (!context) {\n throw new Error('useSession must be used within MesobAuthProvider');\n }\n return context;\n}\n\nexport function useApi(): ApiContextValue {\n const context = useContext(ApiContext);\n if (!context) {\n throw new Error('useApi must be used within MesobAuthProvider');\n }\n return context;\n}\n\nexport function useConfig(): ConfigContextValue {\n const context = useContext(ConfigContext);\n if (!context) {\n throw new Error('useConfig must be used within MesobAuthProvider');\n }\n return context;\n}\n\n/**\n * @deprecated Cookie is httpOnly, can't be checked client-side.\n * Use `useSession().isAuthenticated` instead.\n */\nexport function useHasAuthCookie(): boolean {\n const { status } = useSession();\n return status === 'authenticated' || status === 'loading';\n}\n\n// --- Provider ---\n\ntype MesobAuthProviderProps = {\n config: AuthClientConfig;\n children: ReactNode;\n};\n\nexport function MesobAuthProvider({\n config,\n children,\n}: MesobAuthProviderProps) {\n const mergedConfig = useMemo(\n () =>\n deepmerge(\n { ...defaultAuthClientConfig } as Partial<AuthClientConfig>,\n config,\n ) as AuthClientConfig,\n [config],\n );\n\n const api = useMemo(\n () =>\n createFetchClient<paths>({\n baseUrl: mergedConfig.baseURL,\n fetch: createCustomFetch(mergedConfig),\n }),\n [mergedConfig],\n );\n\n const hooks = useMemo(() => createClient(api), [api]);\n const cookieName = useMemo(\n () => getSessionCookieName(mergedConfig),\n [mergedConfig],\n );\n\n return (\n <QueryClientProvider client={queryClient}>\n <AuthStateProvider\n config={mergedConfig}\n hooks={hooks}\n cookieName={cookieName}\n >\n {children}\n </AuthStateProvider>\n </QueryClientProvider>\n );\n}\n\ntype AuthStateProviderProps = {\n config: AuthClientConfig;\n hooks: OpenApiHooks;\n cookieName: string;\n children: ReactNode;\n};\n\nfunction AuthStateProvider({\n config,\n hooks,\n cookieName,\n children,\n}: AuthStateProviderProps) {\n // Manual override for sign-out / sign-in\n const [override, setOverride] = useState<AuthState | null>(null);\n\n // Always fetch session - cookie is httpOnly, can't check client-side\n // Server will read the cookie and return user/session if valid\n const {\n data: sessionData,\n isLoading,\n isFetched,\n error: sessionError,\n refetch,\n } = hooks.useQuery(\n 'get',\n '/session',\n {},\n {\n enabled: !(override || isServer()),\n refetchOnMount: false,\n refetchOnWindowFocus: false,\n refetchOnReconnect: false,\n retry: false,\n gcTime: 0,\n staleTime: 0,\n },\n );\n\n // Derive state directly - no useEffect\n const user = override?.user ?? sessionData?.user ?? null;\n const session = override?.session ?? sessionData?.session ?? null;\n const error = override?.error ?? (sessionError as Error | null);\n\n // Check error status code\n const errorStatus = (() => {\n if (!sessionError) {\n return null;\n }\n const err = sessionError as { status?: number };\n return err.status ?? null;\n })();\n\n // Check if error is a network/connection error\n const isNetworkError = (() => {\n if (!sessionError) {\n return false;\n }\n const error = sessionError as Error & { cause?: unknown; data?: unknown };\n const errorMessage =\n error.message || String(error) || JSON.stringify(error);\n // Network errors: TypeError, DOMException, or fetch failures\n if (\n error instanceof TypeError ||\n error instanceof DOMException ||\n error.name === 'TypeError' ||\n errorMessage.includes('Failed to fetch') ||\n errorMessage.includes('ERR_CONNECTION_REFUSED') ||\n errorMessage.includes('NetworkError') ||\n errorMessage.includes('Network request failed') ||\n errorMessage.includes('fetch failed')\n ) {\n return true;\n }\n // Check error cause\n if (error.cause) {\n const causeStr = String(error.cause);\n if (\n causeStr.includes('Failed to fetch') ||\n causeStr.includes('ERR_CONNECTION_REFUSED') ||\n causeStr.includes('NetworkError')\n ) {\n return true;\n }\n }\n return false;\n })();\n\n // Compute status\n // biome-ignore lint: Status determination requires multiple checks\n const status: AuthStatus = (() => {\n if (override) {\n return override.status;\n }\n if (isServer()) {\n return 'loading';\n }\n if (user && session) {\n return 'authenticated';\n }\n // Check for network errors or auth errors first - allow auth page to show\n if (isNetworkError || errorStatus === 401) {\n return 'unauthenticated';\n }\n // If we have an error but it's not a network error, still check loading state\n if (sessionError && !isNetworkError && errorStatus !== 401) {\n if (errorStatus && errorStatus >= 500) {\n return 'authenticated';\n }\n // Other errors mean unauthenticated\n if (isFetched) {\n return 'unauthenticated';\n }\n }\n if (isLoading || !isFetched) {\n return 'loading';\n }\n if (isFetched && !user && !session) {\n return 'unauthenticated';\n }\n return 'unauthenticated';\n })();\n\n const signOutMutation = hooks.useMutation('post', '/sign-out');\n const t = createTranslator(config.messages || {});\n\n const setAuth = (auth: AuthResponse) => {\n setOverride({\n user: auth.user,\n session: auth.session,\n status: 'authenticated',\n error: null,\n });\n };\n\n const clearAuth = () => {\n setOverride({\n user: null,\n session: null,\n status: 'unauthenticated',\n error: null,\n });\n };\n\n const refresh = async () => {\n setOverride(null);\n await refetch();\n };\n\n const signOut = async () => {\n try {\n await signOutMutation.mutateAsync({});\n } finally {\n clearAuth();\n }\n };\n\n return (\n <ConfigContext.Provider value={{ config, cookieName, t }}>\n <ApiContext.Provider value={{ hooks, setAuth, clearAuth, refresh }}>\n <SessionContext.Provider\n value={{\n user,\n session,\n status,\n error,\n isLoading: status === 'loading',\n isAuthenticated: status === 'authenticated',\n refresh,\n signOut,\n }}\n >\n {children}\n </SessionContext.Provider>\n </ApiContext.Provider>\n </ConfigContext.Provider>\n );\n}\n","import type { AuthClientConfig } from '../types';\n\nconst isProduction =\n typeof process !== 'undefined' && process.env.NODE_ENV === 'production';\n\nexport const getSessionCookieName = (config: AuthClientConfig): string => {\n const prefix = config.cookiePrefix || '';\n const baseName = 'session_token';\n if (prefix) {\n return `${prefix}_${baseName}`;\n }\n return isProduction ? '__Host-session_token' : baseName;\n};\n"],"mappings":"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","names":["useState","jsx","useState"]}
|
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
account: string;
|
|
5
|
-
};
|
|
6
|
-
type ForgotPasswordProps = {
|
|
7
|
-
onSubmit: (values: ForgotPasswordFormValues) => Promise<void> | void;
|
|
8
|
-
isLoading?: boolean;
|
|
9
|
-
onBack?: () => void;
|
|
10
|
-
};
|
|
11
|
-
declare const ForgotPassword: ({ onSubmit, isLoading, }: ForgotPasswordProps) => react_jsx_runtime.JSX.Element;
|
|
3
|
+
declare const ForgotPassword: () => react_jsx_runtime.JSX.Element;
|
|
12
4
|
|
|
13
5
|
export { ForgotPassword };
|
|
@@ -2,6 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
// src/components/auth/forgot-password.tsx
|
|
4
4
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
5
|
+
import {
|
|
6
|
+
Alert,
|
|
7
|
+
AlertDescription,
|
|
8
|
+
AlertTitle
|
|
9
|
+
} from "@mesob/ui/components/alert";
|
|
5
10
|
import { Button } from "@mesob/ui/components/button";
|
|
6
11
|
import {
|
|
7
12
|
Field,
|
|
@@ -10,10 +15,17 @@ import {
|
|
|
10
15
|
FieldLabel
|
|
11
16
|
} from "@mesob/ui/components/field";
|
|
12
17
|
import { Input } from "@mesob/ui/components/input";
|
|
18
|
+
import { useMesob as useMesob2 } from "@mesob/ui/components/mesob-context";
|
|
13
19
|
import { Spinner } from "@mesob/ui/components/spinner";
|
|
20
|
+
import { IconAlertCircle } from "@tabler/icons-react";
|
|
21
|
+
import { useEffect, useState as useState2 } from "react";
|
|
14
22
|
import { Controller, useForm } from "react-hook-form";
|
|
23
|
+
import { toast } from "sonner";
|
|
15
24
|
import { z } from "zod";
|
|
16
25
|
|
|
26
|
+
// src/hooks/use-translator.ts
|
|
27
|
+
import { useMesob } from "@mesob/ui/components/mesob-context";
|
|
28
|
+
|
|
17
29
|
// src/lib/translations.ts
|
|
18
30
|
function createTranslator(messages, namespace) {
|
|
19
31
|
return (key, params) => {
|
|
@@ -45,7 +57,12 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
|
|
45
57
|
import { deepmerge } from "deepmerge-ts";
|
|
46
58
|
import createFetchClient from "openapi-fetch";
|
|
47
59
|
import createClient from "openapi-react-query";
|
|
48
|
-
import { createContext, useContext,
|
|
60
|
+
import { createContext, useContext, useMemo, useState } from "react";
|
|
61
|
+
|
|
62
|
+
// src/utils/cookie.ts
|
|
63
|
+
var isProduction = typeof process !== "undefined" && process.env.NODE_ENV === "production";
|
|
64
|
+
|
|
65
|
+
// src/provider.tsx
|
|
49
66
|
import { jsx } from "react/jsx-runtime";
|
|
50
67
|
var SessionContext = createContext(null);
|
|
51
68
|
var ApiContext = createContext(null);
|
|
@@ -53,13 +70,17 @@ var ConfigContext = createContext(null);
|
|
|
53
70
|
var queryClient = new QueryClient({
|
|
54
71
|
defaultOptions: {
|
|
55
72
|
queries: {
|
|
56
|
-
staleTime: 1e3 * 60 * 5,
|
|
57
|
-
gcTime: 1e3 * 60 * 10,
|
|
58
|
-
retry: 1,
|
|
59
73
|
refetchOnWindowFocus: false
|
|
60
74
|
}
|
|
61
75
|
}
|
|
62
76
|
});
|
|
77
|
+
function useApi() {
|
|
78
|
+
const context = useContext(ApiContext);
|
|
79
|
+
if (!context) {
|
|
80
|
+
throw new Error("useApi must be used within MesobAuthProvider");
|
|
81
|
+
}
|
|
82
|
+
return context;
|
|
83
|
+
}
|
|
63
84
|
function useConfig() {
|
|
64
85
|
const context = useContext(ConfigContext);
|
|
65
86
|
if (!context) {
|
|
@@ -70,65 +91,268 @@ function useConfig() {
|
|
|
70
91
|
|
|
71
92
|
// src/hooks/use-translator.ts
|
|
72
93
|
function useTranslator(namespace) {
|
|
94
|
+
const mesob = useMesob();
|
|
73
95
|
const { config } = useConfig();
|
|
96
|
+
if (mesob?.t) {
|
|
97
|
+
return (key, params) => mesob.t(namespace ? `${namespace}.${key}` : key, params);
|
|
98
|
+
}
|
|
74
99
|
return createTranslator(config.messages || {}, namespace);
|
|
75
100
|
}
|
|
76
101
|
|
|
77
|
-
// src/
|
|
102
|
+
// src/constants/auth.error.codes.ts
|
|
103
|
+
var AUTH_ERROR_MAPPING = {
|
|
104
|
+
USER_NOT_FOUND: {
|
|
105
|
+
title: "Account Not Found",
|
|
106
|
+
description: "We could not find an account with that identifier. Please check your spelling or sign up."
|
|
107
|
+
},
|
|
108
|
+
INVALID_PASSWORD: {
|
|
109
|
+
title: "Invalid Password",
|
|
110
|
+
description: "The password you entered is incorrect. Please try again."
|
|
111
|
+
},
|
|
112
|
+
USER_EXISTS: {
|
|
113
|
+
title: "Account Already Exists",
|
|
114
|
+
description: "An account with this identifier already exists. Please sign in instead."
|
|
115
|
+
},
|
|
116
|
+
VERIFICATION_EXPIRED: {
|
|
117
|
+
title: "Verification Expired",
|
|
118
|
+
description: "The verification code or link has expired. Please request a new one."
|
|
119
|
+
},
|
|
120
|
+
VERIFICATION_MISMATCH: {
|
|
121
|
+
title: "Invalid Code",
|
|
122
|
+
description: "The verification code you entered is invalid. Please double-check and try again."
|
|
123
|
+
},
|
|
124
|
+
VERIFICATION_NOT_FOUND: {
|
|
125
|
+
title: "Verification Not Found",
|
|
126
|
+
description: "We could not find a pending verification request. Please restart the process."
|
|
127
|
+
},
|
|
128
|
+
TOO_MANY_ATTEMPTS: {
|
|
129
|
+
title: "Too Many Attempts",
|
|
130
|
+
description: "You have made too many requests recently. Please wait a moment before trying again."
|
|
131
|
+
},
|
|
132
|
+
REQUIRES_VERIFICATION: {
|
|
133
|
+
title: "Verification Required",
|
|
134
|
+
description: "You need to verify your account before you can continue. Please check your email or phone."
|
|
135
|
+
},
|
|
136
|
+
UNAUTHORIZED: {
|
|
137
|
+
title: "Unauthorized",
|
|
138
|
+
description: "You are not authorized to perform this action. Please sign in again."
|
|
139
|
+
},
|
|
140
|
+
ACCESS_DENIED: {
|
|
141
|
+
title: "Access Denied",
|
|
142
|
+
description: "You do not have permission to access this resource. Please contact support if you believe this is an error."
|
|
143
|
+
},
|
|
144
|
+
HAS_NO_PASSWORD: {
|
|
145
|
+
title: "No Password Set",
|
|
146
|
+
description: "Your account does not have a password set (e.g. social login). Please sign in with your provider or reset your password."
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
var validCodes = Object.keys(AUTH_ERROR_MAPPING);
|
|
150
|
+
|
|
151
|
+
// src/utils/handle-error.ts
|
|
152
|
+
function isAuthError(err) {
|
|
153
|
+
return typeof err === "object" && err !== null && "message" in err && typeof err.message === "string";
|
|
154
|
+
}
|
|
155
|
+
function extractErrorCode(err) {
|
|
156
|
+
if (err.code && validCodes.includes(err.code)) {
|
|
157
|
+
return err.code;
|
|
158
|
+
}
|
|
159
|
+
if (err.message) {
|
|
160
|
+
const messageUpper = err.message.toUpperCase().trim();
|
|
161
|
+
if (validCodes.includes(messageUpper)) {
|
|
162
|
+
return messageUpper;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return "";
|
|
166
|
+
}
|
|
167
|
+
function sanitizeErrorMessage(message) {
|
|
168
|
+
const lowerMessage = message.toLowerCase();
|
|
169
|
+
const isDatabaseError = lowerMessage.includes("failed query") || lowerMessage.includes("select") || lowerMessage.includes("insert") || lowerMessage.includes("update") || lowerMessage.includes("delete") || lowerMessage.includes("from") || lowerMessage.includes("where") || lowerMessage.includes("limit") || lowerMessage.includes("params:") || lowerMessage.includes("query") || message.includes('"iam".') || message.includes('"tenants"') || message.includes('"users"') || message.includes('"sessions"') || message.includes('"accounts"') || lowerMessage.includes("relation") || lowerMessage.includes("column") || lowerMessage.includes("syntax error") || lowerMessage.includes("database") || lowerMessage.includes("postgres") || lowerMessage.includes("sql");
|
|
170
|
+
if (isDatabaseError) {
|
|
171
|
+
return "An error occurred while processing your request";
|
|
172
|
+
}
|
|
173
|
+
return message;
|
|
174
|
+
}
|
|
175
|
+
function handleAuthError(err, setError, t) {
|
|
176
|
+
const errorCode = extractErrorCode(err);
|
|
177
|
+
if (errorCode && AUTH_ERROR_MAPPING[errorCode]) {
|
|
178
|
+
const mapping = AUTH_ERROR_MAPPING[errorCode];
|
|
179
|
+
setError({
|
|
180
|
+
title: mapping.title,
|
|
181
|
+
description: mapping.description
|
|
182
|
+
});
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const sanitizedMessage = sanitizeErrorMessage(
|
|
186
|
+
err.message || t("errors.fallback")
|
|
187
|
+
);
|
|
188
|
+
setError({
|
|
189
|
+
title: t("errors.fallback"),
|
|
190
|
+
description: sanitizedMessage
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
function handleGenericError(err, setError, t) {
|
|
194
|
+
const rawMessage = err instanceof Error ? err.message : t("errors.fallback");
|
|
195
|
+
const sanitizedMessage = sanitizeErrorMessage(rawMessage);
|
|
196
|
+
setError({
|
|
197
|
+
title: "Error",
|
|
198
|
+
description: sanitizedMessage
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
var handleError = (err, setError, t) => {
|
|
202
|
+
if (isAuthError(err)) {
|
|
203
|
+
handleAuthError(err, setError, t);
|
|
204
|
+
} else {
|
|
205
|
+
handleGenericError(err, setError, t);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
// src/components/auth/auth-layout.tsx
|
|
78
210
|
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
211
|
+
var AuthLayout = ({
|
|
212
|
+
title,
|
|
213
|
+
description,
|
|
214
|
+
children,
|
|
215
|
+
footer,
|
|
216
|
+
logoImage
|
|
217
|
+
}) => {
|
|
218
|
+
return /* @__PURE__ */ jsxs("div", { className: "space-y-4", children: [
|
|
219
|
+
/* @__PURE__ */ jsx2("div", { className: "flex size-8 mb-6 w-full items-center justify-center rounded-md", children: /* @__PURE__ */ jsx2("img", { src: logoImage || "", alt: "Mesob", width: 42, height: 42 }) }),
|
|
220
|
+
/* @__PURE__ */ jsxs("div", { className: "text-center", children: [
|
|
221
|
+
/* @__PURE__ */ jsx2("h1", { className: "text-2xl font-bold tracking-tight", children: title }),
|
|
222
|
+
description && /* @__PURE__ */ jsx2("p", { className: "mt-2 text-sm text-muted-foreground", children: description })
|
|
223
|
+
] }),
|
|
224
|
+
children,
|
|
225
|
+
/* @__PURE__ */ jsx2("div", { className: "mt-2 w-full", children: footer && /* @__PURE__ */ jsx2("div", { className: "w-full text-center text-sm text-muted-foreground", children: footer }) })
|
|
226
|
+
] });
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
// src/components/auth/forgot-password.tsx
|
|
230
|
+
import { jsx as jsx3, jsxs as jsxs2 } from "react/jsx-runtime";
|
|
79
231
|
var forgotPasswordSchema = (t) => z.object({
|
|
80
232
|
account: z.string().min(1, t("errors.accountRequired"))
|
|
81
233
|
});
|
|
82
|
-
var ForgotPassword = ({
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
234
|
+
var ForgotPassword = () => {
|
|
235
|
+
const { hooks } = useApi();
|
|
236
|
+
const { config } = useConfig();
|
|
237
|
+
const mesob = useMesob2();
|
|
86
238
|
const t = useTranslator("Auth.forgotPassword");
|
|
239
|
+
const Link = mesob?.linkComponent ?? config.navigation?.linkComponent;
|
|
240
|
+
const [isLoading, setIsLoading] = useState2(false);
|
|
241
|
+
const [error, setError] = useState2(null);
|
|
242
|
+
const forgotPasswordMutation = hooks.useMutation("post", "/password/forgot");
|
|
243
|
+
const signInLink = config.navigation?.links?.signIn || "/auth/sign-in";
|
|
244
|
+
const onNavigate = config.navigation?.onNavigate || ((path) => {
|
|
245
|
+
if (typeof window !== "undefined") {
|
|
246
|
+
window.location.href = path;
|
|
247
|
+
}
|
|
248
|
+
});
|
|
249
|
+
const logoImage = config.ui.logoImage;
|
|
87
250
|
const form = useForm({
|
|
88
251
|
resolver: zodResolver(forgotPasswordSchema(t)),
|
|
89
252
|
defaultValues: {
|
|
90
253
|
account: ""
|
|
91
254
|
}
|
|
92
255
|
});
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
if (error) {
|
|
258
|
+
toast.error(error.title || "Error", {
|
|
259
|
+
description: error.description
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
}, [error]);
|
|
93
263
|
const handleSubmit = form.handleSubmit(async (values) => {
|
|
94
|
-
|
|
264
|
+
setIsLoading(true);
|
|
265
|
+
setError(null);
|
|
266
|
+
try {
|
|
267
|
+
const res = await forgotPasswordMutation.mutateAsync({
|
|
268
|
+
body: {
|
|
269
|
+
identifier: values.account
|
|
270
|
+
}
|
|
271
|
+
});
|
|
272
|
+
if ("verificationId" in res && res.verificationId) {
|
|
273
|
+
onNavigate(`/auth/reset-password?verificationId=${res.verificationId}`);
|
|
274
|
+
} else {
|
|
275
|
+
onNavigate(
|
|
276
|
+
`/auth/reset-password?identifier=${encodeURIComponent(values.account)}`
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
} catch (err) {
|
|
280
|
+
handleError(err, setError, t);
|
|
281
|
+
} finally {
|
|
282
|
+
setIsLoading(false);
|
|
283
|
+
}
|
|
95
284
|
});
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
{
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
285
|
+
let errorContent = null;
|
|
286
|
+
if (error) {
|
|
287
|
+
if (typeof error === "string") {
|
|
288
|
+
errorContent = { title: "Error", description: error };
|
|
289
|
+
} else {
|
|
290
|
+
errorContent = error;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
return /* @__PURE__ */ jsxs2(
|
|
294
|
+
AuthLayout,
|
|
295
|
+
{
|
|
296
|
+
title: t("title"),
|
|
297
|
+
description: t("description"),
|
|
298
|
+
logoImage,
|
|
299
|
+
footer: Link ? /* @__PURE__ */ jsx3(Link, { href: signInLink, className: "text-primary hover:underline", children: t("footer.backToSignIn") }) : /* @__PURE__ */ jsx3(
|
|
300
|
+
"a",
|
|
301
|
+
{
|
|
302
|
+
href: signInLink,
|
|
303
|
+
onClick: (e) => {
|
|
304
|
+
e.preventDefault();
|
|
305
|
+
onNavigate(signInLink);
|
|
306
|
+
},
|
|
307
|
+
className: "text-primary hover:underline",
|
|
308
|
+
children: t("footer.backToSignIn")
|
|
309
|
+
}
|
|
310
|
+
),
|
|
311
|
+
children: [
|
|
312
|
+
/* @__PURE__ */ jsxs2("form", { id: "forgot-password-form", onSubmit: handleSubmit, children: [
|
|
313
|
+
/* @__PURE__ */ jsx3(FieldGroup, { children: /* @__PURE__ */ jsx3(
|
|
314
|
+
Controller,
|
|
106
315
|
{
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
316
|
+
name: "account",
|
|
317
|
+
control: form.control,
|
|
318
|
+
render: ({ field, fieldState }) => /* @__PURE__ */ jsxs2(Field, { "data-invalid": fieldState.invalid, children: [
|
|
319
|
+
/* @__PURE__ */ jsx3(FieldLabel, { htmlFor: "forgot-password-account", children: t("form.accountLabel") }),
|
|
320
|
+
/* @__PURE__ */ jsx3(
|
|
321
|
+
Input,
|
|
322
|
+
{
|
|
323
|
+
...field,
|
|
324
|
+
id: "forgot-password-account",
|
|
325
|
+
type: "text",
|
|
326
|
+
placeholder: t("form.accountPlaceholder"),
|
|
327
|
+
"aria-invalid": fieldState.invalid
|
|
328
|
+
}
|
|
329
|
+
),
|
|
330
|
+
fieldState.invalid && /* @__PURE__ */ jsx3(FieldError, { errors: [fieldState.error] })
|
|
331
|
+
] })
|
|
112
332
|
}
|
|
113
|
-
),
|
|
114
|
-
|
|
333
|
+
) }),
|
|
334
|
+
/* @__PURE__ */ jsx3("div", { className: "mt-4", children: /* @__PURE__ */ jsxs2(
|
|
335
|
+
Button,
|
|
336
|
+
{
|
|
337
|
+
type: "submit",
|
|
338
|
+
form: "forgot-password-form",
|
|
339
|
+
className: "w-full",
|
|
340
|
+
disabled: isLoading || forgotPasswordMutation.isPending,
|
|
341
|
+
children: [
|
|
342
|
+
isLoading || forgotPasswordMutation.isPending && /* @__PURE__ */ jsx3(Spinner, {}),
|
|
343
|
+
isLoading || forgotPasswordMutation.isPending ? t("form.submitting") : t("form.submit")
|
|
344
|
+
]
|
|
345
|
+
}
|
|
346
|
+
) })
|
|
347
|
+
] }),
|
|
348
|
+
errorContent && /* @__PURE__ */ jsxs2(Alert, { variant: "destructive", className: "mt-4", children: [
|
|
349
|
+
/* @__PURE__ */ jsx3(IconAlertCircle, { className: "h-4 w-4" }),
|
|
350
|
+
/* @__PURE__ */ jsx3(AlertTitle, { children: errorContent.title }),
|
|
351
|
+
/* @__PURE__ */ jsx3(AlertDescription, { children: errorContent.description })
|
|
115
352
|
] })
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
Button,
|
|
120
|
-
{
|
|
121
|
-
type: "submit",
|
|
122
|
-
form: "forgot-password-form",
|
|
123
|
-
className: "w-full",
|
|
124
|
-
disabled: isLoading,
|
|
125
|
-
children: [
|
|
126
|
-
isLoading && /* @__PURE__ */ jsx2(Spinner, {}),
|
|
127
|
-
isLoading ? t("form.submitting") : t("form.submit")
|
|
128
|
-
]
|
|
129
|
-
}
|
|
130
|
-
) })
|
|
131
|
-
] });
|
|
353
|
+
]
|
|
354
|
+
}
|
|
355
|
+
);
|
|
132
356
|
};
|
|
133
357
|
export {
|
|
134
358
|
ForgotPassword
|