Skip to content
Open
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
132 changes: 92 additions & 40 deletions app/(full-page)/auth/login/page.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,115 @@
/* eslint-disable @next/next/no-img-element */
'use client';
import { useRouter } from 'next/navigation';
import React, { useContext, useState } from 'react';
import React, { use, useContext, useState } from 'react';
import { Checkbox } from 'primereact/checkbox';
import { Button } from 'primereact/button';
import { Password } from 'primereact/password';
import { LayoutContext } from '../../../../layout/context/layoutcontext';
import { InputText } from 'primereact/inputtext';
import { classNames } from 'primereact/utils';
import InfoBanner from '@/app/components/InfoBanner';

import { useForm } from 'react-hook-form';
import { schema } from '@/schemas/schema';
import { yupResolver } from '@hookform/resolvers/yup';
import { Controller } from 'react-hook-form';
import { json } from 'stream/consumers';
import { getUser, login } from '@/services/auth';
import FancyLinkBtn from '@/app/components/buttons/FancyLinkBtn';
import { getToken } from '@/utils/auth';
import { logout } from '@/utils/logout';
import { LoginType } from '@/types/login';

const LoginPage = () => {
const [password, setPassword] = useState('');
const [checked, setChecked] = useState(false);
const { layoutConfig } = useContext(LayoutContext);
const { layoutConfig, setUser, setMessage, setGlobalLoading} = useContext(LayoutContext);

const router = useRouter();
const containerClassName = classNames('surface-ground flex align-items-center justify-content-center min-h-screen min-w-screen overflow-hidden', { 'p-input-filled': layoutConfig.inputStyle === 'filled' });
// const containerClassName = classNames('surface-ground flex align-items-center justify-content-center min-h-screen min-w-screen overflow-hidden', { 'p-input-filled': layoutConfig.inputStyle === 'filled' });

const {
register,
handleSubmit,
formState: { errors },
control
} = useForm({
resolver: yupResolver(schema),
mode: 'onChange'
});

const onSubmit = async (value:LoginType) => {
console.log('Данные пользователя: ', value);

const user = await login(value);
console.log(user);
if (user && user.success) {
document.cookie = `access_token=${user.token.access_token}; path=/; Secure; SameSite=Strict; expires=${user.token.expires_at}`;

const token = user.token.access_token;
if (token) {
const res = await getUser(token);
try {
if (res?.success) {
console.log(res);
if (res?.user.is_working) {
window.location.href = '/course';
}

} else {
logout({ setUser, setGlobalLoading });
setMessage({
state: true,
value: { severity: 'error', summary: 'Ошибка', detail: 'Ошибка при авторизации' }
}); // messege - Ошибка при авторизации
console.log('Ошибка при получении пользователя');
}
} catch (error) {
logout({ setUser, setGlobalLoading });
setMessage({
state: true,
value: { severity: 'error', summary: 'Ошибка', detail: 'Ошибка при авторизации' }
}); // messege - Ошибка при авторизации
console.log('Ошибка при получении пользователя');
}
}
} else {
console.log('Ошибка при авторизации');
}
};

return (
<div className={containerClassName}>
<div className="flex flex-column align-items-center justify-content-center">
<img src={`/layout/images/logo-${layoutConfig.colorScheme === 'light' ? 'dark' : 'white'}.svg`} alt="Sakai logo" className="mb-5 w-6rem flex-shrink-0" />
<div
style={{
borderRadius: '56px',
padding: '0.3rem',
background: 'linear-gradient(180deg, var(--primary-color) 10%, rgba(33, 150, 243, 0) 30%)'
}}
>
<div className="w-full surface-card py-8 px-5 sm:px-8" style={{ borderRadius: '53px' }}>
<div className="text-center mb-5">
<img src="/demo/images/login/avatar.png" alt="Image" height="50" className="mb-3" />
<div className="text-900 text-3xl font-medium mb-3">Welcome, Isabel!</div>
<span className="text-600 font-medium">Sign in to continue</span>
</div>
<div className={'flex flex-col gap-4'}>
<InfoBanner title="Кирүү" />
<div className="flex gap-4 flex-column lg:flex-row items-center justify-evenly px-4 mb-8">
<div className="user-img">
<img src="/layout/images/man.png" className="w-[500px]" alt="" />
</div>

<div>
<label htmlFor="email1" className="block text-900 text-xl font-medium mb-2">
Email
<div className="w-[90%] sm:w-[500px] shadow-2xl py-6 px-3 md:py-8 sm:px-4 md:px-8 rounded">
<form onSubmit={handleSubmit(onSubmit)} className="flex flex-col gap-4 md:gap-6">
<div className="flex flex-col">
<label htmlFor="email1" className="block text-900 text-[16px] md:text-xl font-medium mb-1 md:mb-2">
MyEdu email
</label>
<InputText id="email1" type="text" placeholder="Email address" className="w-full md:w-30rem mb-5" style={{ padding: '1rem' }} />
<InputText {...register('email')} id="email1" type="text" placeholder="[email protected]" className="w-[90%] p-2 sm:p-3" />
{errors.email && <b className="text-red-500 text-[12px] ml-2">{errors.email.message}</b>}
</div>

<label htmlFor="password1" className="block text-900 font-medium text-xl mb-2">
Password
<div className="flex flex-col">
<label htmlFor="password1" className="block text-900 font-medium text-[16px] md:text-xl mb-1 md:mb-2">
Сыр сөз
</label>
<Password inputId="password1" value={password} onChange={(e) => setPassword(e.target.value)} placeholder="Password" toggleMask className="w-full mb-5" inputClassName="w-full p-3 md:w-30rem"></Password>

<div className="flex align-items-center justify-content-between mb-5 gap-5">
<div className="flex align-items-center">
<Checkbox inputId="rememberme1" checked={checked} onChange={(e) => setChecked(e.checked ?? false)} className="mr-2"></Checkbox>
<label htmlFor="rememberme1">Remember me</label>
</div>
<a className="font-medium no-underline ml-2 text-right cursor-pointer" style={{ color: 'var(--primary-color)' }}>
Forgot password?
</a>
</div>
<Button label="Sign In" className="w-full p-3 text-xl" onClick={() => router.push('/')}></Button>
<Controller
name="password"
control={control}
defaultValue=""
render={({ field }) => <Password {...field} toggleMask className="w-[100%]" inputClassName="w-[90%] p-2 sm:p-3" inputStyle={{ marginRight: '30px' }} feedback={false} />}
/>
{errors.password && <b className="text-red-500 text-[12px] ml-2">{errors.password.message}</b>}
</div>
</div>
{/* <Button label="Кирүү" type='submit' className="w-full p-2 md:p-3 text-[14px] md:text-xl"></Button> */}
<FancyLinkBtn btnWidth={'90%'} backround={'--mainColor'} effectBg={'--titleColor'} title={'Кирүү'} />
</form>
</div>
</div>
</div>
Expand Down
Loading