mirror of
https://github.com/StepanovPlaton/torrent_frontend.git
synced 2026-04-04 04:40:50 +04:00
Add registration and data caching
This commit is contained in:
@@ -1,16 +1,28 @@
|
||||
import {
|
||||
loginFormSchema,
|
||||
loginFormFieldNames,
|
||||
LoginForm,
|
||||
} from "./schemas/auth";
|
||||
LoginFormType,
|
||||
} from "./schemas/login";
|
||||
|
||||
import {
|
||||
registrationFormSchema,
|
||||
registrationFormFieldNames,
|
||||
RegistrationFormType,
|
||||
RegistrationFormFields,
|
||||
} from "./schemas/registration";
|
||||
|
||||
import { userSchema, User } from "./schemas/user";
|
||||
import { UserService } from "./user";
|
||||
|
||||
export {
|
||||
loginFormSchema,
|
||||
loginFormFieldNames,
|
||||
registrationFormSchema,
|
||||
registrationFormFieldNames,
|
||||
UserService,
|
||||
userSchema,
|
||||
type User,
|
||||
type LoginForm,
|
||||
type LoginFormType,
|
||||
type RegistrationFormType,
|
||||
type RegistrationFormFields,
|
||||
};
|
||||
|
||||
13
src/entities/user/schemas/login.ts
Normal file
13
src/entities/user/schemas/login.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const loginFormSchema = z.object({
|
||||
username: z.string().min(3, "Логин слишком короткий"),
|
||||
password: z.string().min(3, "Пароль слишком короткий"),
|
||||
});
|
||||
export type LoginFormType = z.infer<typeof loginFormSchema>;
|
||||
export type LoginFormFieldsType = keyof LoginFormType;
|
||||
|
||||
export const loginFormFieldNames: { [key in LoginFormFieldsType]: string } = {
|
||||
username: "Логин",
|
||||
password: "Пароль",
|
||||
};
|
||||
20
src/entities/user/schemas/registration.ts
Normal file
20
src/entities/user/schemas/registration.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const registrationFormSchema = z.object({
|
||||
email: z
|
||||
.string()
|
||||
.min(3, "Почта слишком короткий")
|
||||
.email("Это не адрес электронной почты"),
|
||||
name: z.string().min(3, "Логин слишком короткий"),
|
||||
password: z.string().min(3, "Пароль слишком короткий"),
|
||||
});
|
||||
export type RegistrationFormType = z.infer<typeof registrationFormSchema>;
|
||||
export type RegistrationFormFields = keyof RegistrationFormType;
|
||||
|
||||
export const registrationFormFieldNames: {
|
||||
[key in RegistrationFormFields]: string;
|
||||
} = {
|
||||
email: "E-mail",
|
||||
name: "Логин",
|
||||
password: "Пароль",
|
||||
};
|
||||
@@ -1,16 +1,6 @@
|
||||
import { z } from "zod";
|
||||
import { userSchema } from "./user";
|
||||
|
||||
export const loginFormSchema = z.object({
|
||||
username: z.string().min(3, "Логин слишком короткий"),
|
||||
password: z.string().min(3, "Пароль слишком короткий"),
|
||||
});
|
||||
export const loginFormFieldNames = {
|
||||
username: "Логин",
|
||||
password: "Пароль",
|
||||
};
|
||||
export type LoginForm = z.infer<typeof loginFormSchema>;
|
||||
|
||||
export const tokenResponseSchema = z
|
||||
.object({
|
||||
access_token: z.string(),
|
||||
@@ -1,35 +1,45 @@
|
||||
import { HTTPService } from "@/shared/utils/http";
|
||||
import {
|
||||
LoginForm,
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
TokenResponse,
|
||||
tokenResponseSchema,
|
||||
} from "./schemas/auth";
|
||||
import { LoginFormType } from "./schemas/login";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
import Cookies from "js-cookie";
|
||||
import {
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
tokenResponseSchema,
|
||||
} from "./schemas/token";
|
||||
import { RegistrationFormType } from "./schemas/registration";
|
||||
|
||||
export abstract class UserService {
|
||||
public static async Login(loginForm: LoginForm) {
|
||||
public static async Registration(registrationForm: RegistrationFormType) {
|
||||
const accessToken = await HTTPService.post(
|
||||
"/auth",
|
||||
"/auth/registration",
|
||||
tokenResponseSchema,
|
||||
new URLSearchParams(Object.entries(loginForm)),
|
||||
{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
false
|
||||
{ body: registrationForm }
|
||||
);
|
||||
if (accessToken) {
|
||||
const tokenData = this.DecodeToken(accessToken);
|
||||
return this.ProcessToken(accessToken);
|
||||
}
|
||||
|
||||
public static async Login(loginForm: LoginFormType) {
|
||||
const accessToken = await HTTPService.post("/auth", tokenResponseSchema, {
|
||||
body: new URLSearchParams(Object.entries(loginForm)),
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
stringify: false,
|
||||
});
|
||||
return this.ProcessToken(accessToken);
|
||||
}
|
||||
|
||||
private static ProcessToken(token: string | null) {
|
||||
if (token) {
|
||||
const tokenData = this.DecodeToken(token);
|
||||
if (tokenData) {
|
||||
Cookies.set("access-token", accessToken, {
|
||||
Cookies.set("access-token", token, {
|
||||
secure: true,
|
||||
expires: tokenData.expire,
|
||||
});
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static GetToken(): string | undefined {
|
||||
|
||||
Reference in New Issue
Block a user