-
Notifications
You must be signed in to change notification settings - Fork 41
/
Appbar.tsx
43 lines (38 loc) · 1.29 KB
/
Appbar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"use client";
import { useWallet } from "@solana/wallet-adapter-react";
import { useEffect } from "react";
import axios from "axios";
import { BACKEND_URL } from "@/utils";
import dynamic from "next/dynamic";
const WalletButton = dynamic(() => import("./WalletButton"), { ssr: false });
export const Appbar = () => {
const { publicKey, signMessage } = useWallet();
async function signAndSend() {
if (!publicKey || !signMessage || !window) {
return;
}
if (window.localStorage.getItem("token")) {
return;
}
const message = new TextEncoder().encode("Sign into mechanical turks");
const signature = await signMessage(message);
console.log(signature.toString());
console.log(publicKey.toString());
const response = await axios.post(`${BACKEND_URL}/v1/user/signin`, {
signature: signature.toString(),
publicKey: publicKey.toString(),
});
localStorage.setItem("token", response.data.token);
}
useEffect(() => {
signAndSend();
}, [publicKey]);
return (
<div className="flex justify-between border-b pb-2 pt-2">
<div className="text-2xl pl-4 flex justify-center pt-3">Turkify</div>
<div className="text-xl pr-4 pb-2">
<WalletButton publicKey={publicKey?.toString()}></WalletButton>
</div>
</div>
);
};