Merge pull request #118 from bluewave-labs/feat/connect-login

Feat/connect login, resolves #108
This commit is contained in:
Veysel authored and GitHub committed 2024-06-10 18:01:21 -04:00
commit 92c9df9650
6 files changed
+143 -44

No files matched your search

+14 -22
View File
@@ -1014,6 +1014,7 @@
"version": "0.11.14",
"resolved": "https://registry.npmjs.org/@humanwhocodes/config-array/-/config-array-0.11.14.tgz",
"integrity": "sha512-3T8LkOmg45BV5FICb15QQMsyUSWrQ8AygVfC7ZG32zOalnqrilm018ZVCw0eapXux8FtA33q8PSRSstjee3jSg==",
"deprecated": "Use @eslint/config-array instead",
"dev": true,
"dependencies": {
"@humanwhocodes/object-schema": "^2.0.2",
@@ -1335,6 +1336,7 @@
"version": "7.6.2",
"resolved": "https://registry.npmjs.org/@mui/x-charts/-/x-charts-7.6.2.tgz",
"integrity": "sha512-oG22NRno1+HSLV/9jVLThnHAKN4g+MXOO6GqaQxN9LM0hjt1tgRsrNAlfcJndmj/dVwqFtynkFB5qWnTEBZs7Q==",
"license": "MIT",
"dependencies": {
"@babel/runtime": "^7.24.6",
"@mui/base": "^5.0.0-beta.40",
@@ -1925,9 +1927,9 @@
"dev": true
},
"node_modules/@vitejs/plugin-react": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.3.0.tgz",
"integrity": "sha512-KcEbMsn4Dpk+LIbHMj7gDPRKaTMStxxWRkRmxsg/jVdFdJCZWt1SchZcf0M4t8lIKdwwMsEyzhrcOXRrDPtOBw==",
"version": "4.3.1",
"resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-4.3.1.tgz",
"integrity": "sha512-m/V2syj5CuVnaxcUJOQRel/Wr31FFXRFlnOoq1TVtkCxsY5veGMTEmpWHndrhB2U8ScHtCQB1e+4hWYExQc6Lg==",
"dev": true,
"dependencies": {
"@babel/core": "^7.24.5",
@@ -2268,15 +2270,9 @@
}
},
"node_modules/caniuse-lite": {
<<<<<<< HEAD
"version": "1.0.30001628",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001628.tgz",
"integrity": "sha512-S3BnR4Kh26TBxbi5t5kpbcUlLJb9lhtDXISDPwOfI+JoC+ik0QksvkZtUVyikw3hjnkgkMPSJ8oIM9yMm9vflA==",
=======
"version": "1.0.30001629",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001629.tgz",
"integrity": "sha512-c3dl911slnQhmxUIT4HhYzT7wnBK/XYpGnYLOj4nJBaRiw52Ibe7YxlDaAeRECvA786zCuExhxIUJ2K7nHMrBw==",
>>>>>>> 3f41db916efdd46069589e91f5886a4a01d11896
"version": "1.0.30001632",
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001632.tgz",
"integrity": "sha512-udx3o7yHJfUxMLkGohMlVHCvFvWmirKh9JAH/d7WOLPetlH+LTL5cocMZ0t7oZx/mdlOWXti97xLZWc8uURRHg==",
"dev": true,
"funding": [
{
@@ -2646,16 +2642,12 @@
}
},
"node_modules/electron-to-chromium": {
<<<<<<< HEAD
"version": "1.4.790",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.790.tgz",
"integrity": "sha512-eVGeQxpaBYbomDBa/Mehrs28MdvCXfJmEFzaMFsv8jH/MJDLIylJN81eTJ5kvx7B7p18OiPK0BkC06lydEy63A==",
=======
"version": "1.4.792",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.792.tgz",
"integrity": "sha512-rkg5/N3L+Y844JyfgPUyuKK0Hk0efo3JNxUDKvz3HgP6EmN4rNGhr2D8boLsfTV/hGo7ZGAL8djw+jlg99zQyA==",
>>>>>>> 3f41db916efdd46069589e91f5886a4a01d11896
"dev": true
"version": "1.4.796",
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.796.tgz",
"integrity": "sha512-NglN/xprcM+SHD2XCli4oC6bWe6kHoytcyLKCWXmRL854F0qhPhaYgUswUsglnPxYaNQIg2uMY4BvaomIf3kLA==",
"dev": true,
"license": "ISC"
},
"node_modules/error-ex": {
"version": "1.3.2",
+33 -12
View File
@@ -5,6 +5,7 @@ const BASE_URL = import.meta.env.VITE_APP_API_BASE_URL;
const initialState = {
isLoading: false,
authToken: "",
user: "",
success: null,
msg: null,
@@ -22,28 +23,48 @@ export const register = createAsyncThunk(
}
);
export const login = createAsyncThunk("auth/login", async (form, thunkApi) => {
try {
const res = await axios.post(`${BASE_URL}/auth/login`, form);
return res.data;
} catch (error) {
return thunkApi.rejectWithValue(error.response.data);
}
});
const handleAuthFulfilled = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
state.authToken = action.payload.data;
const decodedToken = jwtDecode(action.payload.data);
const user = { ...decodedToken };
state.user = user;
};
const handleAuthRejected = (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
};
const authSlice = createSlice({
name: "auth",
initialState,
reducers: {},
extraReducers: (builder) => {
builder
// Register thunk
.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.fulfilled, handleAuthFulfilled)
.addCase(register.rejected, handleAuthRejected)
// Login thunk
.addCase(login.pending, (state, action) => {
state.isLoading = true;
})
.addCase(register.rejected, (state, action) => {
state.isLoading = false;
state.success = action.payload.success;
state.msg = action.payload.msg;
});
.addCase(login.fulfilled, handleAuthFulfilled)
.addCase(login.rejected, handleAuthRejected);
},
});
-1
View File
@@ -10,7 +10,6 @@ import { useSelector } from "react-redux";
const HomeLayout = () => {
const authState = useSelector((state) => state.auth);
const { user, msg } = authState;
console.log(user, msg);
return (
<div className="home-layout">
<DashboardSidebar />
+78 -8
View File
@@ -1,4 +1,6 @@
import { useState } from "react";
import { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import "./index.css";
import BackgroundPattern from "../../Components/BackgroundPattern/BackgroundPattern";
import Logomark from "../../assets/Images/Logomark.png";
@@ -8,23 +10,84 @@ import Button from "../../Components/Button";
import Google from "../../assets/Images/Google.png";
import PasswordTextField from "../../Components/TextFields/Password/PasswordTextField";
import { loginValidation } from "../../Validation/validation";
import { login } from "../../Features/Auth/authSlice";
import { useDispatch } from "react-redux";
const Login = () => {
const dispatch = useDispatch();
const navigate = useNavigate();
const idMap = {
"login-email-input": "email",
"login-password-input": "password",
};
const [errors, setErrors] = useState({});
const [form, setForm] = useState({
email: "",
password: "",
});
useEffect(() => {
const { error } = loginValidation.validate(form, {
abortEarly: false,
});
if (error) {
// Creates an error object in the format { field: message }
const validationErrors = error.details.reduce((acc, err) => {
return { ...acc, [err.path[0]]: err.message };
}, {});
setErrors(validationErrors);
} else {
setErrors({});
}
}, []);
const handleSubmit = async () => {
try {
await loginValidation.validateAsync(form, { abortEarly: false });
const action = await dispatch(login(form));
if (action.meta.requestStatus === "fulfilled") {
const token = action.payload.data;
localStorage.setItem("token", token);
navigate("/");
}
if (action.meta.requestStatus === "rejected") {
const error = new Error("Request rejected");
error.response = action.payload;
throw error;
}
} catch (error) {
if (error.name === "ValidationError") {
// TODO Handle validation errors
console.log(error.details);
alert(error);
} else if (error.response) {
// TODO handle dispatch errors
alert(error.response.msg);
} else {
// TODO handle unknown errors
console.log(error);
alert("Unknown error");
}
}
};
const handleInput = (e) => {
const fieldName = idMap[e.target.id];
setForm({
...form,
[fieldName]: e.target.value,
});
// Extract and validate individual fields as input changes
const fieldSchema = loginValidation.extract(fieldName);
const { error } = fieldSchema.validate(e.target.value);
let errMsg = "";
if (error) {
errMsg = error.message;
}
setErrors({ ...errors, [fieldName]: errMsg });
const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
setForm(newForm);
};
return (
@@ -44,14 +107,16 @@ const Login = () => {
<div className="login-form-inputs">
<EmailTextField
onChange={handleInput}
error={false}
error={errors.email ? true : false}
helperText={errors.email ? errors.email : ""}
placeholder="Enter your email"
id="login-email-input"
/>
<div className="login-form-v2-spacing" />
<PasswordTextField
onChange={handleInput}
error={false}
error={errors.password ? true : false}
helperText={errors.password ? errors.password : ""}
placeholder="Password"
id="login-password-input"
/>
@@ -63,7 +128,12 @@ const Login = () => {
</div>
<div className="login-form-v3-spacing" />
<div className="login-form-actions">
<Button level="primary" label="Sign in" sx={{ width: "100%" }} />
<Button
level="primary"
label="Sign in"
sx={{ width: "100%" }}
onClick={handleSubmit}
/>
<div className="login-form-v-spacing" />
<Button
level="secondary"
+1
View File
@@ -88,6 +88,7 @@ const Register = () => {
} catch (error) {
if (error.name === "ValidationError") {
// TODO Handle validation errors
console.log(error);
alert(error);
} else if (error.response) {
// TODO handle dispatch errors
+17 -1
View File
@@ -24,4 +24,20 @@ const registerValidation = joi.object({
}),
});
export { registerValidation };
const loginValidation = joi.object({
email: joi
.string()
.email({ tlds: { allow: false } })
.required()
.messages({
"string.email": "Email must be a valid email",
"string.empty": "Email is required",
}),
password: joi.string().min(8).required().messages({
"string.min": "Password must be at least 8 characters",
"string.empty": "Password is required",
}),
});
export { registerValidation, loginValidation };