-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[finishes-187354198] user authentication
- Loading branch information
Mag codes
committed
Jun 14, 2024
1 parent
4a2f100
commit 5d80884
Showing
13 changed files
with
166 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { useEffect } from 'react'; | ||
import { useForm, SubmitHandler } from 'react-hook-form'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import GoogleIcon from './../assets/googleIcon.svg'; | ||
import Button from './Button'; | ||
import Input from './Input'; | ||
import { loginSchema, LoginData } from './../utils/schemas'; | ||
import { useNavigate, useLocation } from 'react-router-dom'; | ||
import { useDispatch } from 'react-redux'; | ||
import { setToken, setUser } from '../redux/reducers/userSlice'; | ||
import { toast } from 'react-toastify'; | ||
import { useLoginUserMutation } from '../services/authenticationAPI'; | ||
|
||
const LoginComponent = () => { | ||
const navigate = useNavigate(); | ||
const location = useLocation(); | ||
const dispatch = useDispatch(); | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
setError, | ||
formState: { errors, isSubmitting }, | ||
} = useForm<LoginData>({ resolver: zodResolver(loginSchema) }); | ||
|
||
const [loginUser, { data, error, isLoading, isSuccess, isError }] = useLoginUserMutation(); | ||
|
||
const onSubmit: SubmitHandler<LoginData> = async (userCredentials) => { | ||
await loginUser(userCredentials); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isError && error) { | ||
setError('root', { | ||
message: 'Invalid email or password', | ||
}); | ||
return; | ||
} | ||
if (isSuccess && data) { | ||
const token = data.token; | ||
|
||
dispatch(setUser(data.user)); | ||
dispatch(setToken(token)); | ||
|
||
toast.success('Login successful'); | ||
const from = location.state?.from?.pathname || '/'; | ||
navigate(from); | ||
} | ||
}, [isError, isSuccess, error, data, navigate, location.state, dispatch]); | ||
|
||
const handleGoogleAuthentication = (e: any) => { | ||
e.preventDefault(); | ||
document.location.assign('https://e-commerce-mavericcks-bn-staging-istf.onrender.com/api/auth/google'); | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className='w-full xl:container flex flex-col-reverse gap-14 md:flex-row px-4 mb-8 items-center md:gap-5 transition-all md:justify-between md:px-10 lg:justify-around mt-5 md:mt-10'> | ||
<div className='w-full md:w-7/12 xl:w-4/12 border-greenColor border-2 rounded-xl p-8'> | ||
<p className='font-bold text-3xl'>Existing Customer?</p> | ||
<p>Sign in to continue</p> | ||
{errors.root && ( | ||
<p className='text-sm bg-redColor text-whiteColor mt-4py-2 rounded-lg px-3'>{errors.root.message}</p> | ||
)} | ||
<form onSubmit={handleSubmit(onSubmit)} className='flex flex-col gap-4 justify-center mt-8'> | ||
<Input | ||
id='loginEmail' | ||
label='Email' | ||
type='email' | ||
placeholder='Enter your email' | ||
{...register('email')} | ||
error={errors.email && errors.email.message} | ||
/> | ||
<Input | ||
id='loginPassword' | ||
label='Password' | ||
type='password' | ||
placeholder='Enter your password' | ||
{...register('password')} | ||
error={errors.password && errors.password.message} | ||
/> | ||
<button | ||
type='submit' | ||
className='p-2 rounded-lg bg-greenColor hover:bg-darkGreen transition-all text-whiteColor font-bold' | ||
> | ||
{isSubmitting || isLoading ? 'Loading...' : 'Sign In'} | ||
</button> | ||
{isSuccess && <p className='text-green-600 text-xs font-normal'>Login successful!</p>} | ||
<span className='self-center font-bold text-grayColor'>or</span> | ||
<button | ||
onClick={handleGoogleAuthentication} | ||
className='p-2 font-bold text-teal-700 border-2 border-greenColor rounded-lg flex flex-row items-center justify-center gap-2 hover:bg-teal-100 transition-all' | ||
> | ||
<img src={GoogleIcon} alt='Google Icon' className='w-5' /> | ||
Continue with Google | ||
</button> | ||
</form> | ||
</div> | ||
<div className='w-full md:w-3/12 mt-0 p-5 border-b-2 md:p-0 md:border-b-0 h-fit flex flex-col gap-3 justify-self-start self-start'> | ||
<p className='font-bold text-3xl'>New Customer?</p> | ||
<p>Create an account here</p> | ||
<Button type='button' text='Register Here' onClick={() => navigate('/register')} /> | ||
</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default LoginComponent; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import './index.css'; | ||
import App from './App'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,15 @@ | ||
import { useNavigate } from "react-router-dom"; | ||
import LoginComponent from '../components/LoginComponent'; | ||
import Navbar from '../components/navbar/Navbar'; | ||
import Footer from '../components/footer/Footer'; | ||
|
||
function Login() { | ||
const navigate = useNavigate(); | ||
|
||
const handleNavigate = () => { | ||
navigate("/"); | ||
}; | ||
|
||
return ( | ||
<div className="w-screen h-screen flex flex-row items-start justify-center bg-slate-900 text-white pt-20"> | ||
<div className="flex flex-col gap-5"> | ||
<h1 className="text-5xl">Login page</h1> | ||
<button className="border rounded-md p-3 hover:text-gray-800 hover:bg-gray-200" onClick={handleNavigate}>Back home</button> | ||
</div> | ||
</div> | ||
); | ||
return ( | ||
<> | ||
<Navbar /> | ||
<LoginComponent /> | ||
<Footer /> | ||
</> | ||
); | ||
} | ||
|
||
export default Login; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { createSlice } from '@reduxjs/toolkit'; | ||
|
||
const initialState = { | ||
user: null, | ||
token: null, | ||
subscription: null, | ||
}; | ||
|
||
const userSlice = createSlice({ | ||
name: 'user', | ||
initialState, | ||
reducers: { | ||
setUser: (state, action) => { | ||
state.user = action.payload; | ||
}, | ||
setToken: (state, action) => { | ||
state.token = action.payload; | ||
}, | ||
setSubscription: (state, action) => { | ||
state.subscription = action.payload; | ||
}, | ||
clearUserData: state => { | ||
state.user = null; | ||
state.token = null; | ||
state.subscription = null; | ||
}, | ||
}, | ||
}); | ||
|
||
export const { setUser, setToken, setSubscription, clearUserData } = userSlice.actions; | ||
export default userSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters