Commit 22c19952 authored by janithGamage's avatar janithGamage

feat: update

desc : update project with
 > Login UI update
 > Login API Connect
 > Login Logic implement
parent 0f5f6b65
......@@ -44,11 +44,11 @@ export const signUp = async (req, res) => {
if (contactNumber === null || typeof contactNumber == "undefined") return res.status(400).json({ code: "02", message: "Contact Number Field Required" })
const existingUserByEmail = await User.findOne({ email: email })
if (existingUserByEmail) return res.status(400).json({ code: "02", message: "User already exist" })
if (existingUserByEmail) return res.status(400).json({ code: "02", message: `For the Email : ${email} User already exist` })
const existingUserByContactNo = await User.findOne({ contactNumber: contactNumber })
if (existingUserByContactNo) return res.status(400).json({ code: "02", message: "User already exist" })
if (existingUserByContactNo) return res.status(400).json({ code: "02", message: `For the Contact No : ${contactNumber} User already exist` })
if (password === null || typeof password == "undefined") return res.status(400).json({ code: "02", message: "Password Field Required" })
if (password !== confirmPassword) return res.status(400).json({ code: "02", message: "Password doesn't match" })
......
......@@ -124,8 +124,26 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => {
window.localStorage.setItem('users', JSON.stringify(users));
};
const loginUser = async (email: string, password: string) => {
const response = await axiosServices.post('/rest_node/user/sign-in', { email, password });
const { token, result } = response.data;
setSession(token);
dispatch({
type: LOGIN,
payload: {
isLoggedIn: true,
user: {
id: result._id,
email: result.email,
name: `${result.firstName} ${result.lastName}`,
role: result.type
}
}
});
};
const registerUser = async (email: string, password: string, firstName: string, lastName: string, contactNumber: string, confirmPassword: string) => {
const response = await axiosServices.post('/rest_node/user/sign-up', {
firstName,
lastName,
......@@ -134,8 +152,8 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => {
password,
confirmPassword,
type: "member"
});
});
const user = response.data.result;
if (window.localStorage.getItem('users') !== undefined && window.localStorage.getItem('users') !== null) {
......@@ -177,7 +195,7 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => {
return <Loader />;
}
return <JWTContext.Provider value={{ ...state, login, logout, register, registerUser, resetPassword, updateProfile }}>{children}</JWTContext.Provider>;
return <JWTContext.Provider value={{ ...state, login, logout, loginUser, register, registerUser, resetPassword, updateProfile }}>{children}</JWTContext.Provider>;
};
export default JWTContext;
......@@ -8,34 +8,34 @@ import {
FormControlLabel,
FormHelperText,
Grid,
Link,
InputAdornment,
InputLabel,
Link,
OutlinedInput,
Stack,
Typography
} from '@mui/material';
// third party
import * as Yup from 'yup';
import { Formik } from 'formik';
import * as Yup from 'yup';
// project import
import IconButton from 'components/@extended/IconButton';
import AnimateButton from 'components/@extended/AnimateButton';
import IconButton from 'components/@extended/IconButton';
import useAuth from 'hooks/useAuth';
import useScriptRef from 'hooks/useScriptRef';
// assets
import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons';
import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons';
// ============================|| JWT - LOGIN ||============================ //
const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => {
const [checked, setChecked] = React.useState(false);
const { login } = useAuth();
const { loginUser } = useAuth();
const scriptedRef = useScriptRef();
const [showPassword, setShowPassword] = React.useState(false);
......@@ -61,7 +61,7 @@ const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => {
})}
onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => {
try {
await login(values.email, values.password);
await loginUser(values.email, values.password);
if (scriptedRef.current) {
setStatus({ success: true });
setSubmitting(false);
......
......@@ -74,6 +74,7 @@ export type JWTContextType = {
user?: UserProfile | null | undefined;
logout: () => void;
login: (email: string, password: string) => Promise<void>;
loginUser: (email: string, password: string) => Promise<void>;
register: (email: string, password: string, firstName: string, lastName: string) => Promise<void>;
registerUser: (email: string, password: string, firstName: string, lastName: string, contactNumber: string, confirmPassword: string) => Promise<void>;
resetPassword: (email: string) => Promise<void>;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment