-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
77 lines (69 loc) · 2.44 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Magic / Google OAuth Demo</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<link
rel="stylesheet"
href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/styles/default.min.css"
/>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/10.1.2/highlight.min.js"></script>
<script src="https://auth.magic.link/sdk"></script>
<script src="https://auth.magic.link/sdk/extension/oauth"></script>
<script>
const MAGIC_API_KEY = "pk_live_4196A5A589C7E50D";
const magic = new Magic(MAGIC_API_KEY, {
extensions: [new MagicOAuthExtension()],
});
const render = async () => {
let html = ``;
if (window.location.pathname === "/callback") {
try {
const result = await magic.oauth.getRedirectResult();
const profile = JSON.stringify(result.oauth.userInfo, undefined, 2);
html = `
<h1>It Worked! 🎉</h1>
<h2>Your User Profile:</h2>
<pre><code class="json tomorrow">${profile}</code></pre>
`;
} catch {
window.location.href = window.location.origin;
}
} else {
html = `
<h2>Please sign up or log in</h2>
<form onsubmit="handleLogin(event)">
<button id="btn-send" class="google" type="submit">
<img src="./sign-in-with-google.svg" />
</button>
</form>
`;
}
document.getElementById("app").innerHTML = html;
document.querySelectorAll("pre code").forEach((block) => {
hljs.highlightBlock(block);
});
};
/**
* Starts the OAuth 2.0 login flow.
*/
const handleLogin = async (e) => {
e.preventDefault();
// Render a button "pending" state.
const btnSend = document.getElementById("btn-send");
btnSend.disabled = true;
btnSend.innerText = "Logging in...";
// Start the Google OAuth 2.0 flow!
const didToken = await magic.oauth.loginWithRedirect({
provider: "google",
redirectURI: `${window.location.origin}/callback`
});
};
</script>
</head>
<body onload="render()">
<div id="app">Loading...</div>
</body>
</html>