import { useMutation, useQuery, useLazyQuery } from '@apollo/react-hooks' import { makeStyles } from '@material-ui/core/styles' import gql from 'graphql-tag' import QRCode from 'qrcode.react' import React, { useContext, useState } from 'react' import { useHistory } from 'react-router-dom' import AppContext from 'src/AppContext' import { ActionButton, Button } from 'src/components/buttons' import { CodeInput } from 'src/components/inputs/base' import { Label3, P } from 'src/components/typography' import { primaryColor } from 'src/styling/variables' import styles from './shared.styles' const SETUP_2FA = gql` mutation setup2FA( $username: String! $password: String! $rememberMe: Boolean! $codeConfirmation: String! ) { setup2FA( username: $username password: $password rememberMe: $rememberMe codeConfirmation: $codeConfirmation ) } ` const GET_2FA_SECRET = gql` query get2FASecret($username: String!, $password: String!) { get2FASecret(username: $username, password: $password) { secret otpauth } } ` const GET_USER_DATA = gql` { userData { id username role } } ` const useStyles = makeStyles(styles) const Setup2FAState = ({ state, dispatch }) => { const classes = useStyles() const history = useHistory() const { setUserData } = useContext(AppContext) 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(GET_2FA_SECRET, { variables: { username: state.clientField, password: state.passwordField }, onCompleted: ({ get2FASecret }) => { setSecret(get2FASecret.secret) setOtpauth(get2FASecret.otpauth) } }) const [getUserData] = useLazyQuery(GET_USER_DATA, { onCompleted: ({ userData }) => { setUserData(userData) history.push('/') } }) const [setup2FA, { error: mutationError }] = useMutation(SETUP_2FA, { onCompleted: ({ setup2FA: success }) => { success ? getUserData() : setInvalidToken(true) } }) const getErrorMsg = () => { if (mutationError || queryError) return 'Internal server error.' if (twoFAConfirmation.length !== 6 && invalidToken) return 'The code should have 6 characters!' if (invalidToken) return 'Code is invalid. Please try again.' return null } return ( secret && otpauth && ( <>
We detected that this account does not have its two-factor authentication enabled. In order to protect the resources in the system, a two-factor authentication is enforced. To finish this process, please scan the following QR code or insert the secret further below on an authentication app of your choice, such as Google Authenticator or Authy.
Your secret: {secret} { setShowing(!isShowing) }}> {isShowing ? 'Hide' : 'Show'}
{getErrorMsg() && (

{getErrorMsg()}

)}
) ) } export default Setup2FAState