|
| 1 | +"use client"; |
| 2 | +import axios from "axios"; |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | +import Cookies from "js-cookie"; |
| 5 | +import Link from "next/link"; |
| 6 | + |
| 7 | +const REACT_APP_GATEWAY_URL = "https://gateway.netsepio.com/"; |
| 8 | + |
| 9 | +const Navbar = () => { |
| 10 | + const wallet = Cookies.get("taylor_wallet"); |
| 11 | + |
| 12 | + const [hovered, setHovered] = useState(false); |
| 13 | + const [avatarUrl, setAvatarUrl] = useState(""); |
| 14 | + |
| 15 | + const logout = { |
| 16 | + color: hovered ? "red" : "black", |
| 17 | + }; |
| 18 | + |
| 19 | + const getAptosWallet = () => { |
| 20 | + if ("aptos" in window) { |
| 21 | + return window.aptos; |
| 22 | + } else { |
| 23 | + window.open("https://petra.app/", "_blank"); |
| 24 | + } |
| 25 | + }; |
| 26 | + |
| 27 | + const connectWallet = async () => { |
| 28 | + const wallet = getAptosWallet(); |
| 29 | + try { |
| 30 | + const response = await wallet.connect(); |
| 31 | + |
| 32 | + const account = await wallet.account(); |
| 33 | + console.log("account", account); |
| 34 | + |
| 35 | + const networkwallet = await window.aptos.network(); |
| 36 | + |
| 37 | + // Check if the connected network is Mainnet |
| 38 | + if (networkwallet === "Testnet") { |
| 39 | + const { data } = await axios.get( |
| 40 | + `${REACT_APP_GATEWAY_URL}api/v1.0/flowid?walletAddress=${account.address}` |
| 41 | + ); |
| 42 | + console.log(data); |
| 43 | + |
| 44 | + const message = data.payload.eula; |
| 45 | + const nonce = data.payload.flowId; |
| 46 | + const publicKey = account.publicKey; |
| 47 | + |
| 48 | + const { signature, fullMessage } = await wallet.signMessage({ |
| 49 | + message, |
| 50 | + nonce, |
| 51 | + }); |
| 52 | + console.log("sign", signature, "full message", fullMessage); |
| 53 | + |
| 54 | + const authenticationData = { |
| 55 | + flowId: nonce, |
| 56 | + signature: `0x${signature}`, |
| 57 | + pubKey: publicKey, |
| 58 | + }; |
| 59 | + |
| 60 | + const authenticateApiUrl = `${REACT_APP_GATEWAY_URL}api/v1.0/authenticate`; |
| 61 | + |
| 62 | + const config = { |
| 63 | + url: authenticateApiUrl, |
| 64 | + method: "POST", |
| 65 | + headers: { |
| 66 | + "Content-Type": "application/json", |
| 67 | + }, |
| 68 | + data: authenticationData, |
| 69 | + }; |
| 70 | + |
| 71 | + try { |
| 72 | + const response = await axios(config); |
| 73 | + console.log("auth data", response.data); |
| 74 | + const token = await response?.data?.payload?.token; |
| 75 | + const userId = await response?.data?.payload?.userId; |
| 76 | + // localStorage.setItem("platform_token", token); |
| 77 | + Cookies.set("taylor_token", token, { expires: 7 }); |
| 78 | + Cookies.set("taylor_wallet", account.address, { expires: 7 }); |
| 79 | + Cookies.set("taylor_userid", userId, { expires: 7 }); |
| 80 | + |
| 81 | + window.location.reload(); |
| 82 | + } catch (error) { |
| 83 | + console.error(error); |
| 84 | + } |
| 85 | + } else { |
| 86 | + alert(`Switch to Testnet in your wallet`); |
| 87 | + } |
| 88 | + } catch (err) { |
| 89 | + console.log(err); |
| 90 | + } |
| 91 | + }; |
| 92 | + |
| 93 | + const handleDeleteCookie = () => { |
| 94 | + Cookies.remove("taylor_wallet"); |
| 95 | + Cookies.remove("taylor_token"); |
| 96 | + window.location.href = "/"; |
| 97 | + }; |
| 98 | + |
| 99 | + useEffect(() => { |
| 100 | + const fetchData = async () => { |
| 101 | + try { |
| 102 | + const getRandomNumber = () => Math.floor(Math.random() * 1000); |
| 103 | + const apiUrl = `https://api.multiavatar.com/${getRandomNumber()}`; |
| 104 | + |
| 105 | + const response = await axios.get(apiUrl); |
| 106 | + const svgDataUri = `data:image/svg+xml,${encodeURIComponent(response.data)}`; |
| 107 | + setAvatarUrl(svgDataUri); |
| 108 | + } catch (error) { |
| 109 | + console.error('Error fetching avatar:', error.message); |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + fetchData(); |
| 114 | + }, []); |
| 115 | + |
| 116 | + return ( |
| 117 | + <div> |
| 118 | + {wallet ? ( |
| 119 | + <div className="flex gap-4"> |
| 120 | + <Link href="/profile">{avatarUrl && <img src={avatarUrl} alt="Avatar" style={{width: 45}}/>} </Link> |
| 121 | + <div> |
| 122 | + <div className="ltext-black rounded-lg text-lg font-bold text-center"> |
| 123 | + {wallet.slice(0, 4)}...{wallet.slice(-4)} |
| 124 | + </div> |
| 125 | + <button |
| 126 | + onClick={handleDeleteCookie} |
| 127 | + style={logout} |
| 128 | + className="mx-auto hover:text-red-400 text-black text-lg font-bold" |
| 129 | + onMouseEnter={() => setHovered(true)} |
| 130 | + onMouseLeave={() => setHovered(false)} |
| 131 | + > |
| 132 | + Logout |
| 133 | + </button> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + ) : ( |
| 137 | + <> |
| 138 | + <button onClick={connectWallet}>Connect wallet</button> |
| 139 | + </> |
| 140 | + )} |
| 141 | + </div> |
| 142 | + ); |
| 143 | +}; |
| 144 | + |
| 145 | +export default Navbar; |
0 commit comments