MCPcopy Create free account
hub / github.com/UI5/webcomponents / auth

Function auth

packages/website/src/components/Editor/githubAuth.js:9–68  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

7 * Handle OAuth popup window and authentication flow
8 */
9export const auth = async () => {
10 const { authUrl, state } = await fetchAuthUrl();
11
12 // open github oauth popup
13 const popup = window.open(authUrl, "github-auth", "width=600,height=700,scrollbars=yes,resizable=yes");
14
15 // listen for oauth callback
16 const authResult = await new Promise((resolve, reject) => {
17 const messageHandler = (event) => {
18 // verify origin for security
19 if (!event.origin.includes("github.com") && !event.origin.includes("ui5-webc-playground-server.netlify.app")) {
20 return;
21 }
22
23 if (event.data.type === "GITHUB_OAUTH_SUCCESS") {
24 window.removeEventListener("message", messageHandler);
25 popup.close();
26 resolve(event.data);
27 } else if (event.data.type === "GITHUB_OAUTH_ERROR") {
28 window.removeEventListener("message", messageHandler);
29 popup.close();
30 reject(new Error(event.data.error));
31 }
32 };
33
34 window.addEventListener("message", messageHandler);
35
36 // poll for url changes in popup
37 const pollTimer = setInterval(() => {
38 try {
39 if (popup.closed) {
40 clearInterval(pollTimer);
41 window.removeEventListener("message", messageHandler);
42 reject(new Error("authentication cancelled by user"));
43 return;
44 }
45
46 // check if we got redirected back with code
47 const url = popup.location.href;
48 if (url.includes("code=")) {
49 clearInterval(pollTimer);
50 window.removeEventListener("message", messageHandler);
51
52 const urlParams = new URLSearchParams(new URL(url).search);
53 const code = urlParams.get("code");
54 const returnedState = urlParams.get("state");
55
56 popup.close();
57 resolve({ code, state: returnedState });
58 }
59 } catch (error) {
60 // cross-origin restrictions prevent access to popup url
61 // continue polling until popup closes or we get a message
62 }
63 }, 1000);
64 });
65
66 // exchange code for token

Callers 1

signInWithGitHubFunction · 0.90

Calls 4

fetchAuthUrlFunction · 0.90
exchangeCodeForTokenFunction · 0.90
getMethod · 0.80
closeMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…