-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (61 loc) · 2.22 KB
/
index.js
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
/**
* Generates a simple HTML login button pointing an an instance of MIT Open
* If already logged in, displays the user's username
* @function initLoginButton
* @param {String} containerId The id property of the container element to place the login button / status in
* @param {String} baseUrl The base URL of the MIT Open instance
* @param {String} redirectUrl The URL to redirect to after login is complete
* @param {String} buttonText The text to show on the Login button
* @param {String} buttonClass The CSS class(es) to assign to the button
* @param {String} loggedInTextClass The CSS class(es) to assign to the logged-in status text
*/
export function initLoginButton(
containerId,
baseUrl,
redirectUrl = "",
buttonText = "Login",
buttonClass = "",
loggedInTextClass = "",
) {
const container = document.getElementById(containerId)
const parsedBaseUrl = new URL(baseUrl)
const currentUserUrl = `${parsedBaseUrl.origin}/api/v0/users/me/?format=json`
const redirectUrlParam =
redirectUrl !== "" ? `?next=${encodeURIComponent(redirectUrl)}` : ""
const loginUrl = `${parsedBaseUrl.origin}/login/ol-oidc/${redirectUrlParam}`
fetch(currentUserUrl, {
method: "GET",
credentials: "include",
mode: "cors",
headers: {
Accept: "application/json",
},
})
.then((response) => {
if (!response.ok) {
// create the login button
const linkButton = document.createElement("a")
const linkText = document.createTextNode(buttonText)
linkButton.appendChild(linkText)
linkButton.title = buttonText
linkButton.href = loginUrl
if (buttonClass !== "") {
linkButton.classList.add(...buttonClass.split(" "))
}
container.appendChild(linkButton)
}
return response.json()
})
.then((data) => {
if (data["username"] !== undefined) {
// display the logged in user
const userName = data["username"]
const loggedInText = document.createElement("span")
if (loggedInTextClass !== "") {
loggedInText.classList.add(...loggedInTextClass.split(" "))
}
loggedInText.textContent = `Logged in as: ${userName}`
container.appendChild(loggedInText)
}
})
}