-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsuccess.html
More file actions
127 lines (115 loc) · 3.79 KB
/
success.html
File metadata and controls
127 lines (115 loc) · 3.79 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!DOCTYPE html>
<html>
<head>
<title>Ethers.js Sign Message Example</title>
<script src="https://cdn.ethers.io/lib/ethers-5.7.1.umd.min.js"
type="application/javascript"></script>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
}
h1 {
margin-bottom: 20px;
}
p {
text-align: center;
font-size: 20px;
margin-top: 50px;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
background-color: #3f51b5;
color: #fff;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #1c2b46;
}
</style>
</head>
<body>
<h1>Ethers.js Sign Message Example</h1>
<button onclick="signMessage()">Sign Message</button>
<p id="p1"></p>
<script>
// Get the nonce value from the backend server
async function getNonce() {
const response = await fetch('/api/nonce');
const data = await response.json();
return data.nonce;
}
// Sign the message with the nonce and send it to the backend server for verification
async function signMessage() {
try {
var element = document.getElementById("p1");
let stored = window.localStorage.getItem("token")
console.log(stored)
if (window.localStorage.getItem("token") != null ) {
let token = window.localStorage.getItem("token");
const response = await fetch('/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
let newresponse = await response.json();
console.log(newresponse);
if (newresponse == "ok") {
window.location.href = '/success';
}
else {
window.localStorage.removeItem("token");
element.innerHTML = "Token Expired, log in again to get a new Token !!!"
}
}
else {
// Get the nonce value from the backend server
const nonce = await getNonce();
// Get the signer account using MetaMask
const provider = new ethers.providers.Web3Provider(window.ethereum);
const signer = provider.getSigner();
const address = await signer.getAddress();
// Sign the message using the signer account and the nonce value
const message = `I am signing this message to prove my identity. Nonce: ${nonce}`;
const signedMessage = await signer.signMessage(message);
const data = { signedMessage, message, address };
// Send the signed message and the public address of the signer to the backend server for verification
const response = await fetch('/login', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
let token = await response.json();
window.localStorage.setItem("token", token);
const newResponse = await fetch('/verify', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});
let answer = await newResponse.json();
console.log(answer)
if (answer == "ok") {
window.location.href = '/success';
}
else {
var element = document.getElementById("p1");
element.innerHTML = "Invalid Token !!!"
}
}
} catch (error) {
console.error(error);
}
}
</script>
</body>
</html>