Advertisement
Fahim_7861

Untitled

Jul 7th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. import React, { useState } from "react"
  2. import { useRouter } from "next/router"
  3. import Avatar from "@material-ui/core/Avatar"
  4. import Box from "@material-ui/core/Box"
  5. import Button from "@material-ui/core/Button"
  6. import CircularProgress from "@material-ui/core/CircularProgress"
  7. import Container from "@material-ui/core/Container"
  8. import TextField from "@material-ui/core/TextField"
  9. import Typography from "@material-ui/core/Typography"
  10. import LockOutlined from "@material-ui/icons/LockOutlined"
  11. import { VariantType } from "notistack"
  12. import { GoogleLoginResponse, GoogleLoginResponseOffline, useGoogleLogin } from "react-google-login"
  13. import { useDispatch } from "react-redux"
  14. import { v4 as uuidV4 } from "uuid"
  15. import Google from "assets/icons/Google"
  16. import Copyright from "components/copyright/Copyright"
  17. import { googleSignInSuccess, serverSignIn } from "redux/auth/action"
  18. import { enqueueSnackbar } from "redux/ui/actions"
  19. import { Notification } from "redux/ui/reducers"
  20. import { useStyles } from "./SignIn.style"
  21. const SignIn: React.FC = () => {
  22. const classes = useStyles()
  23. const [loading, setLoading] = useState(false)
  24. const dispatch = useDispatch()
  25. const router = useRouter()
  26. const buildNotification = (message: string, variant: VariantType) => {
  27. const notification: Notification = ({
  28. message: message,
  29. options: {
  30. key: uuidV4(),
  31. variant: variant,
  32. },
  33. })
  34. dispatch(enqueueSnackbar(notification))
  35. }
  36. const { signIn, loaded } = useGoogleLogin({
  37. clientId: process.env.NEXT_PUBLIC_GOOGLE_OAUTH2_CLIENT_ID!,
  38. isSignedIn: false,
  39. uxMode: "popup",
  40. scope: "profile email",
  41. accessType: "online",
  42. responseType: "token",
  43. // prompt: "consent",
  44. onAutoLoadFinished: () => console.log("onAutoLoadFinished"),
  45. onSuccess: (loginResponse: GoogleLoginResponse | GoogleLoginResponseOffline) => {
  46. console.log(loginResponse)
  47. setLoading(false)
  48. if ("tokenObj" in loginResponse) {
  49. dispatch(googleSignInSuccess(loginResponse.tokenObj))
  50. const onServerSignInSuccess = () => router.push("/")
  51. dispatch(serverSignIn(loginResponse.tokenObj.id_token, onServerSignInSuccess))
  52. // dispatch(getUserData(loginResponse.tokenObj.id_token));
  53. }
  54. },
  55. onFailure: (error) => {
  56. console.log("onFailure", error)
  57. setLoading(false)
  58. buildNotification(error.error, "error")
  59. },
  60. })
  61. return (
  62. <Container component="main" maxWidth="xs">
  63. <div className={classes.paper}>
  64. <Avatar className={classes.avatar}>
  65. <LockOutlined/>
  66. </Avatar>
  67. <Typography component="h1" variant="h5">
  68. Sign in
  69. </Typography>
  70. <form className={classes.form} noValidate>
  71. <TextField
  72. variant="outlined"
  73. margin="normal"
  74. required
  75. fullWidth
  76. id="email"
  77. label="Email Address"
  78. name="email"
  79. autoComplete="email"
  80. autoFocus
  81. disabled={true}
  82. />
  83. <TextField
  84. variant="outlined"
  85. margin="normal"
  86. required
  87. fullWidth
  88. name="password"
  89. label="Password"
  90. type="password"
  91. id="password"
  92. autoComplete="current-password"
  93. disabled={true}
  94. />
  95. <Button
  96. type="submit"
  97. fullWidth
  98. variant="contained"
  99. color="primary"
  100. className={classes.submit}
  101. disabled={true}
  102. >
  103. Sign In
  104. </Button>
  105. <Typography variant="body2" color="textSecondary" align="center">OR</Typography>
  106. <Button
  107. type="button"
  108. fullWidth
  109. variant="contained"
  110. color="primary"
  111. className={classes.submit}
  112. startIcon={!loaded || loading ? <CircularProgress size={24} color={"inherit"}/> : <Google/>}
  113. onClick={() => {
  114. setLoading(true)
  115. signIn()
  116. }}
  117. disabled={!loaded || loading}
  118. >
  119. Sign In with Google
  120. </Button>
  121. </form>
  122. </div>
  123. <Box mt={8}>
  124. <Copyright/>
  125. </Box>
  126. </Container>
  127. )
  128. }
  129. export default SignIn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement