Skip to content

Commit

Permalink
Fixed errors when a server action was executed without reCAPTCHA
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianLeChat committed Nov 23, 2024
1 parent 6adee8d commit 1362541
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 48 deletions.
4 changes: 2 additions & 2 deletions app/[locale]/authentication/components/oauth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export default function OAuthForm()
{
startTransition( () =>
{
serverAction( signInAction, formData );
serverAction( signInAction, formData, messages );
} );
}}
>
Expand Down Expand Up @@ -125,7 +125,7 @@ export default function OAuthForm()
{
startTransition( () =>
{
serverAction( signInAction, formData );
serverAction( signInAction, formData, messages );
} );
}}
>
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/authentication/components/signin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default function SignInForm()
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( signInAction, formData );
serverAction( signInAction, formData, messages );
} );
}}
className="space-y-6"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/authentication/components/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default function SignUpForm()
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( signUpAction, formData );
serverAction( signUpAction, formData, messages );
} );
}}
className="space-y-6"
Expand Down
13 changes: 0 additions & 13 deletions app/[locale]/components/cookie-consent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

"use client";

import { toast } from "sonner";
import { useMessages } from "next-intl";
import { usePathname } from "next/navigation";
import { GoogleTagManager } from "@next/third-parties/google";
Expand Down Expand Up @@ -138,18 +137,6 @@ export default function CookieConsent()
process.env.NEXT_PUBLIC_ANALYTICS_ENABLED === "true"
);
}

// Google reCAPTCHA.
if (
!cookie.categories.includes( "security" )
&& process.env.NEXT_PUBLIC_RECAPTCHA_ENABLED === "true"
)
{
toast.warning( messages.form.errors.recaptcha_failed, {
duration: 10000,
description: messages.form.errors.recaptcha_error
} );
}
}
} );
}, [ pathname, messages ] );
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/components/notification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default function Notifications()

// Envoi de la requête au serveur et
// traitement de la réponse.
const state = await serverAction( updateReadState, new FormData() );
const state = await serverAction( updateReadState, new FormData(), formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down
15 changes: 7 additions & 8 deletions app/[locale]/components/user-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export default function UserMenu( { session }: { session: Session } )
{
// Déclaration des variables d'état.
const router = useRouter();
const messages = useTranslations( "header" );
const formMessages = useTranslations( "form" );
const headerMessages = useTranslations( "header" );
const [ isOpen, setOpen ] = useState( false );

// Déclaration des constantes.
Expand Down Expand Up @@ -141,9 +142,7 @@ export default function UserMenu( { session }: { session: Session } )
isAdmin ? "text-destructive" : "text-primary"
}`}
>
{isAdmin
? messages( "admin_account" )
: messages( "user_account" )}
{isAdmin ? headerMessages( "admin_account" ) : headerMessages( "user_account" )}
</p>
</DropdownMenuLabel>

Expand All @@ -154,7 +153,7 @@ export default function UserMenu( { session }: { session: Session } )
<DropdownMenuGroup>
<Link href="/dashboard">
<DropdownMenuItem>
{messages( "dashboard" )}
{headerMessages( "dashboard" )}

<DropdownMenuShortcut>
ALT/⌥ + D
Expand All @@ -164,7 +163,7 @@ export default function UserMenu( { session }: { session: Session } )

<Link href="/settings">
<DropdownMenuItem>
{messages( "settings" )}
{headerMessages( "settings" )}

<DropdownMenuShortcut>
ALT/⌥ + S
Expand All @@ -179,10 +178,10 @@ export default function UserMenu( { session }: { session: Session } )
<DropdownMenuItem
onClick={() =>
{
serverAction( signOutAccount, new FormData() );
serverAction( signOutAccount, new FormData(), formMessages );
}}
>
{messages( "logout" )}
{headerMessages( "logout" )}
<DropdownMenuShortcut>ALT/⌥ + L</DropdownMenuShortcut>
</DropdownMenuItem>
</DropdownMenuContent>
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/dashboard/components/file-history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default function FileHistory( {

// Envoi de la requête au serveur et
// attente de la réponse.
const data = await serverAction( restoreVersion, form );
const data = await serverAction( restoreVersion, form, formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down
9 changes: 6 additions & 3 deletions app/[locale]/dashboard/components/file-upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export default function FileUpload( {
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( uploadAction, formData );
serverAction( uploadAction, formData, formMessages );
} );
}}
>
Expand Down Expand Up @@ -567,7 +567,9 @@ export default function FileUpload( {
name={field.name}
checked={field.value}
disabled={isPending}
onCheckedChange={( value ) =>
onCheckedChange={(
value
) =>
{
if ( value )
{
Expand Down Expand Up @@ -718,7 +720,8 @@ export default function FileUpload( {
disabled={( date ) => date > oneYear
|| date < today}
onSelect={( value ) => field.onChange(
value?.toISOString() ?? ""
value?.toISOString()
?? ""
)}
className="rounded-md border"
initialFocus
Expand Down
26 changes: 21 additions & 5 deletions app/[locale]/dashboard/components/row-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ export default function RowActions( {

// Envoi de la requête au serveur et
// traitement de la réponse.
const files = ( await serverAction( changeFileStatus, form ) ) as string[];
const files = ( await serverAction(
changeFileStatus,
form,
formMessages
) ) as string[];

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -163,7 +167,11 @@ export default function RowActions( {

// Envoi de la requête au serveur et
// traitement de la réponse.
const files = ( await serverAction( changeFileStatus, form ) ) as string[];
const files = ( await serverAction(
changeFileStatus,
form,
formMessages
) ) as string[];

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -221,7 +229,11 @@ export default function RowActions( {

// Envoi de la requête au serveur et
// traitement de la réponse.
const files = ( await serverAction( renameFile, form ) ) as string[];
const files = ( await serverAction(
renameFile,
form,
formMessages
) ) as string[];

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -277,7 +289,11 @@ export default function RowActions( {

// Envoi de la requête au serveur et
// traitement de la réponse.
const files = ( await serverAction( deleteFile, form ) ) as string[];
const files = ( await serverAction(
deleteFile,
form,
formMessages
) ) as string[];

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -331,7 +347,7 @@ export default function RowActions( {

// Envoi de la requête au serveur et
// traitement de la réponse.
const state = await serverAction( deleteSharedUser, form );
const state = await serverAction( deleteSharedUser, form, formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down
6 changes: 3 additions & 3 deletions app/[locale]/dashboard/components/share-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export default function ShareManager( {

// Envoi de la requête au serveur et
// attente de la réponse.
const state = await serverAction( addSharedUser, form );
const state = await serverAction( addSharedUser, form, formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -145,7 +145,7 @@ export default function ShareManager( {

// Envoi de la requête au serveur et
// attente de la réponse.
const state = await serverAction( updateSharedUser, form );
const state = await serverAction( updateSharedUser, form, formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down Expand Up @@ -184,7 +184,7 @@ export default function ShareManager( {

// Envoi de la requête au serveur et
// attente de la réponse.
const state = await serverAction( deleteSharedUser, form );
const state = await serverAction( deleteSharedUser, form, formMessages );

// Fin de l'état de chargement.
setLoading( false );
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/appearance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export default function Appearance( { session }: { session: Session } )
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( updateAction, formData );
serverAction( updateAction, formData, messages );
} );
}}
className="space-y-8"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/issue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export default function Account()
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( updateAction, formData );
serverAction( updateAction, formData, messages );
} );
}}
className="space-y-8"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/notifications.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default function Notifications( { session }: { session: Session } )
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( updateAction, formData );
serverAction( updateAction, formData, messages );
} );
}}
className="space-y-8"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/privacy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default function Privacy()
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( deleteAction, formData );
serverAction( deleteAction, formData, formMessages );
} );
}}
className="space-y-8"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/storage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default function Storage( { session }: { session: Session } )
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( updateAction, formData );
serverAction( updateAction, formData, messages );
} );
}}
className="space-y-8"
Expand Down
2 changes: 1 addition & 1 deletion app/[locale]/settings/components/user.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export default function User( { session }: { session: Session } )
// Exécution de l'action côté serveur.
startTransition( () =>
{
serverAction( updateAction, formData );
serverAction( updateAction, formData, messages );
} );
}}
className="space-y-8"
Expand Down
35 changes: 31 additions & 4 deletions utilities/recaptcha.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

"use client";

import { toast } from "sonner";

export default async function serverAction(
action: ( payload: FormData ) => void,
formData: FormData
formData: FormData,
messages: ( key: string ) => string
)
{
// On vérifie d'abord si le service reCAPTCHA est présent ou non.
Expand All @@ -16,9 +19,33 @@ export default async function serverAction(
// ne sont pas encore chargés dans le navigateur de l'utilisateur.
if ( typeof window.grecaptcha === "undefined" )
{
// Dans ce cas, on traite la requête comme si le service reCAPTCHA n'était
// pas activé et on laisse le serveur répondre à l'utilisateur.
return action( formData );
// Premier cas de figure : le serveur utilise reCAPTCHA mais le client
// n'a pas encore chargé les services de Google reCAPTCHA ou ils ont été
// explicitement désactivés par l'utilisateur.
if ( process.env.NEXT_PUBLIC_RECAPTCHA_ENABLED === "true" )
{
toast.error( messages( "errors.recaptcha_failed" ), {
duration: 10000,
description: messages( "errors.recaptcha_error" )
} );

return false;
}

// Deuxième cas de figure : le serveur n'utilise pas reCAPTCHA.
// On exécute alors l'action côté serveur sans vérification.
try
{
return action( formData );
}
catch
{
toast.error( messages( "errors.internal_error" ), {
description: messages( "errors.server_error" )
} );

return false;
}
}

// On créé après une promesse afin de gérer le chargement des services de
Expand Down

0 comments on commit 1362541

Please sign in to comment.