diff --git a/frontend/src/components/welcome_page/LoginVia42.js b/frontend/src/components/welcome_page/LoginVia42.js
index 94b9833b8..edd10eb2c 100644
--- a/frontend/src/components/welcome_page/LoginVia42.js
+++ b/frontend/src/components/welcome_page/LoginVia42.js
@@ -9,11 +9,13 @@ const OAuth = async ({ navigate, redirect_uri }) => {
window.onload = () => {
const urlParams = new URLSearchParams(window.location.search);
const token = urlParams.get("token");
- Cookies.set("access", token, {
- expires: 7,
- sameSite: "Strict",
- secure: true,
- });
+ if (token) {
+ Cookies.set("access", token, {
+ expires: 7,
+ sameSite: "None",
+ secure: true,
+ });
+ }
};
const LoginButton = ({ t, navigate, redirect_uri }) => {
diff --git a/frontend/src/components/welcome_page/RegisterButton.js b/frontend/src/components/welcome_page/RegisterButton.js
index 5bd92aa0a..2bc6cfbca 100644
--- a/frontend/src/components/welcome_page/RegisterButton.js
+++ b/frontend/src/components/welcome_page/RegisterButton.js
@@ -24,12 +24,14 @@ const RegisterButt = ({ t, redirectToHome, redirect_uri }) => {
{ withCredentials: true }
);
const token = response.data.access;
- Cookies.set("access", token, {
- expires: 7,
- sameSite: "Strict",
- secure: true,
- });
- if (response.data.access) redirectToHome();
+ if (token) {
+ Cookies.set("access", token, {
+ expires: 7,
+ sameSite: "Strict",
+ secure: true,
+ });
+ redirectToHome();
+ }
} catch (error) {
if (error.response && error.response.data) {
let errorMessage;
diff --git a/frontend/src/components/welcome_page/SignInButton.js b/frontend/src/components/welcome_page/SignInButton.js
index d84149d93..989246070 100644
--- a/frontend/src/components/welcome_page/SignInButton.js
+++ b/frontend/src/components/welcome_page/SignInButton.js
@@ -2,6 +2,7 @@ import React, { useState } from "react";
import axios from "axios";
import { WelcomeButtonStyle } from "../buttons/ButtonStyle";
import Cookies from "js-cookie";
+import { getUserDetails } from "../API";
const SignInButt = ({ t, redirectToHome, redirect_uri }) => {
const [showFields, setShowFields] = useState(false);
@@ -21,12 +22,19 @@ const SignInButt = ({ t, redirectToHome, redirect_uri }) => {
{ withCredentials: true }
);
const token = response.data.access;
- Cookies.set("access", token, {
- expires: 7,
- sameSite: "Strict",
- secure: true,
- });
- if (response.data.access) redirectToHome();
+ if (token) {
+ Cookies.set("access", token, {
+ expires: 7,
+ sameSite: "Strict",
+ secure: true,
+ });
+ const user = await getUserDetails({ redirectUri: redirect_uri });
+ if (user.data.user.TwoFA === true) {
+ window.location.href = "/2fa";
+ } else {
+ redirectToHome();
+ }
+ }
} catch (error) {
if (error.response && error.response.data) {
let errorMessage;
diff --git a/frontend/src/pages/Profile.js b/frontend/src/pages/Profile.js
index 23c6c502f..d66236332 100644
--- a/frontend/src/pages/Profile.js
+++ b/frontend/src/pages/Profile.js
@@ -4,7 +4,7 @@ import React, { useState, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { ButtonStyle } from "../components/buttons/ButtonStyle";
import UserSettings from "../components/profile/UserSettings";
-import axios from "axios";
+import { fetchUserDetails, changeUsername, changeAbout } from "../components/API";
import { CiEdit } from "react-icons/ci";
import Cookies from "js-cookie";
@@ -234,83 +234,6 @@ const defaultUserDetails = {
"https://raw.githubusercontent.com/zstenger93/Transcendence/master/images/transcendence.webp",
};
-export const getUserDetails = async ({ redirectUri }) => {
- let response = {};
- try {
- const token = Cookies.get('access');
- response = await axios.get(`${redirectUri}/api/profile`, {
- headers: {
- withCredentials: true,
- Authorization: `Bearer ${token}`,
- },
- });
- } catch (error) {
- console.log(error);
- }
- return response;
-};
-
-const changeAbout = async ({ redirectUri, about }) => {
- let response = {};
- try {
- const token = Cookies.get('access');
- response = await axios.post(
- `${redirectUri}/api/updateProfile`,
- { AboutMe: about },
- {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- withCredentials: true,
- }
- );
- } catch (error) {
- console.log(error);
- }
- return response;
-};
-
-const changeUsername = async ({ redirectUri, username }) => {
- let response = {};
- try {
- const token = Cookies.get('access');
- response = await axios.post(
- `${redirectUri}/api/updateProfile`,
- { username: username },
- {
- headers: {
- Authorization: `Bearer ${token}`,
- },
- withCredentials: true,
- }
- );
- } catch (error) {
- console.log(error);
- }
- return response;
-};
-
-const fetchUserDetails = async (
- setUserDetails,
- setUsername,
- setImageUrl,
- redirectUri
-) => {
- const response = await getUserDetails({ redirectUri });
- setUserDetails(response.data.user);
-
- console.log(response.data.user);
-
- setUsername(response.data.user.username);
-
- if (response.data.user.profile_picture) {
- let url = decodeURIComponent(
- response.data.user.profile_picture.replace("/media/", "")
- ).replace(":", ":/");
- setImageUrl(url);
- }
-};
-
function Profile({ redirectUri }) {
const [userDetails, setUserDetails] = useState(null);
const [imageUrl, setImageUrl] = useState(defaultUserDetails.profile_picture);
@@ -319,10 +242,8 @@ function Profile({ redirectUri }) {
);
useEffect(() => {
- const wtf = Cookies.get('access');
- console.log("wtf1: ", wtf);
- if (wtf) {
- console.log("wtf2: ", wtf);
+ const token = Cookies.get('access');
+ if (token) {
fetchUserDetails(setUserDetails, setUsername, setImageUrl, redirectUri);
}
}, [redirectUri]);
@@ -404,6 +325,8 @@ function Profile({ redirectUri }) {
}
};
+ console.log(imageUrl);
+
return (