mirror of
https://github.com/bubblecup-12/VogelSocialMedia.git
synced 2025-07-07 19:18:51 +00:00
tokens refresh when jwt is expired and added basic axios config
This commit is contained in:
parent
c48498af95
commit
fbf645ba0f
15 changed files with 470 additions and 289 deletions
|
@ -1,7 +1,9 @@
|
|||
import "./loginAndSignUpPage.css";
|
||||
import { useEffect, useState } from "react";
|
||||
import axios from "axios";
|
||||
import api from "../api/axios";
|
||||
import ButtonRotkehlchen from "../components/ButtonRotkehlchen";
|
||||
import { useLocation, useNavigate } from "react-router-dom";
|
||||
import { useAuth } from "../api/Auth";
|
||||
|
||||
type FormData = {
|
||||
username: string;
|
||||
|
@ -15,6 +17,14 @@ function LoginAndSignUpPage({ signupProp }: { signupProp: boolean }) {
|
|||
error: String;
|
||||
details: { message: string }[];
|
||||
}>();
|
||||
|
||||
const location = useLocation();
|
||||
const navigate = useNavigate();
|
||||
|
||||
const { setUserState } = useAuth();
|
||||
const returnTo =
|
||||
new URLSearchParams(location.search).get("returnTo") || "/feed";
|
||||
|
||||
useEffect(() => {
|
||||
setSignup(signupProp);
|
||||
}, [signupProp]);
|
||||
|
@ -34,24 +44,28 @@ function LoginAndSignUpPage({ signupProp }: { signupProp: boolean }) {
|
|||
setErrorMessages(undefined);
|
||||
try {
|
||||
const response = signup
|
||||
? await axios.post("http://localhost:3001/api/user/register", {
|
||||
? await api.post("http://localhost:3001/api/user/register", {
|
||||
email: formData.email,
|
||||
username: formData.username,
|
||||
password: formData.password,
|
||||
})
|
||||
: await axios.post("http://localhost:3001/api/user/login", {
|
||||
: await api.post("http://localhost:3001/api/user/login", {
|
||||
username: formData.username,
|
||||
password: formData.password,
|
||||
});
|
||||
const authHeader = response.headers["authorization"];
|
||||
if (authHeader && authHeader.startsWith("Bearer ")) {
|
||||
const token = authHeader.substring(7);
|
||||
console.log(token, "Hello");
|
||||
localStorage.setItem("token", token);
|
||||
}
|
||||
const refreshToken = response.headers["refresh-token"];
|
||||
if (refreshToken) {
|
||||
localStorage.setItem("refreshToken", refreshToken);
|
||||
}
|
||||
await setUserState();
|
||||
navigate(returnTo, { replace: true });
|
||||
} catch (err: any) {
|
||||
setErrorMessages(err.response.data);
|
||||
console.error("error:", err.response.data);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue