import { useQuery, useMutation } from '@apollo/react-hooks' import { makeStyles, Grid } from '@material-ui/core' import Paper from '@material-ui/core/Paper' import gql from 'graphql-tag' import QRCode from 'qrcode.react' import React, { useState } from 'react' import { useLocation, useHistory } from 'react-router-dom' import { ActionButton, Button } from 'src/components/buttons' import { CodeInput } from 'src/components/inputs/base' import { H2, Label2, Label3, P } from 'src/components/typography' import { ReactComponent as Logo } from 'src/styling/icons/menu/logo.svg' import { primaryColor } from 'src/styling/variables' import styles from './shared.styles' const QueryParams = () => new URLSearchParams(useLocation().search) const useStyles = makeStyles(styles) const VALIDATE_RESET_2FA_LINK = gql` query validateReset2FALink($token: String!) { validateReset2FALink(token: $token) { user_id secret otpauth } } ` const RESET_2FA = gql` mutation reset2FA( $token: String! $userID: ID! $secret: String! $code: String! ) { reset2FA(token: $token, userID: $userID, secret: $secret, code: $code) } ` const Reset2FA = () => { const classes = useStyles() const history = useHistory() const token = QueryParams().get('t') const [userID, setUserID] = useState(null) const [isLoading, setLoading] = useState(true) const [wasSuccessful, setSuccess] = useState(false) const [secret, setSecret] = useState(null) const [otpauth, setOtpauth] = useState(null) const [isShowing, setShowing] = useState(false) const [invalidToken, setInvalidToken] = useState(false) const [twoFAConfirmation, setTwoFAConfirmation] = useState('') const handle2FAChange = value => { setTwoFAConfirmation(value) setInvalidToken(false) } const { error: queryError } = useQuery(VALIDATE_RESET_2FA_LINK, { variables: { token: token }, onCompleted: ({ validateReset2FALink: info }) => { setLoading(false) if (!info) { setSuccess(false) } else { setUserID(info.user_id) setSecret(info.secret) setOtpauth(info.otpauth) setSuccess(true) } }, onError: () => { setLoading(false) setSuccess(false) } }) const [reset2FA, { error: mutationError }] = useMutation(RESET_2FA, { onCompleted: ({ reset2FA: success }) => { success ? history.push('/') : setInvalidToken(true) } }) const getErrorMsg = () => { if (queryError) return 'Internal server error' if (twoFAConfirmation.length !== 6 && invalidToken) return 'The code should have 6 characters!' if (mutationError || invalidToken) return 'Code is invalid. Please try again.' return null } return (

Lamassu Admin

{!isLoading && wasSuccessful && ( <>
To finish this process, please scan the following QR code or insert the secret further below on an authentication app of your choice, such Google Authenticator or Authy.
Your secret: {secret} { setShowing(!isShowing) }}> {isShowing ? 'Hide' : 'Show'}
{getErrorMsg() && (

{getErrorMsg()}

)}
)} {!isLoading && !wasSuccessful && ( <> Link has expired )}
) } export default Reset2FA