Skip to content

Commit

Permalink
feat(client): add register page and some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lareii committed Sep 13, 2024
1 parent 9a5a8ae commit 79ee684
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 12 deletions.
12 changes: 8 additions & 4 deletions client/app/(auth)/layout.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
'use client';

import { motion, AnimatePresence } from 'framer-motion';
import { usePathname } from 'next/navigation';
import Image from 'next/image';
import { motion, AnimatePresence } from 'framer-motion';

export default function Layout({ children }) {
const pathname = usePathname().split('/')[1];

return (
<AnimatePresence mode='wait'>
<motion.div
key={pathname}
initial={{ y: 20, opacity: 0 }}
animate={{ y: 0, opacity: 1 }}
transition={{ ease: 'easeInOut', duration: 0.3 }}
>
<div className='h-screen flex justify-center sm:items-center sm:p-5'>
<div className='rounded-[var(--radius)] min-h-96 w-[50rem] flex flex-col p-10 max-sm:gap-y-10 sm:bg-zinc-100 sm:bg-zinc-900 max-sm:border-0'>
<div className='h-screen flex justify-center md:items-center md:p-5'>
<div className='rounded-[var(--radius)] h-96 w-[50rem] flex flex-col p-10 max-md:gap-y-10 md:bg-zinc-100 md:bg-zinc-900 max-md:border-0'>
<Image
src='/copluk.svg'
alt='copl.uk logo'
width={100}
height={100}
/>
<div className='grow flex flex-col justify-center'>{children}</div>
<div className='h-full flex flex-col justify-center'>{children}</div>
</div>
</div>
</motion.div>
Expand Down
12 changes: 6 additions & 6 deletions client/app/(auth)/login/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { useToast } from '@/components/ui/use-toast';
import { login } from '@/lib/api/auth';

const formSchema = z.object({
username: z.string().min(1, 'kullanıcı adı boş bırakılamaz'),
password: z.string().min(1, 'parola boş bırakılamaz')
username: z.string().min(1, 'kullanıcı adı boş bırakılamaz.'),
password: z.string().min(1, 'parola boş bırakılamaz.')
});

export default function Page() {
Expand Down Expand Up @@ -63,11 +63,11 @@ export default function Page() {
}

return (
<div className='flex max-sm:flex-col justify-between items-start gap-10'>
<div className='basis-1/2'>
<div className='flex max-md:flex-col justify-between items-start gap-5 h-full'>
<div className='basis-1/2 mt-10'>
<div className='text-2xl font-bold'>oturum açın</div>
<div className='text-sm'>
çöplüğe giriş yapmak için lütfen bilgilerinizi girin.
copl.uk{'\''}e giriş yapmak için lütfen bilgilerinizi girin.
</div>
<div className='text-muted-foreground text-xs mt-5'>
henüz bir hesabın yok mu?{' '}
Expand All @@ -79,7 +79,7 @@ export default function Page() {
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className='flex flex-col gap-y-5 basis-1/2 w-full'
className='basis-1/2 flex flex-col justify-center gap-y-5 w-full h-full'
>
<div className='flex flex-col gap-y-2'>
<FormField
Expand Down
191 changes: 190 additions & 1 deletion client/app/(auth)/register/page.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,192 @@
'use client';

import Link from 'next/link';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { zodResolver } from '@hookform/resolvers/zod';
import { useForm } from 'react-hook-form';
import { z } from 'zod';
import { LoaderCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage
} from '@/components/ui/form';
import { useToast } from '@/components/ui/use-toast';
import { register } from '@/lib/api/auth';

const formSchema = z
.object({
email: z.string().email('geçerli bir e-posta adresi girin.'),
display_name: z
.string()
.min(1, 'görünen isminiz 1-25 karakter uzunluğunda olmalıdır.')
.max(25, 'görünen isminiz 1-25 karakter uzunluğunda olmalıdır.'),
username: z
.string()
.min(1, 'kullanıcı adınız 3-25 karakter uzunluğunda olmalıdır.')
.max(25, 'kullanıcı adınız 3-25 karakter uzunluğunda olmalıdır.')
.regex(
/^[a-zA-Z0-9._]+$/,
'kullanıcı adınız sadece harf, rakam, nokta ve alt çizgi içerebilir.'
),
password: z
.string()
.min(8, 'parolanız 8-50 karakter uzunluğunda olmalıdır.')
.max(50, 'parolanız 8-50 karakter uzunluğunda olmalıdır.'),
passwordConfirmation: z.string()
})
.refine((data) => data.password === data.passwordConfirmation, {
message: 'parolalar eşleşmiyor',
path: ['passwordConfirmation']
});

export default function Page() {
return <div>register</div>;
const [isSubmitting, setIsSubmitting] = useState(false);
const router = useRouter();
const { toast } = useToast();

const form = useForm({
resolver: zodResolver(formSchema)
});

async function onSubmit(values) {
setIsSubmitting(true);

const response = await register({
email: values.email,
display_name: values.display_name,
username: values.username,
password: values.password
});

if (!response) {
toast({
title: 'hay aksi, bir şeyler ters gitti!',
description: 'bir hata oluştu. lütfen daha sonra tekrar deneyin.',
duration: 3000
});
setIsSubmitting(false);
return;
}
if (response.status === 409) {
toast({
title: 'hay aksi, bir şeyler ters gitti!',
description: 'bu e-posta adresi veya kullanıcı adı zaten kullanımda.',
duration: 3000
});
setIsSubmitting(false);
return;
}

setIsSubmitting(false);
router.push('/login');
router.refresh();
}

return (
<div className='flex max-md:flex-col justify-between items-start gap-5 h-full'>
<div className='basis-1/2 mt-10'>
<div className='text-2xl font-bold'>kayıt olun</div>
<div className='text-sm'>
copl.uk{"'"}e katılmak için lütfen bilgilerinizi girin.
</div>
<div className='text-muted-foreground text-xs mt-5'>
hali hazırda bir hesabın var mı?{' '}
<Link href='/login' className='underline'>
giriş yap
</Link>
</div>
</div>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className='basis-1/2 flex flex-col gap-y-5 justify-center w-full h-full'
>
<div className='flex flex-col gap-y-2'>
<FormField
control={form.control}
name='email'
render={({ field }) => (
<FormItem className='space-y-1'>
<FormLabel>e-posta</FormLabel>
<FormControl>
<Input placeholder='[email protected]' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className='flex gap-x-2'>
<FormField
control={form.control}
name='display_name'
render={({ field }) => (
<FormItem className='space-y-1 w-full'>
<FormLabel>görünen isim</FormLabel>
<FormControl>
<Input placeholder='çöpçü' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='username'
render={({ field }) => (
<FormItem className='space-y-1 w-full'>
<FormLabel>kullanıcı adı</FormLabel>
<FormControl>
<Input placeholder='copcu1337' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className='flex gap-x-2'>
<FormField
control={form.control}
name='password'
render={({ field }) => (
<FormItem className='space-y-1 w-full'>
<FormLabel>parola</FormLabel>
<FormControl>
<Input type='password' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name='passwordConfirmation'
render={({ field }) => (
<FormItem className='space-y-1 w-full'>
<FormLabel>parola tekrarı</FormLabel>
<FormControl>
<Input type='password' {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
</div>
<Button type='submit' className='w-full' disabled={isSubmitting}>
{isSubmitting && (
<LoaderCircle className='w-4 h-4 mr-2 animate-spin' />
)}
kayıt oluyorum
</Button>
</form>
</Form>
</div>
);
}
2 changes: 1 addition & 1 deletion client/app/app/settings/account/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default function Page() {
const emailForm = useForm({
resolver: zodResolver(
z.object({
email: z.string().email('geçerli bir e-posta adresi girin')
email: z.string().email('geçerli bir e-posta adresi girin.')
})
),
defaultValues: {
Expand Down
9 changes: 9 additions & 0 deletions client/lib/api/auth/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import api from '@/lib/api';

export async function register(data) {
try {
const response = await api.post('/auth/register', data);
return response;
} catch (error) {
return error.response;
}
}

export async function login(username, password) {
try {
const response = await api.post('/auth/login', { username, password });
Expand Down

0 comments on commit 79ee684

Please sign in to comment.