AppFlowy-Cloud/admin_frontend/templates/login.html

91 lines
2.8 KiB
HTML

<!doctype html>
<html lang="en">
<body>
<h1>Admin Login</h1>
<form id="loginForm">
<table>
<tr>
<td>
<label for="email">Email:</label>
</td>
<td>
<input type="text" id="email" name="email" required />
</td>
</tr>
<tr>
<td>
<label for="password">Password:</label>
</td>
<td>
<input type="password" id="password" name="password" required />
</td>
</tr>
</table>
<button type="button" id="submitBtn">Submit</button>
</form>
<div id="response"></div>
<div id="oauth2Login">
<p>Or login with:</p>
<a href="/gotrue/authorize?provider=google&redirect_to=/web/login">Google</a>
</div>
<script>
document
.getElementById("submitBtn")
.addEventListener("click", function () {
var data = {
email: document.getElementById("email").value,
password: document.getElementById("password").value,
};
fetch("/web-api/login", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
credentials: "same-origin",
})
.then((response) => {
if (!response.ok) {
// If HTTP status code is not OK, throw an error with the status text
throw Error(response.statusText);
}
window.location.href = "/web/home";
})
.catch((error) => {
console.error(`Error:, ${error}`);
document.getElementById(
"response",
).innerText = `Login failed: ${error.message}`;
});
});
if (window.location.hash) {
// OAuth
// Extract data from the URL fragment
const fragmentData = window.location.hash.substring(1); // Remove the leading #
const fragmentParams = new URLSearchParams(fragmentData); // Parse the fragment data as a URLSearchParams object
const refreshToken = fragmentParams.get("refresh_token"); // Extract the refresh_token
fetch(`/web-api/login_refresh/${refreshToken}`, {
// Login in via refresh_token
method: "POST",
})
.then((response) => {
if (!response.ok) {
// If HTTP status code is not OK, throw an error with the status text
throw Error(response.statusText);
}
window.location.href = "/web/home";
})
.catch((error) => {
console.error(`Error:, ${error}`);
document.getElementById(
"response",
).innerText = `OAuth Login failed: ${error.message}`;
});
}
</script>
</body>
</html>