Used redux for auth information
This commit is contained in:
1 parent
43d3cd6d84
commit
de7b6b4c89
5 files changed
+80
-17
No files matched your search
@@ -0,0 +1,50 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
|
||||||
|
import { jwtDecode } from "jwt-decode";
|
||||||
|
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
isLoading: false,
|
||||||
|
user: "",
|
||||||
|
success: null,
|
||||||
|
msg: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
export const register = createAsyncThunk(
|
||||||
|
"auth/register",
|
||||||
|
async (form, thunkApi) => {
|
||||||
|
try {
|
||||||
|
const res = await axios.post(`${BASE_URL}/auth/register`, form);
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
const authSlice = createSlice({
|
||||||
|
name: "auth",
|
||||||
|
initialState,
|
||||||
|
reducers: {},
|
||||||
|
extraReducers: (builder) => {
|
||||||
|
builder
|
||||||
|
.addCase(register.pending, (state, action) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(register.fulfilled, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
const decodedToken = jwtDecode(action.payload.data);
|
||||||
|
const user = { ...decodedToken };
|
||||||
|
state.user = user;
|
||||||
|
})
|
||||||
|
.addCase(register.rejected, (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default authSlice.reducer;
|
||||||
@@ -4,22 +4,23 @@ import DashboardSidebar from "../../Components/DashboardSidebar";
|
|||||||
import "./index.css";
|
import "./index.css";
|
||||||
|
|
||||||
// Demo, remove me
|
// Demo, remove me
|
||||||
import { jwtDecode } from "jwt-decode";
|
import { useSelector } from "react-redux";
|
||||||
// Emd Demo
|
// Emd Demo
|
||||||
|
|
||||||
const HomeLayout = () => {
|
const HomeLayout = () => {
|
||||||
const token = localStorage.getItem("token");
|
const authState = useSelector((state) => state.auth);
|
||||||
const decodedToken = token && jwtDecode(token);
|
const { user, msg } = authState;
|
||||||
|
console.log(user, msg);
|
||||||
return (
|
return (
|
||||||
<div className="home-layout">
|
<div className="home-layout">
|
||||||
<DashboardSidebar />
|
<DashboardSidebar />
|
||||||
<div className="main-content">
|
<div className="main-content">
|
||||||
<NavBar />
|
<NavBar />
|
||||||
{/* Remove me later */}
|
{/* Remove me later */}
|
||||||
{token && <p>You are logged in</p>}
|
{user && <p>{user.firstname}</p>}
|
||||||
{token && <p>First Name: {decodedToken.firstname}</p>}
|
{user && <p>{user.lastname}</p>}
|
||||||
{token && <p>Last Name: {decodedToken.lastname}</p>}
|
{user && <p>{user.email}</p>}
|
||||||
{token && <p>Email: {decodedToken.email}</p>}
|
{msg && <p>Msg: {msg}</p>}
|
||||||
{/* I am a demo */}
|
{/* I am a demo */}
|
||||||
</div>
|
</div>
|
||||||
<Outlet />
|
<Outlet />
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import axios from "axios";
|
|
||||||
import Button from "../../Components/Button/";
|
import Button from "../../Components/Button/";
|
||||||
import Link from "../../Components/Link";
|
import Link from "../../Components/Link";
|
||||||
import {
|
import {
|
||||||
|
|||||||
@@ -13,9 +13,13 @@ import Button from "../../Components/Button";
|
|||||||
import Google from "../../assets/Images/Google.png";
|
import Google from "../../assets/Images/Google.png";
|
||||||
import { registerValidation } from "../../Validation/validation";
|
import { registerValidation } from "../../Validation/validation";
|
||||||
|
|
||||||
|
import { useDispatch } from "react-redux";
|
||||||
|
import { register } from "../../Features/Auth/authSlice";
|
||||||
|
|
||||||
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
|
||||||
|
|
||||||
const Register = () => {
|
const Register = () => {
|
||||||
|
const dispatch = useDispatch();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
// TODO If possible, change the IDs of these fields to match the backend
|
// TODO If possible, change the IDs of these fields to match the backend
|
||||||
@@ -55,23 +59,31 @@ const Register = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = async () => {
|
const handleSubmit = async () => {
|
||||||
console.log("submitting");
|
|
||||||
try {
|
try {
|
||||||
await registerValidation.validateAsync(form, { abortEarly: false });
|
await registerValidation.validateAsync(form, { abortEarly: false });
|
||||||
|
const action = await dispatch(register(form));
|
||||||
|
|
||||||
const result = await axios.post(`${BASE_URL}/auth/register`, form);
|
if (action.meta.requestStatus === "fulfilled") {
|
||||||
const token = result.data.data;
|
const token = action.payload.data;
|
||||||
localStorage.setItem("token", token);
|
localStorage.setItem("token", token);
|
||||||
navigate("/"); // Redirect to home page
|
navigate("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (action.meta.requestStatus === "rejected") {
|
||||||
|
const error = new Error("Request rejected");
|
||||||
|
error.response = action.payload;
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// TODO User friendly error display
|
|
||||||
if (error.name === "ValidationError") {
|
if (error.name === "ValidationError") {
|
||||||
|
// TODO Handle validation errors
|
||||||
console.log(error);
|
console.log(error);
|
||||||
alert("Invalid input");
|
alert("Invalid input");
|
||||||
} else if (error.response) {
|
} else if (error.response) {
|
||||||
console.log(error);
|
// TODO handle dispatch errors
|
||||||
alert(error.response.data.msg);
|
alert(error.response.msg);
|
||||||
} else {
|
} else {
|
||||||
|
// TODO handle unknown errors
|
||||||
console.log(error);
|
console.log(error);
|
||||||
alert("Unknown error");
|
alert("Unknown error");
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-1
@@ -1,6 +1,7 @@
|
|||||||
import { configureStore } from "@reduxjs/toolkit";
|
import { configureStore } from "@reduxjs/toolkit";
|
||||||
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
import monitorsReducer from "./Features/Monitors/monitorsSlice";
|
||||||
|
import authReducer from "./Features/Auth/authSlice";
|
||||||
|
|
||||||
export default configureStore({
|
export default configureStore({
|
||||||
reducer: { monitors: monitorsReducer },
|
reducer: { monitors: monitorsReducer, auth: authReducer },
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user