-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.ts
More file actions
134 lines (113 loc) · 3.03 KB
/
hooks.ts
File metadata and controls
134 lines (113 loc) · 3.03 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
128
129
130
131
132
133
134
import { ethers } from 'ethers'
import { useEffect, useState } from 'react'
import { SiweMessage as SIWEMessage } from 'siwe'
import login from './pages/api/login'
import { Maybe, MeResponse } from './types';
declare type ExternalProvider = (
import('@ethersproject/providers').ExternalProvider
)
declare global {
interface Window {
ethereum: ExternalProvider
}
}
export const useSIWE = () => {
const [address, setAddress] = useState<Maybe<string>>(null)
const [name, setName] = useState<Maybe<string>>(null)
const [status, setStatus] = useState<Maybe<string>>(null)
const [connected, setConnected] = (
useState(false)
)
const { host = null, origin = null } = (
typeof window !== 'undefined' ? window.location : {}
)
const [provider] = useState(
typeof window !== 'undefined' ? (
new ethers.providers.Web3Provider(
window.ethereum
)
) : (
null
)
)
useEffect(() => {
const check = async () => {
const meRes = await fetch('/api/me', {
method: 'POST',
credentials: 'same-origin',
})
if(meRes.ok) {
const me = await meRes.json()
console.info({ me })
const { ens: name, address } = me
setName(name)
setAddress(address)
setConnected(true)
}
}
check()
}, [])
const connect = async () => {
if(!provider) return
try {
await provider.send('eth_requestAccounts', [])
const signer = provider.getSigner()
const address = await signer.getAddress()
const statement = 'Login to Mimis'
setStatus('Getting nonce…')
const nonceRes = await fetch(`/api/nonce`, {
credentials: 'same-origin',
})
const nonce = await nonceRes.text()
setStatus('Building message…')
const messageBase = new SIWEMessage({
domain: host ?? 'default',
address,
statement,
uri: origin ?? 'example://default',
version: '1',
chainId: 1,
nonce,
})
const message = messageBase.prepareMessage()
setStatus('Signing message…')
const signature = await signer.signMessage(message)
setStatus('Logging in…')
const loginRes = await fetch('/api/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message, signature }),
credentials: 'same-origin',
})
const auth = await loginRes.json()
console.info({ auth })
const { ens: name } = auth as MeResponse
setConnected(true)
setName(name ?? null)
setAddress(address)
} finally {
setStatus(null)
}
console.info({ name, address })
}
const disconnect = async () => {
const logoutRes = await fetch('/api/logout', {
method: 'POST',
credentials: 'same-origin',
})
if(logoutRes.ok) {
setConnected(false)
setName(null)
setAddress(null)
}
}
return {
connected,
connect,
disconnect,
address,
name,
}
}