import React, { useEffect, useRef, useState } from "react";
// import { useHistory } from 'react';
// import { redirect } from "react-router-dom";
import "../styles/App.css";
import api from '../script/axiosApi';
import QRCodeStyling from "qr-code-styling";
import { motion } from 'framer-motion'
const qrCode = new QRCodeStyling({
width: 300,
height: 300,
// image: "../assets/profile.jpg",
dotsOptions: {
color: "black",
type: "rounded"
},
backgroundOptions: {
color: "#5843e4",
},
imageOptions: {
crossOrigin: "anonymous",
margin: 20
}
});
function QrCode () {
// const url = "https://www.youtube.com";
const ref = useRef(null);
const [user, setUser] = useState(false);
const [url, setUrl] = useState(false);
const [secret, setSecret] = useState(false);
const [code, setCode] = useState('');
const [activated, setActivated] = useState(false);
// const history = useHistory();
useEffect(() => {
qrCode.append(ref.current);
const getUser = async ()=>{
try{
const tmpUser = await api.get("/profile");
setUser(tmpUser.data);
if (tmpUser.data.otp_verified)
{
setActivated(true);
return ;
}
const otpData = await api.post("/otp");
setUrl(otpData.data.otpauth_url);
setSecret(otpData.data.base32_secret);
// const tmpUser = await api.get("/profile")
// console.log("test")
// console.table(convs);
}
catch(err){
console.log(err);
}
};
getUser();
}, []);
useEffect(() => {
qrCode.update({
data: url
});
}, [url]);
const handleKeyPress = async (e)=>{
// console.log(`e in press= ${e.key}`)
if (e.key !== "Enter")
return ;
try{
console.log("code= ", code)
const res = await api.post("/verifyOtp", {token: code})
console.log("res= ", res.data)
console.log("res= ", res)
if (res.data === 1)
{
console.log("registered")
// history.push('/login')
const path = 'http://' + process.env.REACT_APP_BASE_URL + '/';
window.history.pushState({}, null, path);
window.location.reload(false);
}
else
{
console.log("bad code")
//alert ?? retry
}
// redirect('/test')
}
catch(err){
console.log(err)
}
}
const handleDesactivate = async () => {
try {
await api.post("/deleteOtp")
// const path = 'http://' + process.env.REACT_APP_BASE_URL + '/';
// window.history.pushState({}, null, path);
window.location.reload(false);
} catch(err) {
console.log(err);
}
};
return (
//
// QRcode
// {secret}
//
//
// {}
//
{!activated && (
<>
Enter The Secret
{secret}
Or Scan The QRCode
>
)}
<>
{!activated && localStorage.getItem('token') ? (
<>
Double Auth Validation
setCode(e.target.value)}
/>
>
) : (
)}
>
{/* {!localStorage.getItem('token') && (
<>
Double Auth
setCode(e.target.value)}
/>
>
) : ()}
*/}
{/* {!activated && (
)} */}
)
}
export default QrCode