Skip to content

Commit

Permalink
доделал валидацию авторизации
Browse files Browse the repository at this point in the history
  • Loading branch information
semant1cs committed Dec 11, 2023
1 parent 3532e0e commit 93ee7db
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 28 deletions.
15 changes: 9 additions & 6 deletions frontend/src/UIComponents/customInput/CustomInput.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
import React from 'react';
import "./customInput.scss"
import {FieldValues, RegisterOptions, UseFormRegister} from "react-hook-form";
import {RegisterOptions, UseFormRegister} from "react-hook-form";
import {FormValues} from "../../components/authentication/AuthForm.tsx";


interface IPropTypes {
placeholder?: string,
type: string,
name: string,
name?: "login" | "password",
className?: string,
autoFocus?: boolean,
disabled?: boolean,
validateRules: RegisterOptions,
validateRules?: RegisterOptions,
width?: string,
height?: string,
defaultValue?: string,
register: UseFormRegister<FieldValues>,
register?: UseFormRegister<FormValues>,
required?: boolean,
value?: string,
}

const CustomInput: React.FC<IPropTypes> =
Expand All @@ -30,7 +33,7 @@ const CustomInput: React.FC<IPropTypes> =
defaultValue,
register,
validateRules,
...inputProps
...inputProps
}) => {
return (
<div className="custom-input" style={{width: width, height: height}}>
Expand All @@ -44,7 +47,7 @@ const CustomInput: React.FC<IPropTypes> =
disabled={disabled !== undefined ? disabled : false}
style={{height: height}}
defaultValue={defaultValue}
{...register(name, validateRules)}
{...register !== undefined && name !== undefined ? {...register(name, validateRules)} : ""}
{...inputProps}
/>
{/*{error.message && <span className="custom-input__error">{error.message}</span>}*/}
Expand Down
36 changes: 23 additions & 13 deletions frontend/src/components/authentication/AuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,60 @@ import {useForm} from "react-hook-form";

import {observer} from "mobx-react-lite";

import rulesValidation from "../../utils/auth.ts";
import authStore from "../../store/authStore.ts";
import auth from "../../utils/auth.ts";

export type FormValues = {
login: string,
password: string,
}

const AuthForm: React.FC = observer(() => {
const navigateTo = useNavigate()
const {register, handleSubmit} = useForm()
const {register, handleSubmit} = useForm<FormValues>()

const [isPasswordShows, setIsPasswordShows] = useState(false)

const handleOnChangeIsPasswordShows = useCallback(() => {
setIsPasswordShows(!isPasswordShows)
}, [isPasswordShows])

const onHandleSubmit = useCallback(({login, password}) => {
authStore.signIn(login, password).then(() => navigateTo('/map'))
const onHandleSubmit = useCallback((data: {login: string, password: string}) => {
authStore.signIn(data.login, data.password).then(() => navigateTo('/map'))
console.log(data.login, data.password)
}, [navigateTo])

return (
<form className="auth__form" onSubmit={handleSubmit((data) => onHandleSubmit(data))}>
<form
className="auth__form"
onSubmit={handleSubmit((data: FormValues) => onHandleSubmit(data))}
>
<h2 className="auth-form-title">ВХОД</h2>

<fieldset className="auth-fields">
<CustomInput
type="email"
type="text"
register={register}
name={rulesValidation.login.fieldName}
validateRules={rulesValidation.login.rules}
autoFocus={true}
placeholder="Логин"
name="login"
validateRules={auth.login.rules}
/>

<CustomInput
type={isPasswordShows ? "text" : "password"}
register={register}
name={rulesValidation.password.fieldName}
validateRules={rulesValidation.password.rules}
placeholder="Пароль"
name={auth.password.fieldName}
validateRules={auth.password.rules}
/>
</fieldset>

<CustomCheckbox
text="Показать пароль"
id="is-remember"
additionalClassName="is-remember-password__checkbox"
handleOnChange={handleOnChangeIsPasswordShows}
isChecked={isPasswordShows}
/>

<input type="submit" className="auth__btn" value="ВОЙТИ"/>
</form>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/mapMenu/MapMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const MapMenu: React.FC = observer(() => {

return (
<div>
{!user?.is_superuser
{user?.is_superuser
? <SuperUserMap/>
: <EmployeeMap user={user} formattedDate={formattedDate}/>
}
Expand Down
22 changes: 15 additions & 7 deletions frontend/src/store/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,14 @@ import axios from "axios";

class AuthStore {
isUserAuthorized: boolean = false

// userRole: string | null = null

constructor() {
makeAutoObservable(this)
}

async logOutUser() {
this.isUserAuthorized = false
axios.post("http://localhost:8000/auth/logout").catch(reason => console.log(reason))
}

async signIn(login: string, password: string) {
console.log(login, password)
await axios.post("http://localhost:8000/auth/login", {
username: login,
password: password,
Expand All @@ -26,10 +21,23 @@ class AuthStore {
'Content-Type': 'application/x-www-form-urlencoded'
}, withCredentials: true
})
.then(() => this.isUserAuthorized = true)
.then(() => this.authorize())
.catch(reason => console.log(reason))
}

async logOutUser() {
this.unauthorize()
axios.post("http://localhost:8000/auth/logout").catch(reason => console.log(reason))
}

private unauthorize = () => {
this.isUserAuthorized = false
}

private authorize = () => {
this.isUserAuthorized = true
}

private async signUp(login: string, password: string,) {
axios.post("http://localhost:8000/auth/register", {
username: login,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/types/RuleValidationType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export interface IRuleValidationListType {
}

export interface IRuleValidationType {
fieldName: string,
fieldName: "login" | "password",
rules: RegisterOptions<FieldValues, string>
}

0 comments on commit 93ee7db

Please sign in to comment.