token refresher

This commit is contained in:
amNobodyyy
2025-01-29 00:21:08 +05:30
parent 16db1725f3
commit 597e47c2d0
10 changed files with 265 additions and 100 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect , useState } from "react";
import React, { useEffect, useState } from "react";
import { Container, Col, Row } from "react-bootstrap";
import { FcGoogle } from "react-icons/fc";
import axios from "axios";
@@ -18,7 +18,6 @@ function AuthPage() {
const notifyError = (message) => {
toast.error(message);
};
function ToggleSign(event) {
event.preventDefault();
@@ -40,38 +39,39 @@ function AuthPage() {
async function handleSubmit(event) {
event.preventDefault();
if (!formData.username.trim() && signin) {
notifyError("Username cannot be empty");
return;
}
if (!formData.username.trim() && signin) {
notifyError("Username cannot be empty");
return;
}
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailRegex.test(formData.email)) {
notifyError("Enter a valid email address");
return;
}
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
if (!emailRegex.test(formData.email)) {
notifyError("Enter a valid email address");
return;
}
// Check password length
if (formData.password.length < 8) {
notifyError("Password must be at least 8 characters long");
return;
}
// Check password length
if (formData.password.length < 8) {
notifyError("Password must be at least 8 characters long");
return;
}
try {
const response = await axios.post(
`http://localhost:8080/api/${
!signin ? "login" : "register"
}`,
formData
`http://localhost:8080/api/${!signin ? "login" : "register"}`,
formData,
{ withCredentials: true }
);
const { user } = response.data;
delete user.password;
const gravatarUrl = `https://www.gravatar.com/avatar/${md5(
user.email
)}?d=identicon`;
user.profilePicture = gravatarUrl;
localStorage.setItem("user", JSON.stringify(user));
window.location.href = "/Welcom";
if (response.status === 200) {
const { user } = response.data;
delete user.password;
const gravatarUrl = `https://www.gravatar.com/avatar/${md5(
user.email
)}?d=identicon`;
user.profilePicture = gravatarUrl;
window.location.href = "/Welcome";
}
} catch (error) {
console.error("Authentication error:", error);
if (
@@ -88,11 +88,9 @@ function AuthPage() {
const handleGoogleLogin = (event) => {
event.preventDefault();
window.location.href =
"http://localhost:8080/auth/google";
window.location.href = "http://localhost:8080/auth/google";
};
return (
<>
<ToastContainer />