mirror of
https://github.com/lemeow125/Ivy-Frontend.git
synced 2024-11-17 06:39:25 +08:00
Allow querying for saved login session
This commit is contained in:
parent
1f668be499
commit
f411ea00a4
11 changed files with 107 additions and 32 deletions
10
src/App.tsx
10
src/App.tsx
|
@ -1,4 +1,4 @@
|
||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
import Dashboard from "./Routes/Dashboard/Dashboard";
|
import Dashboard from "./Routes/Dashboard/Dashboard";
|
||||||
import Error from "./Routes/Error/Error";
|
import Error from "./Routes/Error/Error";
|
||||||
import Products from "./Routes/Products/Products";
|
import Products from "./Routes/Products/Products";
|
||||||
|
@ -60,10 +60,8 @@ const router = createBrowserRouter([
|
||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
return (
|
return (
|
||||||
<React.StrictMode>
|
<Provider store={Store}>
|
||||||
<Provider store={Store}>
|
<RouterProvider router={router} />
|
||||||
<RouterProvider router={router} />
|
</Provider>
|
||||||
</Provider>
|
|
||||||
</React.StrictMode>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,9 @@ import {
|
||||||
LoginParams,
|
LoginParams,
|
||||||
RegistrationParams,
|
RegistrationParams,
|
||||||
} from "../../Interfaces/Interfaces";
|
} from "../../Interfaces/Interfaces";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
|
||||||
|
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
|
|
||||||
// Note APIs
|
// Note APIs
|
||||||
|
|
||||||
|
@ -47,7 +50,7 @@ export function UpdateProduct(note: UpdateProductParams) {
|
||||||
return response.data;
|
return response.data;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error updating product", error);
|
console.log("Error updating product", error.response);
|
||||||
return error;
|
return error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -64,7 +67,7 @@ export function AddProduct(note: AddProductParams) {
|
||||||
return response.data;
|
return response.data;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error adding product", error);
|
console.log("Error adding product", error.response);
|
||||||
return error;
|
return error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -78,7 +81,7 @@ export function DeleteProduct(id: number) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Error deleting product", error);
|
console.log("Error deleting product", error.response);
|
||||||
return error;
|
return error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -93,7 +96,7 @@ export function UserRegister(register: RegistrationParams) {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Registration failed: " + error);
|
console.log("Registration failed: " + error.response);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -110,7 +113,7 @@ export function UserLogin(user: LoginParams) {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Login Failed: " + error);
|
console.log("Login Failed: " + error.response);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -126,6 +129,10 @@ export function UserInfo() {
|
||||||
.then((response) => {
|
.then((response) => {
|
||||||
console.log(response.data);
|
console.log(response.data);
|
||||||
return response.data;
|
return response.data;
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
console.log("Error retrieving user data", error.response);
|
||||||
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -137,7 +144,22 @@ export function UserActivate(activation: ActivationParams) {
|
||||||
return true;
|
return true;
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((error) => {
|
||||||
console.log("Activation failed: " + error);
|
console.log("Activation failed: " + error.response);
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function CheckSavedSession() {
|
||||||
|
console.log("Checking for saved session by querying user data");
|
||||||
|
if (JSON.parse(localStorage.getItem("token") || "{}")) {
|
||||||
|
if (await UserInfo()) {
|
||||||
|
console.log("Previous session found");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
console.log("Previous session found but expired");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
console.log("No previous session found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
|
@ -1,13 +1,29 @@
|
||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
import Sidebar from "../Sidebar/Sidebar";
|
import Sidebar from "../Sidebar/Sidebar";
|
||||||
import Header from "../Header/Header";
|
import Header from "../Header/Header";
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
|
import { CheckSavedSession, UserInfo } from "../Api/Api";
|
||||||
|
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
|
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
|
||||||
export interface props {
|
export interface props {
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Container(props: props) {
|
export default function Container(props: props) {
|
||||||
|
const dispatch = useDispatch();
|
||||||
|
// Function to check for previous login session
|
||||||
|
async function CheckPreviousSession() {
|
||||||
|
if (await CheckSavedSession()) {
|
||||||
|
await dispatch(toggle_login());
|
||||||
|
await dispatch(SetUser(await UserInfo()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
useEffect(() => {
|
||||||
|
CheckPreviousSession();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Header />
|
<Header />
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { useSelector, useDispatch } from "react-redux";
|
import { useSelector, useDispatch } from "react-redux";
|
||||||
import { toggle_login } from "../../Features/Login/LoginSlice";
|
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { LoggedInUserState } from "../../Interfaces/Interfaces";
|
||||||
|
|
||||||
export interface state {
|
export interface state {
|
||||||
logged_in: {
|
logged_in: {
|
||||||
|
@ -11,6 +12,9 @@ export interface state {
|
||||||
}
|
}
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const logged_in = useSelector((state: state) => state.logged_in.value);
|
const logged_in = useSelector((state: state) => state.logged_in.value);
|
||||||
|
const logged_in_user = useSelector(
|
||||||
|
(state: LoggedInUserState) => state.logged_in_user.value
|
||||||
|
);
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
async function login() {
|
async function login() {
|
||||||
|
@ -20,7 +24,9 @@ export default function Login() {
|
||||||
|
|
||||||
if (logged_in) {
|
if (logged_in) {
|
||||||
return (
|
return (
|
||||||
<p style={{ ...styles.text_white, ...styles.text_M }}>Welcome Jophiel</p>
|
<p style={{ ...styles.text_white, ...styles.text_M }}>
|
||||||
|
Logged in as {logged_in_user.username}
|
||||||
|
</p>
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import { toggle_login } from "../../Features/Login/LoginSlice";
|
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
import { Button } from "@mui/material";
|
import { Button } from "@mui/material";
|
||||||
import styles from "../../styles";
|
import styles from "../../styles";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
|
|
||||||
|
export const LoggedInUserSlice = createSlice({
|
||||||
|
name: "Login",
|
||||||
|
initialState: {
|
||||||
|
value: {
|
||||||
|
email: "",
|
||||||
|
id: 0,
|
||||||
|
username: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
reducers: {
|
||||||
|
SetUser: (state, action) => {
|
||||||
|
state.value = {
|
||||||
|
email: action.payload.email,
|
||||||
|
id: action.payload.id,
|
||||||
|
username: action.payload.username,
|
||||||
|
};
|
||||||
|
},
|
||||||
|
UnsetUser: (state) => {
|
||||||
|
state.value = {
|
||||||
|
email: "",
|
||||||
|
id: 0,
|
||||||
|
username: "",
|
||||||
|
};
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Action creators are generated for each case reducer function
|
||||||
|
export const { SetUser, UnsetUser } = LoggedInUserSlice.actions;
|
||||||
|
|
||||||
|
export default LoggedInUserSlice.reducer;
|
|
@ -14,7 +14,7 @@ export interface LoginState {
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface LoggedInUserState {
|
export interface LoggedInUserState {
|
||||||
LoggedInUser: {
|
logged_in_user: {
|
||||||
value: {
|
value: {
|
||||||
email: string;
|
email: string;
|
||||||
id: number;
|
id: number;
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import { configureStore } from "@reduxjs/toolkit";
|
import { configureStore } from "@reduxjs/toolkit";
|
||||||
import LoginReducer from "../../../Features/Login/LoginSlice";
|
import LoginReducer from "../../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
|
import LoggedInUserReducer from "../../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
|
||||||
|
|
||||||
export default configureStore({
|
export default configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
logged_in: LoginReducer,
|
logged_in: LoginReducer,
|
||||||
|
logged_in_user: LoggedInUserReducer,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,8 +6,9 @@ import { useDispatch } from "react-redux";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
|
|
||||||
import { UserLogin } from "../../Components/Api/Api";
|
import { UserInfo, UserLogin } from "../../Components/Api/Api";
|
||||||
import { toggle_login } from "../../Features/Login/LoginSlice";
|
import { toggle_login } from "../../Features/Redux/Slices/Login/LoginSlice";
|
||||||
|
import { SetUser } from "../../Features/Redux/Slices/LoggedInUserSlice/LoggedInUserSlice";
|
||||||
|
|
||||||
export default function Login() {
|
export default function Login() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
@ -68,7 +69,7 @@ export default function Login() {
|
||||||
});
|
});
|
||||||
if (await UserLogin(user)) {
|
if (await UserLogin(user)) {
|
||||||
await dispatch(toggle_login());
|
await dispatch(toggle_login());
|
||||||
// await dispatch(SetUser(await UserInfo()));
|
await dispatch(SetUser(await UserInfo()));
|
||||||
navigate("/");
|
navigate("/");
|
||||||
} else {
|
} else {
|
||||||
setError("Invalid Login");
|
setError("Invalid Login");
|
||||||
|
@ -89,6 +90,7 @@ export default function Login() {
|
||||||
>
|
>
|
||||||
Register
|
Register
|
||||||
</Button>
|
</Button>
|
||||||
|
<p style={{ ...styles.text_red, ...styles.text_M }}>{error}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,17 +1,13 @@
|
||||||
import React from 'react';
|
import React from "react";
|
||||||
import ReactDOM from 'react-dom/client';
|
import ReactDOM from "react-dom/client";
|
||||||
import './index.css';
|
import "./index.css";
|
||||||
import App from './App';
|
import App from "./App";
|
||||||
import reportWebVitals from './reportWebVitals';
|
import reportWebVitals from "./reportWebVitals";
|
||||||
|
|
||||||
const root = ReactDOM.createRoot(
|
const root = ReactDOM.createRoot(
|
||||||
document.getElementById('root') as HTMLElement
|
document.getElementById("root") as HTMLElement
|
||||||
);
|
|
||||||
root.render(
|
|
||||||
<React.StrictMode>
|
|
||||||
<App />
|
|
||||||
</React.StrictMode>
|
|
||||||
);
|
);
|
||||||
|
root.render(<App />);
|
||||||
|
|
||||||
// If you want to start measuring performance in your app, pass a function
|
// If you want to start measuring performance in your app, pass a function
|
||||||
// to log results (for example: reportWebVitals(console.log))
|
// to log results (for example: reportWebVitals(console.log))
|
||||||
|
|
Loading…
Reference in a new issue