-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign-in-with-ethereum.ts
63 lines (50 loc) · 2.26 KB
/
sign-in-with-ethereum.ts
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
import { Auth, createUserWithEmailAndPassword, signInWithEmailAndPassword, updateProfile, User, UserCredential } from "firebase/auth";
const isObject = (obj: any): obj is Record<any, any> => typeof obj === "object" && obj !== null;
const isGlobalThisEthereum = (obj: any): obj is { ethereum: { request: <R = any>(payload: Record<any, any>) => Promise<R> } } => isObject(obj) && isObject(obj.ethereum) && typeof obj.ethereum.request === "function";
const getEthereum = (obj: any) => isGlobalThisEthereum(obj) ? obj.ethereum : null;
const ethereum = getEthereum(globalThis);
let signMessage = (opts: { siteName: string, address: string }) =>
`${opts.siteName} wants you to sign in with your Ethereum account:\n${opts.address}`
export async function signInWithEthereum(auth: Auth, opts?: { siteName?: string }): Promise<UserCredential> {
if (!ethereum) {
throw new Error("No ethereum provider found");
}
const [account] = await ethereum.request<string[]>({ method: "eth_requestAccounts" });
if (!account) {
throw new Error("No account found");
}
const signed = await ethereum.request<string>({
method: 'personal_sign',
params: [
signMessage({
siteName: opts?.siteName ?? auth.app.options.projectId ?? globalThis.location.hostname,
address: account,
}),
account,
auth.app.options.appId,
],
});
let fragment = signed.slice(-10);
const email = `${account}.${fragment}.3@${auth.app.options.authDomain}`;
const password = signed;
let userCredential: UserCredential;
let user: User;
try {
userCredential = await signInWithEmailAndPassword(auth, email, password);
user = userCredential.user;
} catch (e) {
const isAuthUserNotFound = (e: any) => typeof e === "object" && e !== null && e.hasOwnProperty("code") && e.code === "auth/user-not-found"
if (isAuthUserNotFound(e)) {
userCredential = await createUserWithEmailAndPassword(auth, email, password);
user = userCredential.user;
} else {
throw e;
}
}
if (!user.displayName) {
await updateProfile(user, {
displayName: account,
});
}
return userCredential;
}