generated from goitacademy/react-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from 001elijah/something
- Loading branch information
Showing
11 changed files
with
245 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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 |
---|---|---|
|
@@ -79,4 +79,5 @@ | |
AOS.init(); | ||
</script> | ||
</body> | ||
<script src="https://apis.google.com/js/api.js" async defer></script> | ||
</html> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,66 @@ | ||
import { useFormik } from 'formik'; | ||
import { useState } from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
|
||
import { ButtonAuth } from '../ButtonAuth/ButtonAuth'; | ||
import { validationSchemaLogin } from '../schemaValidation'; | ||
import { loginUser } from 'redux/Auth/authOperations'; | ||
import { LoginWithGoogle } from '../../AuthWithGoogle/AuthWithGoogle'; | ||
|
||
import icons from '../../../assets/icons/sprite.svg'; | ||
import y from './Login.module.scss'; | ||
|
||
export const Login = () => { | ||
const dispatch = useDispatch(); | ||
const [showPassword, setShowPassword] = useState(false); | ||
|
||
const toggleShowPassword = () => setShowPassword(!showPassword); | ||
|
||
const formik = useFormik({ | ||
initialValues: { | ||
email: '', | ||
password: '', | ||
}, | ||
validationSchema: validationSchemaLogin, | ||
onSubmit: values => { | ||
dispatch(loginUser(values)); | ||
}, | ||
}); | ||
return ( | ||
<form onSubmit={formik.handleSubmit}> | ||
<label htmlFor="email"> | ||
<input | ||
id="email" | ||
name="email" | ||
type="email" | ||
onChange={formik.handleChange} | ||
value={formik.values.email} | ||
placeholder="Enter your email" | ||
/> | ||
{formik.touched.email && formik.errors.email && ( | ||
<p className={y.error}>{formik.errors.email}</p> | ||
)} | ||
</label> | ||
<label htmlFor="password"> | ||
<input | ||
id="password" | ||
name="password" | ||
type={showPassword ? 'text' : 'password'} | ||
onChange={formik.handleChange} | ||
value={formik.values.password} | ||
placeholder="Confirm a password" | ||
/> | ||
<svg className={y.eye} onClick={toggleShowPassword}> | ||
<use | ||
href={showPassword ? `${icons}#icon-eye` : `${icons}#icon-antiEye`} | ||
></use> | ||
</svg> | ||
{formik.touched.password && formik.errors.password && ( | ||
<p className={y.error}>{formik.errors.password}</p> | ||
)} | ||
</label> | ||
<ButtonAuth title="Log In Now" /> | ||
<LoginWithGoogle></LoginWithGoogle> | ||
</form> | ||
); | ||
}; |
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,25 @@ | ||
import React from 'react'; | ||
import { useDispatch } from 'react-redux'; | ||
import { authWithGoogleOperation } from '../../redux/Auth/authGoogleOperations'; | ||
import svg from '../../assets/icons/sprite.svg'; | ||
import s from './AuthWithGoogle.module.scss'; | ||
|
||
export const AuthWithGoogle = () => { | ||
const dispatch = useDispatch(); | ||
|
||
const handleGoogleLogin = () => { | ||
const googleClientId = `128986061205-1q4bl09jae2cqs2qljamh3tvboatcrin.apps.googleusercontent.com`; | ||
dispatch(authWithGoogleOperation(googleClientId)); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<button type="button" className={s.button} onClick={handleGoogleLogin}> | ||
<span className={s.title}>Continue with</span> | ||
<svg className={s.google}> | ||
<use xlinkHref={`${svg}#icon-google`} /> | ||
</svg> | ||
</button> | ||
</div> | ||
); | ||
}; |
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,41 @@ | ||
@import '../../assets/styles/vars'; | ||
@import '../../assets/styles/mixins'; | ||
|
||
.button { | ||
width: 100%; | ||
height: 49px; | ||
margin-top: 24px; | ||
|
||
background-color: $accent-color; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
border: none; | ||
border-radius: 8px; | ||
|
||
cursor: pointer; | ||
|
||
&.colorful { | ||
background-color: $accent-bright-color; | ||
color: $white-text-color; | ||
} | ||
|
||
@include mobile { | ||
min-width: 288px; | ||
} | ||
|
||
@include tablet { | ||
min-width: 344px; | ||
} | ||
} | ||
.title { | ||
color: $black-text-color; | ||
font-weight: 500; | ||
font-size: 14px; | ||
line-height: 1.5; | ||
text-align: end; | ||
} | ||
.google { | ||
height: 50px; | ||
width: 72px; | ||
} |
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 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,67 @@ | ||
import { gapi } from 'gapi-script'; | ||
import { authWithGoogleApi } from '../../services/backendAPI'; | ||
import { logOut } from './authSlice'; | ||
import { Notify } from 'notiflix'; | ||
|
||
export const authWithGoogleOperation = googleClientId => { | ||
return async dispatch => { | ||
try { | ||
const authResult = await authenticateWithGoogle(googleClientId); | ||
if (authResult.error) { | ||
throw new Error(authResult.error); | ||
} | ||
|
||
const { credential } = authResult; | ||
const data = await authWithGoogleApi({ credential }); | ||
const { token, userName, email, theme, avatarUrl } = data; | ||
|
||
dispatch({ | ||
type: 'auth/register/fulfilled', | ||
payload: { | ||
token, | ||
userName, | ||
email, | ||
theme, | ||
avatarUrl, | ||
}, | ||
}); | ||
Notify.success('Welcome'); | ||
} catch (error) { | ||
console.log(error); | ||
dispatch(logOut()); | ||
if (error.status === 400) { | ||
Notify.failure('Error'); | ||
} else if (error.status === 500) { | ||
Notify.failure('Server error'); | ||
} | ||
} | ||
}; | ||
}; | ||
|
||
const authenticateWithGoogle = googleClientId => { | ||
return new Promise((resolve, reject) => { | ||
// Initialize the Google Sign-In API | ||
gapi.load('auth2', () => { | ||
gapi.auth2.init({ | ||
client_id: googleClientId, | ||
}); | ||
|
||
// Trigger Google Sign-In flow | ||
gapi.auth2 | ||
.getAuthInstance() | ||
.signIn({ prompt: 'select_account' }) | ||
.then(googleUser => { | ||
const idToken = googleUser.getAuthResponse().id_token; | ||
|
||
// Resolve with the credential containing the Google ID token | ||
resolve({ credential: { idToken } }); | ||
}) | ||
.catch(error => { | ||
// Reject with the error message | ||
reject( | ||
new Error(`Failed to authenticate with Google: ${error.error}`), | ||
); | ||
}); | ||
}); | ||
}); | ||
}; |
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