-
Notifications
You must be signed in to change notification settings - Fork 665
Expand file tree
/
Copy pathoauth.ts
More file actions
133 lines (127 loc) · 4.01 KB
/
oauth.ts
File metadata and controls
133 lines (127 loc) · 4.01 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
import type { ThirdwebClient } from "../../../../../client/client.js";
import { getThirdwebBaseUrl } from "../../../../../utils/domains.js";
import type { OAuthOption } from "../../../../../wallets/types.js";
import { getLoginUrl } from "../../../core/authentication/getLoginPath.js";
import type { AuthStoredTokenWithCookieReturnType } from "../../../core/authentication/types.js";
import type { Ecosystem } from "../../../core/wallet/types.js";
import { DEFAULT_POP_UP_SIZE } from "./constants.js";
const closeWindow = ({
isWindowOpenedByFn,
win,
closeOpenedWindow,
}: {
win?: Window | null;
isWindowOpenedByFn: boolean;
closeOpenedWindow?: (openedWindow: Window) => void;
}) => {
if (isWindowOpenedByFn) {
win?.close();
} else {
if (win && closeOpenedWindow) {
closeOpenedWindow(win);
} else if (win) {
win.close();
}
}
};
export async function loginWithOauthRedirect(options: {
authOption: OAuthOption;
client: ThirdwebClient;
ecosystem?: Ecosystem;
redirectUrl?: string;
mode?: "redirect" | "popup" | "window";
authFlow?: "connect" | "link";
}): Promise<void> {
const loginUrl = getLoginUrl({
...options,
mode: options.mode || "redirect",
authFlow: options.authFlow,
});
if (options.mode === "redirect") {
window.location.href = loginUrl;
} else {
window.open(loginUrl);
}
// wait for 5 secs for the redirect to happen
// that way it interrupts the rest of the execution that would normally keep connecting
await new Promise((resolve) => setTimeout(resolve, 5000));
}
export const loginWithOauth = async (options: {
authOption: OAuthOption;
client: ThirdwebClient;
ecosystem?: Ecosystem;
openedWindow?: Window | null | undefined;
closeOpenedWindow?: ((openedWindow: Window) => void) | undefined;
}): Promise<AuthStoredTokenWithCookieReturnType> => {
let win = options.openedWindow;
let isWindowOpenedByFn = false;
if (!win) {
win = window.open(
getLoginUrl({ ...options, mode: "popup" }),
`Login to ${options.authOption}`,
DEFAULT_POP_UP_SIZE,
);
isWindowOpenedByFn = true;
}
if (!win) {
throw new Error("Something went wrong opening pop-up");
}
const result = await new Promise<AuthStoredTokenWithCookieReturnType>(
(resolve, reject) => {
// detect when the user closes the login window
const pollTimer = window.setInterval(async () => {
if (win.closed) {
clearInterval(pollTimer);
window.removeEventListener("message", messageListener);
reject(new Error("User closed login window"));
}
}, 1000);
const messageListener = async (
event: MessageEvent<{
eventType: string;
authResult?: AuthStoredTokenWithCookieReturnType;
errorString?: string;
}>,
) => {
if (event.origin !== getThirdwebBaseUrl("inAppWallet")) {
return;
}
if (typeof event.data !== "object") {
reject(new Error("Invalid event data"));
return;
}
switch (event.data.eventType) {
case "oauthSuccessResult": {
window.removeEventListener("message", messageListener);
clearInterval(pollTimer);
closeWindow({
closeOpenedWindow: options.closeOpenedWindow,
isWindowOpenedByFn,
win,
});
if (event.data.authResult) {
resolve(event.data.authResult);
}
break;
}
case "oauthFailureResult": {
window.removeEventListener("message", messageListener);
clearInterval(pollTimer);
closeWindow({
closeOpenedWindow: options.closeOpenedWindow,
isWindowOpenedByFn,
win,
});
reject(new Error(event.data.errorString));
break;
}
default: {
// no-op, DO NOT THROW HERE
}
}
};
window.addEventListener("message", messageListener);
},
);
return result;
};