diff --git a/src/api/user.ts b/src/api/user.ts index 67a0cbe..71ccf84 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -1,6 +1,23 @@ import axios from "../axiosInterceptor"; +// GET + export const getUserToken = async () => { const response = await axios.post("/user-service/get-token"); response.data; }; + +export const getUserAPI = async () => { + const response = await axios.get("/user-service/user"); + return response.data; +}; + +// POST + +export const loginAPI = async (email: string, password: string) => { + const response = await axios.post("/user-service/login", { + email, + password, + }); + response.data; +}; diff --git a/src/components/LoginPage/LoginPage.jsx b/src/components/LoginPage/LoginPage.jsx index 7c1969f..f9807de 100755 --- a/src/components/LoginPage/LoginPage.jsx +++ b/src/components/LoginPage/LoginPage.jsx @@ -1,236 +1,162 @@ import SpinnerLogin from "../SpinnerLogin"; -import React from "react"; +import React, { useEffect, useState } from "react"; import env from "../../enviroment/envFrontEnd"; +import { getUserAPI } from "../../api/user"; +import { useLocation, useNavigate } from "react-router-dom"; +import { setUser } from "../../reducers/user"; +import { useAppDispatch } from "../../hooks/store"; const LoginPage = (props) => { + const [email, setEmail] = useState(""); + const [password, setPassword] = useState(""); + const [verifyPassword, setVerifyPassword] = useState(""); + const [attemptingLogin, setAttemptingLogin] = useState(false); + const dispatch = useAppDispatch(); + const location = useLocation(); + const navigate = useNavigate(); + + const attemptLoginWithToken = async () => { + setAttemptingLogin(true); + console.log("login with token"); + + try { + const userResponse = await getUserAPI(); + console.log("user response", userResponse); + if (userResponse.emailVerified) { + // TODO: Fix this + } + + const redirectPath = location.state?.from?.pathname || "/home"; + console.log("redirect path", redirectPath); + dispatch(setUser(userResponse)); + navigate(redirectPath); + setAttemptingLogin(false); + window.localStorage.setItem("hasPreviouslyLoggedIn", "true"); + } catch (e) { + console.log("Login Error", e); + setAttemptingLogin(false); + } + }; + + const onSubmit = () => {}; + + useEffect(() => { + attemptLoginWithToken(); + }, []); + + if (attemptingLogin) { + return ( +
+
+
+ +
+
+
+ ); + } + return (
- {!props.loginFailed ? ( -
-
-
- +
+
+
+
+ logo +
+ + {/* */} +
+

+ {props.state.loginMode + ? "Login to your account" + : "Create an account"} +

+
+
+ setEmail(e.target.value)} + value={email} + /> +
+ {!props.state.resetPasswordMode ? ( +
+ setPassword(e.target.value)} + value={password} + /> + {props.state.loginMode ? ( + Forgot? + ) : undefined} +
+ ) : undefined} + {props.state.loginMode ? undefined : ( +
+ setVerifyPassword(e.target.value)} + value={verifyPassword} + /> +
+ )} +
+ +
+ + {!props.state.resetPasswordMode ? ( +
+ {props.state.loginMode ? ( +

+ Don't have an account?{" "} + Create account +

+ ) : ( +

+ Back to Login +

+ )} +
+ ) : ( +
+

+ Back to{" "} + Login +

+
+ )} + + {props.loginFailed && ( +
+ +

{props.loginFailed}

+
+ )} +
- ) : ( -
- {props.loginFailed && props.loginFailedCode === 404 ? ( -
-
-
-

Confirm your email address

-

- We've sent a confirmation email to{" "} - - {env.emailAddress - ? env.emailAddress.length !== 0 - ? env.emailAddress - : props.state.email - : props.state.email} - - . -

-

Refresh this page after confirming email.

-
- {/*
- -
-
- -
*/} -
-

- - checkbox - {" "} - We’ve resent the email. You can resend again in{" "} - - 0: - - {props.state.verifyEmailResentTimer} - - -

- - Resend confirmation email - - - Logout - -
-
-
-
-
- ) : ( -
-
-
- logo -
- - {/* */} -
-

- {props.state.loginMode - ? "Login to your account" - : "Create an account"} -

-
-
- -
- {!props.state.resetPasswordMode ? ( -
- - {props.state.loginMode ? ( - Forgot? - ) : undefined} -
- ) : undefined} - {props.state.loginMode ? undefined : ( -
- -
- )} -
- -
- - {!props.state.resetPasswordMode ? ( -
- {props.state.loginMode ? ( -

- Don't have an account?{" "} - - Create account - -

- ) : ( -

- Back to Login -

- )} -
- ) : ( -
-

- Back to{" "} - Login -

-
- )} - - {props.loginFailed ? ( - props.loginFailedCode === 404 ? ( -
-
- -

Email Not Verified

-
-
-

- Resend Email Verification -

-

- Logout -

-
-
- ) : ( -
- -

{props.loginFailed}

-
- ) - ) : undefined} -
-
- - {/* */} -
-

Forgot your password?

-

- Type in your email address and we’ll send you instructions - to reset your password. -

-
-
- -
-
- -
-
-
- -
-

Check your email

-

- If the email address matches any in our database, we’ll send - you an email with instructions on how to reset your password -

-
-
-
- )} - - {/* */} - {/*
-

Copyright © 2020 myDrive. All Rights Reserved.

-
*/} -
- )} +
); }; diff --git a/src/components/LoginPage/index.jsx b/src/components/LoginPage/index.jsx index d2d9371..8b12ba4 100644 --- a/src/components/LoginPage/index.jsx +++ b/src/components/LoginPage/index.jsx @@ -225,7 +225,7 @@ class LoginPageContainer extends React.Component { ...this.state, email: "", })); - this.loginWithToken(); + // this.loginWithToken(); }; render() { diff --git a/src/reducers/user.ts b/src/reducers/user.ts new file mode 100644 index 0000000..eb68dcb --- /dev/null +++ b/src/reducers/user.ts @@ -0,0 +1,27 @@ +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { UserType } from "../types/user"; + +interface UserStateType { + user?: null | UserType; + loggedIn: boolean; +} + +const initialState: UserStateType = { + user: null, + loggedIn: false, +}; + +const userSlice = createSlice({ + name: "selected", + initialState, + reducers: { + setUser: (state, action: PayloadAction) => { + state.user = action.payload; + state.loggedIn = true; + }, + }, +}); + +export const { setUser } = userSlice.actions; + +export default userSlice.reducer; diff --git a/src/routers/PrivateRoute.jsx b/src/routers/PrivateRoute.jsx index 07ee47a..6e6ae28 100644 --- a/src/routers/PrivateRoute.jsx +++ b/src/routers/PrivateRoute.jsx @@ -1,9 +1,11 @@ import React from "react"; import { connect, useSelector } from "react-redux"; import { Route, Navigate, useLocation } from "react-router-dom"; +import { useAppSelector } from "../hooks/store"; const PrivateRoute = ({ children }) => { - const isAuthenticated = useSelector((state) => !!state.auth.id); + const isAuthenticated = useAppSelector((state) => state.user.loggedIn); + console.log("isAuthenticated", isAuthenticated); const location = useLocation(); if (!isAuthenticated) { return ; diff --git a/src/store/configureStore.ts b/src/store/configureStore.ts index 7c04b75..06eb5c8 100755 --- a/src/store/configureStore.ts +++ b/src/store/configureStore.ts @@ -20,6 +20,7 @@ import uploadStorageSwitcherReducer from "../reducers/uploadStorageSwitcher"; import mobileContextMenuReducer from "../reducers/mobileContextMenu"; import selectedReducer from "../reducers/selected"; import leftSectionReducer from "../reducers/leftSection"; +import userReducer from "../reducers/user"; //const composeEnchancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; @@ -34,6 +35,7 @@ const store = configureStore({ leftSection: leftSectionReducer, // selectedItem: selectedItemReducer, uploads: uploadsReducer, + user: userReducer, // storage: storageReducer, // quickFiles: quickFilesReducer, // popupFile: popupFilesReducer, diff --git a/src/types/user.ts b/src/types/user.ts new file mode 100644 index 0000000..8737c8d --- /dev/null +++ b/src/types/user.ts @@ -0,0 +1,5 @@ +export interface UserType { + name: string; + email: string; + emailVerified?: boolean; +}