Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
gumaerc committed Jan 25, 2024
1 parent 04ab6f7 commit 6b52cdc
Showing 1 changed file with 38 additions and 30 deletions.
68 changes: 38 additions & 30 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,51 @@
* @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, buttonText = "Login", buttonClass = "", loggedInTextClass = "") {
export function initLoginButton(
containerId,
baseUrl,
buttonText = "Login",
buttonClass = "",
loggedInTextClass = ""
) {
const container = document.getElementById(containerId)
const parsedBaseUrl = new URL(baseUrl)
const reconstructedBaseUrl = `${parsedBaseUrl.protocol}//${parsedBaseUrl.hostname}${parsedBaseUrl.port !== '' ? `:${parsedBaseUrl.port}` : ''}`
const reconstructedBaseUrl = `${parsedBaseUrl.protocol}//${parsedBaseUrl.hostname}${parsedBaseUrl.port !== "" ? `:${parsedBaseUrl.port}` : ""}`
const apiUrl = `${reconstructedBaseUrl}/api/v0/users/me?format=json`
const loginUrl = `${reconstructedBaseUrl}/login/ol-oidc/`
fetch(apiUrl, {
method: "GET",
method: "GET",
credentials: "include",
mode: "cors",
headers: {
"Accept": "application/json",
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(' '))
})
.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)
}
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(' '))
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)
}
loggedInText.textContent = `Logged in as: ${userName}`
container.appendChild(loggedInText)
}
})
})
}

0 comments on commit 6b52cdc

Please sign in to comment.