Skip to content
Snippets Groups Projects
Commit ce748347 authored by Razzan Yoni's avatar Razzan Yoni
Browse files

resolve auth

parent 90690f04
Branches
No related merge requests found
...@@ -3,7 +3,7 @@ import React from "react"; ...@@ -3,7 +3,7 @@ import React from "react";
import { RenderRoutes } from "@/routes/RenderRoutes.tsx"; import { RenderRoutes } from "@/routes/RenderRoutes.tsx";
import { routes } from "@/routes/routes.ts"; import { routes } from "@/routes/routes.ts";
export type UserContext = {token: string | null, onLogin: (accessToken: string) => void, onLogout: () => void} export type UserContext = {token: string | null, username: string | null, onLogin: (accessToken: string, username: string) => void, onLogout: () => void}
export const AuthContext = React.createContext< export const AuthContext = React.createContext<
UserContext>(null as unknown as UserContext); UserContext>(null as unknown as UserContext);
......
...@@ -16,38 +16,8 @@ import { Link } from "react-router-dom"; ...@@ -16,38 +16,8 @@ import { Link } from "react-router-dom";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import {useAuth} from "@/TonalityApp.tsx"; import {useAuth} from "@/TonalityApp.tsx";
import api from "@/api/api.ts"; import api from "@/api/api.ts";
import {loginFormSchema} from "@/validations/login-validation.ts";
const isUsernameInvalid = async (username: string): Promise<boolean> => {
const res = await api.post("username-availability", {
username: username,
});
const responseBody: { usernameAvailable: string } = res.data;
return responseBody.usernameAvailable === "false";
};
const loginFormSchema = z.object({
username: z
.string()
.min(2, {
message: "Username has a minimum length of 2.",
})
.max(50, {
message: "Username has a maximum length of 50.",
})
.refine(isUsernameInvalid, {
message: "User does not exist.",
}),
password: z
.string()
.min(8, {
message: "Password has a minimum length of 8.",
})
.max(255, {
message: "Password has a maximum length of 255.",
}),
});
const LoginPage = () => { const LoginPage = () => {
// Define form // Define form
...@@ -77,9 +47,7 @@ const LoginPage = () => { ...@@ -77,9 +47,7 @@ const LoginPage = () => {
}, },
); );
console.log("res", res.data.accessToken) onLogin(res.data.accessToken, values.username);
onLogin(res.data.accessToken);
} catch (err) { } catch (err) {
if ( if (
axios.isAxiosError(err) && axios.isAxiosError(err) &&
......
...@@ -15,43 +15,10 @@ import { Input } from "@/components/ui/input.tsx"; ...@@ -15,43 +15,10 @@ import { Input } from "@/components/ui/input.tsx";
import { Button } from "@/components/ui/button.tsx"; import { Button } from "@/components/ui/button.tsx";
import { Link, useNavigate } from "react-router-dom"; import { Link, useNavigate } from "react-router-dom";
import { StatusCodes } from "http-status-codes"; import { StatusCodes } from "http-status-codes";
import axios from "axios";
import { useToast } from "@/components/ui/use-toast.ts"; import { useToast } from "@/components/ui/use-toast.ts";
import api from "@/api/api.ts"; import api from "@/api/api.ts";
import {signUpFormSchema} from "@/validations/signup-validation.ts";
const restApiUrl: string = import.meta.env.VITE_REST_API_URL;
const isUsernameAvailable = async (username: string): Promise<boolean> => {
const res = await axios.post(restApiUrl + "username-availability", {
username: username,
});
const responseBody: { usernameAvailable: string } = res.data;
return responseBody.usernameAvailable === "true";
};
const signUpFormSchema = z.object({
username: z
.string()
.min(2, {
message: "Username must have a minimum length of 2.",
})
.max(50, {
message: "Username must have a maximum length of 50.",
})
.refine(isUsernameAvailable, {
message: "Username already exists.",
}),
password: z
.string()
.min(8, {
message: "Password must have a minimum length of 8.",
})
.max(255, {
message: "Password must have a maximum length of 255.",
}),
});
const SignUpPage = () => { const SignUpPage = () => {
const { toast } = useToast(); const { toast } = useToast();
...@@ -109,7 +76,7 @@ const SignUpPage = () => { ...@@ -109,7 +76,7 @@ const SignUpPage = () => {
<FormItem> <FormItem>
<FormLabel>Password</FormLabel> <FormLabel>Password</FormLabel>
<FormControl> <FormControl>
<Input placeholder="" {...field} /> <Input type="password" placeholder="" {...field} />
</FormControl> </FormControl>
<FormMessage /> <FormMessage />
</FormItem> </FormItem>
......
import api from "@/api/api.ts";
import * as z from "zod";
const isUsernameInvalid = async (username: string): Promise<boolean> => {
const res = await api.post("username-availability", {
username: username,
});
const responseBody: { usernameAvailable: string } = res.data;
return responseBody.usernameAvailable === "false";
};
export const loginFormSchema = z.object({
username: z
.string()
.min(2, {
message: "Username has a minimum length of 2.",
})
.max(50, {
message: "Username has a maximum length of 50.",
})
.refine(isUsernameInvalid, {
message: "User does not exist.",
}),
password: z
.string()
.min(8, {
message: "Password has a minimum length of 8.",
})
.max(255, {
message: "Password has a maximum length of 255.",
}),
});
import * as z from "zod";
import api from "@/api/api.ts";
const isUsernameAvailable = async (username: string): Promise<boolean> => {
const res = await api.post(
"username-availability",
{
username: username,
});
const responseBody: { usernameAvailable: string } = res.data;
return responseBody.usernameAvailable === "true";
};
export const signUpFormSchema = z.object({
username: z
.string()
.min(2, {
message: "Username must have a minimum length of 2.",
})
.max(50, {
message: "Username must have a maximum length of 50.",
})
.refine(isUsernameAvailable, {
message: "Username already exists.",
}),
password: z
.string()
.min(8, {
message: "Password must have a minimum length of 8.",
})
.max(255, {
message: "Password must have a maximum length of 255.",
}),
});
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment