mirror of
https://github.com/StepanovPlaton/torrent_frontend.git
synced 2026-04-04 04:40:50 +04:00
Work on game form
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const userSchema = z.object({
|
||||
id: z.number().positive(),
|
||||
name: z.string().min(3),
|
||||
email: z.string().min(3),
|
||||
id: z.number().positive(),
|
||||
username: z.string().min(3),
|
||||
email: z.string().min(3),
|
||||
});
|
||||
export type User = z.infer<typeof userSchema>;
|
||||
|
||||
@@ -1,52 +1,53 @@
|
||||
import { HTTPService } from "@/shared/utils/http";
|
||||
import {
|
||||
LoginForm,
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
TokenResponse,
|
||||
tokenResponseSchema,
|
||||
LoginForm,
|
||||
TokenData,
|
||||
tokenDataSchema,
|
||||
TokenResponse,
|
||||
tokenResponseSchema,
|
||||
} from "./schemas/auth";
|
||||
import { jwtDecode } from "jwt-decode";
|
||||
import Cookies from "js-cookie";
|
||||
|
||||
export abstract class UserService {
|
||||
public static async Login(loginForm: LoginForm) {
|
||||
const accessToken = await HTTPService.post(
|
||||
"/auth",
|
||||
tokenResponseSchema,
|
||||
new URLSearchParams(Object.entries(loginForm)),
|
||||
{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
}
|
||||
);
|
||||
if (accessToken) {
|
||||
const tokenData = this.DecodeToken(accessToken);
|
||||
if (tokenData) {
|
||||
Cookies.set("access-token", accessToken, {
|
||||
secure: true,
|
||||
expires: tokenData.expire,
|
||||
});
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
}
|
||||
public static async Login(loginForm: LoginForm) {
|
||||
const accessToken = await HTTPService.post(
|
||||
"/auth",
|
||||
tokenResponseSchema,
|
||||
new URLSearchParams(Object.entries(loginForm)),
|
||||
{
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
false
|
||||
);
|
||||
if (accessToken) {
|
||||
const tokenData = this.DecodeToken(accessToken);
|
||||
if (tokenData) {
|
||||
Cookies.set("access-token", accessToken, {
|
||||
secure: true,
|
||||
expires: tokenData.expire,
|
||||
});
|
||||
return tokenData;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static GetToken(): string | undefined {
|
||||
return Cookies.get("access-token");
|
||||
}
|
||||
public static GetToken(): string | undefined {
|
||||
return Cookies.get("access-token");
|
||||
}
|
||||
|
||||
public static IdentifyYourself(): TokenData | undefined {
|
||||
const token = Cookies.get("access-token");
|
||||
if (token) {
|
||||
return this.DecodeToken(token);
|
||||
}
|
||||
}
|
||||
public static IdentifyYourself(): TokenData | undefined {
|
||||
const token = Cookies.get("access-token");
|
||||
if (token) {
|
||||
return this.DecodeToken(token);
|
||||
}
|
||||
}
|
||||
|
||||
public static DecodeToken(token: string): TokenData | undefined {
|
||||
const tokenPayload = jwtDecode(token);
|
||||
const parseResult = tokenDataSchema.safeParse(tokenPayload);
|
||||
if (parseResult.success) {
|
||||
return parseResult.data;
|
||||
} else console.error("JWT payload broken - " + parseResult.error);
|
||||
}
|
||||
public static DecodeToken(token: string): TokenData | undefined {
|
||||
const tokenPayload = jwtDecode(token);
|
||||
const parseResult = tokenDataSchema.safeParse(tokenPayload);
|
||||
if (parseResult.success) {
|
||||
return parseResult.data;
|
||||
} else console.error("JWT payload broken - " + parseResult.error);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user