Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(admin): add error msg on login form #179

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions apps/admin/app/ui/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Form, FormControl, FormDescription, FormField, FormItem, FormMessage }
import { Input } from '@repo/ui/components/ui/input';
import { Label } from '@repo/ui/components/ui/label';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { saveCookie } from '../lib/utils';
Expand All @@ -24,6 +25,7 @@ export type User = {
export function LoginForm(): JSX.Element {
const router = useRouter();
const urlApi = process.env.NEXT_PUBLIC_API_URL;
const [errorMessage, setErrorMessage] = useState<string | null>(null);

const formSchema = z.object({
email: z.string().email({ message: 'Email invalide' }),
Expand All @@ -39,7 +41,7 @@ export function LoginForm(): JSX.Element {
});

async function onSubmit(values: z.infer<typeof formSchema>) {
fetch(`${urlApi}/auth/login`, {
const response = await fetch(`${urlApi}/auth/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand All @@ -48,22 +50,22 @@ export function LoginForm(): JSX.Element {
email: values.email,
password: values.password,
}),
})
.then((response) => response.json())
.then(async (data: { user: User; token: string }) => {
const roles = data.user.roles;
if (roles.some((role: { id: number }) => role.id === 5)) {
localStorage.setItem('user', JSON.stringify(data.user));
localStorage.setItem('access_token', data.token);
await saveCookie(data.user, data.token);
router.push('/dashboard');
} else {
throw new Error("Vous n'avez pas les droits pour accéder à cette page");
}
})
.catch((error: Error) => {
console.error(error);
});
});
if (!response.ok) {
setErrorMessage('Email ou mot de passe incorrect');
return;
}
const data = await response.json();
const roles = data.user.roles;

if (roles.some((role: { id: number }) => role.id === 5)) {
localStorage.setItem('user', JSON.stringify(data.user));
localStorage.setItem('access_token', data.token);
await saveCookie(data.user, data.token);
router.push('/dashboard');
} else {
setErrorMessage("Vous ne disposez pas des droits d'accès.");
}
}

return (
Expand All @@ -73,6 +75,7 @@ export function LoginForm(): JSX.Element {
<div className="text-center">
<h1 className="text-4xl font-bold mb-4">Dashboard Athlonix</h1>
</div>
{errorMessage && <div className="text-red-500 text-center mb-4">{errorMessage}</div>}
<div className="grid gap-4">
<div className="grid gap-2 my-4">
<FormField
Expand Down
4 changes: 2 additions & 2 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@repo/types": "workspace:*",
"@supabase/supabase-js": "^2.43.4",
"hono": "^4.4.4",
"stripe": "^15.9.0",
"stripe": "^15.10.0",
"zod": "^3.23.8",
"zod-validation-error": "^3.3.0"
},
Expand All @@ -32,7 +32,7 @@
"@types/node": "^20.14.2",
"@types/swagger-ui-dist": "^3.30.4",
"supabase": "^1.172.2",
"tsx": "^4.12.0",
"tsx": "^4.13.2",
"typescript": "^5.4.5",
"vitest": "^1.6.0"
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"@repo/biome-config": "workspace:*",
"@repo/supabase": "workspace:*",
"@repo/typescript-config": "workspace:*",
"turbo": "^2.0.1"
"turbo": "^2.0.3"
},
"packageManager": "pnpm@9.1.4",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@repo/biome-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@turbo/gen": "^2.0.1",
"@turbo/gen": "^2.0.3",
"@types/node": "^20.14.2",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
Expand Down
114 changes: 57 additions & 57 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading