@@ -13,10 +13,10 @@ Coded by www.creative-tim.com
1313* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
1414*/
1515
16- import { useState } from "react" ;
17-
16+ import { useState , useEffect } from "react" ;
17+ import { RotatingLines } from "react-loader-spinner" ;
1818// react-router-dom components
19- import { Link } from "react-router-dom" ;
19+ import { Link , useNavigate } from "react-router-dom" ;
2020
2121// @mui material components
2222import Switch from "@mui/material/Switch" ;
@@ -29,52 +29,57 @@ import SoftButton from "components/SoftButton";
2929
3030// Authentication layout components
3131import CoverLayout from "layouts/authentication/components/CoverLayout" ;
32+ import GithubSocial from "layouts/authentication/components/Socials/github" ;
33+ import Separator from "layouts/authentication/components/Separator" ;
3234
3335// Images
3436import curved9 from "assets/images/curved-images/curved-6.jpg" ;
3537
3638import AuthApi from "../../../api/auth" ;
37- import { useNavigate } from 'react-router-dom' ;
3839import { useAuth } from "../../../auth-context/auth.context" ;
40+ import { API_SERVER } from "config/constant" ;
3941
4042function SignIn ( ) {
4143 const navigate = useNavigate ( ) ;
4244
4345 const [ rememberMe , setRememberMe ] = useState ( true ) ;
4446 const [ formData , setFormData ] = useState ( { } ) ;
45- const [ error , setError ] = useState ( "" )
47+ const [ error , setError ] = useState ( "" ) ;
48+ const [ isLoading , setIsLoading ] = useState ( false ) ;
4649
4750 const { setUser } = useAuth ( ) ;
4851 const { user } = useAuth ( ) ;
4952
5053 const handleSetRememberMe = ( ) => setRememberMe ( ! rememberMe ) ;
5154
52- const handleFormData = e => {
55+ const handleFormData = ( e ) => {
5356 setFormData ( {
5457 ...formData ,
55- [ e . target . name ] : e . target . value
56- } )
57- }
58+ [ e . target . name ] : e . target . value ,
59+ } ) ;
60+ } ;
5861
59- const submitFormData = e => {
62+ const submitFormData = ( e ) => {
6063 e . preventDefault ( ) ;
61- AuthApi . Login ( formData ) . then ( response => {
62- if ( response . data . success ) {
63- return setProfile ( response ) ;
64- } else {
65- setError ( response . data . msg )
66- }
67- } ) . catch ( error => {
68- if ( error . response ) {
69- return setError ( error . response . data . msg ) ;
70- }
71- return setError ( "There has been an error." ) ;
72- } )
73- }
64+ AuthApi . Login ( formData )
65+ . then ( ( response ) => {
66+ if ( response . data . success ) {
67+ return setProfile ( response ) ;
68+ } else {
69+ setError ( response . data . msg ) ;
70+ }
71+ } )
72+ . catch ( ( error ) => {
73+ if ( error . response ) {
74+ return setError ( error . response . data . msg ) ;
75+ }
76+ return setError ( "There has been an error." ) ;
77+ } ) ;
78+ } ;
7479
7580 const handleRedirect = ( ) => {
7681 return navigate ( "/dashboard" ) ;
77- }
82+ } ;
7883
7984 const setProfile = ( response ) => {
8085 let user = { ...response . data . user } ;
@@ -85,51 +90,102 @@ function SignIn() {
8590 return navigate ( "/dashboard" ) ;
8691 } ;
8792
93+ useEffect ( ( ) => {
94+ const url = window . location . href ;
95+ const hasCode = url . includes ( "?code=" ) ;
96+
97+ // If Github API returns the code parameter
98+ if ( hasCode ) {
99+ const newUrl = url . split ( "?code=" ) ;
100+ window . history . pushState ( { } , null , newUrl [ 0 ] ) ;
101+ setIsLoading ( true ) ;
102+
103+ const requestData = {
104+ code : newUrl [ 1 ] ,
105+ } ;
106+
107+ AuthApi . Authorize ( requestData . code )
108+ . then ( ( { data } ) => {
109+ if ( data . user ) {
110+ setUser ( JSON . stringify ( data . user ) ) ;
111+ localStorage . setItem ( "user" , JSON . stringify ( data . user ) ) ;
112+ handleRedirect ( ) ;
113+ } else {
114+ setError ( "no user returned" ) ;
115+ }
116+ } )
117+ . catch ( ( error ) => {
118+ setError ( error . message ) ;
119+ } )
120+ . finally ( ( ) => setIsLoading ( false ) ) ;
121+ }
122+ } , [ ] ) ;
123+
88124 return (
89125 < CoverLayout
90126 title = "Welcome back"
91- description = "Enter your email and password to sign in"
127+ description = "Login through github or enter your email and password to sign in"
92128 image = { curved9 }
93129 >
94- { user && user . token ? (
95- < div >
96- < h3 style = { { textAlign : "center" } } > You are already signed in.</ h3 >
97- < SoftBox mt = { 4 } mb = { 1 } >
98- < SoftButton variant = "gradient" buttonColor = "info" fullWidth onClick = { handleRedirect } >
99- { `Let's go` }
100- </ SoftButton >
130+ { isLoading ? (
131+ < SoftBox display = "flex" justifyContent = "center" >
132+ < RotatingLines
133+ strokeColor = "black"
134+ strokeWidth = "5"
135+ animationDuration = "0.75"
136+ width = "96"
137+ visible = { true }
138+ />
101139 </ SoftBox >
102- </ div >
103- ) : (
104- < SoftBox component = "form" role = "form" >
105- < SoftBox mb = { 2 } >
106- < SoftBox mb = { 1 } ml = { 0.5 } >
107- < SoftTypography component = "label" variant = "caption" fontWeight = "bold" >
108- Email
109- </ SoftTypography >
140+ ) : user && user . token ? (
141+ < div >
142+ < h3 style = { { textAlign : "center" } } > You are already signed in.</ h3 >
143+ < SoftBox mt = { 4 } mb = { 1 } >
144+ < SoftButton variant = "gradient" buttonColor = "info" fullWidth onClick = { handleRedirect } >
145+ { `Let's go` }
146+ </ SoftButton >
110147 </ SoftBox >
111- < SoftInput type = "email" name = "email" onChange = { handleFormData } placeholder = "Email" />
112- </ SoftBox >
113- < SoftBox mb = { 2 } >
114- < SoftBox mb = { 1 } ml = { 0.5 } >
115- < SoftTypography component = "label" variant = "caption" fontWeight = "bold" >
116- Password
117- </ SoftTypography >
148+ </ div >
149+ ) : (
150+ < >
151+ < SoftBox display = "flex" flexDirection = "column" alignItems = "center" mb = { 2 } >
152+ < GithubSocial />
118153 </ SoftBox >
119- < SoftInput type = "password" name = "password" onChange = { handleFormData } placeholder = "Password" />
120- </ SoftBox >
121- < SoftBox display = "flex" alignItems = "center" >
122- < Switch checked = { rememberMe } onChange = { handleSetRememberMe } />
123- < SoftTypography
124- variant = "button"
125- fontWeight = "regular"
126- onClick = { handleSetRememberMe }
127- sx = { { cursor : "pointer" , userSelect : "none" } }
128- >
129- Remember me
130- </ SoftTypography >
131- </ SoftBox >
132- < SoftBox mt = { 2 } mb = { 2 } textAlign = "center" >
154+ < Separator />
155+ < SoftBox component = "form" role = "form" >
156+ < SoftBox mb = { 2 } >
157+ < SoftBox mb = { 1 } ml = { 0.5 } >
158+ < SoftTypography component = "label" variant = "caption" fontWeight = "bold" >
159+ Email
160+ </ SoftTypography >
161+ </ SoftBox >
162+ < SoftInput type = "email" name = "email" onChange = { handleFormData } placeholder = "Email" />
163+ </ SoftBox >
164+ < SoftBox mb = { 2 } >
165+ < SoftBox mb = { 1 } ml = { 0.5 } >
166+ < SoftTypography component = "label" variant = "caption" fontWeight = "bold" >
167+ Password
168+ </ SoftTypography >
169+ </ SoftBox >
170+ < SoftInput
171+ type = "password"
172+ name = "password"
173+ onChange = { handleFormData }
174+ placeholder = "Password"
175+ />
176+ </ SoftBox >
177+ < SoftBox display = "flex" alignItems = "center" >
178+ < Switch checked = { rememberMe } onChange = { handleSetRememberMe } />
179+ < SoftTypography
180+ variant = "button"
181+ fontWeight = "regular"
182+ onClick = { handleSetRememberMe }
183+ sx = { { cursor : "pointer" , userSelect : "none" } }
184+ >
185+ Remember me
186+ </ SoftTypography >
187+ </ SoftBox >
188+ < SoftBox mt = { 2 } mb = { 2 } textAlign = "center" >
133189 < h6
134190 style = { {
135191 fontSize : ".8em" ,
@@ -142,27 +198,29 @@ function SignIn() {
142198 { error }
143199 </ h6 >
144200 </ SoftBox >
145- < SoftBox mt = { 4 } mb = { 1 } >
146- < SoftButton variant = "gradient" color = "info" onClick = { submitFormData } fullWidth >
147- sign in
148- </ SoftButton >
149- </ SoftBox >
150- < SoftBox mt = { 3 } textAlign = "center" >
151- < SoftTypography variant = "button" color = "text" fontWeight = "regular" >
152- Don't have an account?{ " " }
153- < SoftTypography
154- component = { Link }
155- to = "/authentication/sign-up"
156- variant = "button"
157- color = "info"
158- fontWeight = "medium"
159- textGradient
160- >
161- Sign up
162- </ SoftTypography >
163- </ SoftTypography >
164- </ SoftBox >
165- </ SoftBox > ) }
201+ < SoftBox mt = { 4 } mb = { 1 } >
202+ < SoftButton variant = "gradient" color = "info" onClick = { submitFormData } fullWidth >
203+ sign in
204+ </ SoftButton >
205+ </ SoftBox >
206+ < SoftBox mt = { 3 } textAlign = "center" >
207+ < SoftTypography variant = "button" color = "text" fontWeight = "regular" >
208+ Don't have an account?{ " " }
209+ < SoftTypography
210+ component = { Link }
211+ to = "/authentication/sign-up"
212+ variant = "button"
213+ color = "info"
214+ fontWeight = "medium"
215+ textGradient
216+ >
217+ Sign up
218+ </ SoftTypography >
219+ </ SoftTypography >
220+ </ SoftBox >
221+ </ SoftBox >
222+ </ >
223+ ) }
166224 </ CoverLayout >
167225 ) ;
168226}
0 commit comments