fix: auth

This commit is contained in:
David Nguyen
2025-02-09 00:46:25 +11:00
parent f5bfec1990
commit e128e9369e
13 changed files with 188 additions and 142 deletions

View File

@ -58,7 +58,7 @@ export const PasswordForm = ({ className }: PasswordFormProps) => {
const onFormSubmit = async ({ currentPassword, password }: TPasswordFormSchema) => {
try {
await authClient.updatePassword({
await authClient.emailPassword.updatePassword({
currentPassword,
password,
});

View File

@ -1,6 +1,7 @@
import { useEffect, useMemo, useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod';
import type { MessageDescriptor } from '@lingui/core';
import { msg } from '@lingui/core/macro';
import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
@ -40,12 +41,20 @@ import { PasswordInput } from '@documenso/ui/primitives/password-input';
import { PinInput, PinInputGroup, PinInputSlot } from '@documenso/ui/primitives/pin-input';
import { useToast } from '@documenso/ui/primitives/use-toast';
const CommonErrorMessages = {
// [AuthenticationErrorCode.USER_MISSING_PASSWORD]:
// 'This account appears to be using a social login method, please sign in using that method',
const CommonErrorMessages: Record<string, MessageDescriptor> = {
[AuthenticationErrorCode.AccountDisabled]: msg`This account has been disabled. Please contact support.`,
};
const handleFallbackErrorMessages = (code: string) => {
const message = CommonErrorMessages[code];
if (!message) {
return msg`An unknown error occurred`;
}
return message;
};
const LOGIN_REDIRECT_PATH = '/documents';
export const ZSignInFormSchema = z.object({
@ -163,12 +172,7 @@ export const SignInForm = ({
credential: JSON.stringify(credential),
csrfToken: sessionId,
redirectUrl,
// callbackUrl,
// redirect: false,
});
// Todo: Can't use navigate because of embed?
// window.location.href = callbackUrl;
} catch (err) {
setIsPasskeyLoading(false);
@ -186,10 +190,10 @@ export const SignInForm = ({
msg`This passkey is not configured for this application. Please login and add one in the user settings.`,
)
.with(
AuthenticationErrorCode.SessionExpired, // Todo
AuthenticationErrorCode.SessionExpired,
() => msg`This session has expired. Please try again.`,
)
.otherwise(() => msg`Please try again later or login using your normal details`);
.otherwise(() => handleFallbackErrorMessages(error.code));
toast({
title: 'Something went wrong',
@ -208,11 +212,7 @@ export const SignInForm = ({
totpCode,
backupCode,
redirectUrl,
// callbackUrl,
// redirect: false,
});
// window.location.href = callbackUrl; // Todo: Handle redirect.
} catch (err) {
console.log(err);
@ -245,7 +245,7 @@ export const SignInForm = ({
AuthenticationErrorCode.InvalidTwoFactorCode,
() => msg`The two-factor authentication code provided is incorrect`,
)
.otherwise(() => msg`An unknown error occurred`);
.otherwise(() => handleFallbackErrorMessages(error.code));
toast({
title: _(msg`Unable to sign in`),
@ -257,7 +257,7 @@ export const SignInForm = ({
const onSignInWithGoogleClick = async () => {
try {
await authClient.google.signIn(); // Todo: Handle redirect.
await authClient.google.signIn();
} catch (err) {
toast({
title: _(msg`An unknown error occurred`),

View File

@ -124,7 +124,13 @@ export const SignUpForm = ({
const onFormSubmit = async ({ name, email, password, signature, url }: TSignUpFormSchema) => {
try {
await authClient.emailPassword.signUp({ name, email, password, signature, url });
await authClient.emailPassword.signUp({
name,
email,
password,
signature,
url,
});
await navigate(`/unverified-account`);

View File

@ -1,13 +1,13 @@
import { useState } from 'react';
import { Trans } from '@lingui/react/macro';
import { Trans, useLingui } from '@lingui/react/macro';
import { RecipientRole } from '@prisma/client';
import { useNavigate } from 'react-router';
import { authClient } from '@documenso/auth/client';
import { Alert, AlertDescription } from '@documenso/ui/primitives/alert';
import { Button } from '@documenso/ui/primitives/button';
import { DialogFooter } from '@documenso/ui/primitives/dialog';
import { useToast } from '@documenso/ui/primitives/use-toast';
import { useRequiredDocumentSigningAuthContext } from './document-signing-auth-provider';
@ -24,7 +24,9 @@ export const DocumentSigningAuthAccount = ({
}: DocumentSigningAuthAccountProps) => {
const { recipient } = useRequiredDocumentSigningAuthContext();
const navigate = useNavigate();
const { t } = useLingui();
const { toast } = useToast();
const [isSigningOut, setIsSigningOut] = useState(false);
@ -32,18 +34,18 @@ export const DocumentSigningAuthAccount = ({
try {
setIsSigningOut(true);
// Todo
await authClient.signOut();
// {
// // redirect: false,
// // Todo: Redirect to signin like below
// }
await navigate(`/signin#email=${email}`);
await authClient.signOut({
redirectUrl: `/signin#email=${email}`,
});
} catch {
setIsSigningOut(false);
// Todo: Alert.
toast({
title: t`Something went wrong`,
description: t`We were unable to log you out at this time.`,
duration: 10000,
variant: 'destructive',
});
}
};

View File

@ -3,7 +3,6 @@ import { useState } from 'react';
import { msg } from '@lingui/core/macro';
import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
import { useNavigate } from 'react-router';
import { authClient } from '@documenso/auth/client';
import { Button } from '@documenso/ui/primitives/button';
@ -21,18 +20,15 @@ export const DocumentSigningAuthPageView = ({
const { _ } = useLingui();
const { toast } = useToast();
const navigate = useNavigate();
const [isSigningOut, setIsSigningOut] = useState(false);
const handleChangeAccount = async (email: string) => {
try {
setIsSigningOut(true);
// Todo: Redirect false
await authClient.signOut();
await navigate(emailHasAccount ? `/signin#email=${email}` : `/signup#email=${email}`);
await authClient.signOut({
redirectUrl: emailHasAccount ? `/signin#email=${email}` : `/signup#email=${email}`,
});
} catch {
toast({
title: _(msg`Something went wrong`),

View File

@ -27,8 +27,6 @@ export const loader = ({ params }: Route.LoaderArgs) => {
};
export default function VerifyEmailPage({ loaderData }: Route.ComponentProps) {
console.log('hello world');
const { token } = loaderData;
const { _ } = useLingui();
@ -42,7 +40,6 @@ export default function VerifyEmailPage({ loaderData }: Route.ComponentProps) {
setIsLoading(true);
try {
// Todo: Types and check.
const response = await authClient.emailPassword.verifyEmail({
token,
});

View File

@ -40,8 +40,9 @@ export default defineConfig({
external: ['@node-rs/bcrypt', '@prisma/client'],
},
optimizeDeps: {
// include: ['react-icons'],
exclude: ['@node-rs/bcrypt'],
entries: ['./app/**/*', '../../packages/ui/**/*', '../../packages/lib/**/*'],
include: ['prop-types', 'file-selector', 'attr-accept'],
exclude: ['node_modules', '@node-rs/bcrypt', '@documenso/pdf-sign', 'sharp'],
},
resolve: {
alias: {