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) => { ...@@ -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" }) 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 }) 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 }) 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 === 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" }) if (password !== confirmPassword) return res.status(400).json({ code: "02", message: "Password doesn't match" })
......
...@@ -124,6 +124,24 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => { ...@@ -124,6 +124,24 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => {
window.localStorage.setItem('users', JSON.stringify(users)); 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 registerUser = async (email: string, password: string, firstName: string, lastName: string, contactNumber: string, confirmPassword: string) => {
const response = await axiosServices.post('/rest_node/user/sign-up', { const response = await axiosServices.post('/rest_node/user/sign-up', {
...@@ -177,7 +195,7 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => { ...@@ -177,7 +195,7 @@ export const JWTProvider = ({ children }: { children: React.ReactElement }) => {
return <Loader />; 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; export default JWTContext;
...@@ -8,34 +8,34 @@ import { ...@@ -8,34 +8,34 @@ import {
FormControlLabel, FormControlLabel,
FormHelperText, FormHelperText,
Grid, Grid,
Link,
InputAdornment, InputAdornment,
InputLabel, InputLabel,
Link,
OutlinedInput, OutlinedInput,
Stack, Stack,
Typography Typography
} from '@mui/material'; } from '@mui/material';
// third party // third party
import * as Yup from 'yup';
import { Formik } from 'formik'; import { Formik } from 'formik';
import * as Yup from 'yup';
// project import // project import
import IconButton from 'components/@extended/IconButton';
import AnimateButton from 'components/@extended/AnimateButton'; import AnimateButton from 'components/@extended/AnimateButton';
import IconButton from 'components/@extended/IconButton';
import useAuth from 'hooks/useAuth'; import useAuth from 'hooks/useAuth';
import useScriptRef from 'hooks/useScriptRef'; import useScriptRef from 'hooks/useScriptRef';
// assets // assets
import { EyeOutlined, EyeInvisibleOutlined } from '@ant-design/icons'; import { EyeInvisibleOutlined, EyeOutlined } from '@ant-design/icons';
// ============================|| JWT - LOGIN ||============================ // // ============================|| JWT - LOGIN ||============================ //
const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => { const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => {
const [checked, setChecked] = React.useState(false); const [checked, setChecked] = React.useState(false);
const { login } = useAuth(); const { loginUser } = useAuth();
const scriptedRef = useScriptRef(); const scriptedRef = useScriptRef();
const [showPassword, setShowPassword] = React.useState(false); const [showPassword, setShowPassword] = React.useState(false);
...@@ -61,7 +61,7 @@ const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => { ...@@ -61,7 +61,7 @@ const AuthLogin = ({ isDemo = false }: { isDemo?: boolean }) => {
})} })}
onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => { onSubmit={async (values, { setErrors, setStatus, setSubmitting }) => {
try { try {
await login(values.email, values.password); await loginUser(values.email, values.password);
if (scriptedRef.current) { if (scriptedRef.current) {
setStatus({ success: true }); setStatus({ success: true });
setSubmitting(false); setSubmitting(false);
......
...@@ -74,6 +74,7 @@ export type JWTContextType = { ...@@ -74,6 +74,7 @@ export type JWTContextType = {
user?: UserProfile | null | undefined; user?: UserProfile | null | undefined;
logout: () => void; logout: () => void;
login: (email: string, password: string) => Promise<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>; 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>; registerUser: (email: string, password: string, firstName: string, lastName: string, contactNumber: string, confirmPassword: string) => Promise<void>;
resetPassword: (email: 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