64 lines
1.8 KiB
JavaScript
64 lines
1.8 KiB
JavaScript
const CONFIG = {
|
|
client_id: "{{ config.client_id }}",
|
|
redirect_uri: "{{ config.domain }}",
|
|
authorization_endpoint: "{{ config.gitea_host }}/login/oauth/authorize",
|
|
token_endpoint: "{{ config.gitea_host }}/login/oauth/access_token",
|
|
requested_scopes: "read:repository,write:repository"
|
|
};
|
|
|
|
var G_AUTH = window.localStorage.getItem("token") !== null;
|
|
|
|
document.on = document.addEventListener;
|
|
function sugar(obj) {
|
|
obj.on = obj.addEventListener;
|
|
obj.addClass = (x) => obj.classList.add(x);
|
|
obj.delClass = (x) => obj.classList.remove(x);
|
|
obj.hasClass = (x) => obj.classList.contains(x);
|
|
obj.setClass = (x, y) => obj.classList.toggle(x, y);
|
|
return obj;
|
|
}
|
|
|
|
function get(s) {
|
|
let x = [...document.querySelectorAll(s)].map(sugar);
|
|
return (x.length === 1) ? x[0] : x;
|
|
}
|
|
|
|
// Check if object is empty.
|
|
function isEmpty(obj) {
|
|
for (const prop in obj) {
|
|
if (Object.hasOwn(obj, prop)) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Returns error and resolves promise if necessary.
|
|
function getErr(f) {
|
|
return (err) => {
|
|
if (err instanceof Promise) {
|
|
return err.then(f);
|
|
} else {
|
|
return err;
|
|
}
|
|
}
|
|
}
|
|
|
|
get("#login").on("click", async (e) => {
|
|
e.preventDefault();
|
|
if (G_AUTH) {
|
|
window.localStorage.clear();
|
|
window.location = "/";
|
|
} else {
|
|
// Build the authorization URL
|
|
let url = CONFIG.authorization_endpoint
|
|
+ "?response_type=code"
|
|
+ "&client_id="+encodeURIComponent(CONFIG.client_id)
|
|
+ "&scope="+encodeURIComponent(CONFIG.requested_scopes)
|
|
+ "&redirect_uri="+encodeURIComponent(CONFIG.redirect_uri);
|
|
|
|
// Redirect to the authorization server
|
|
window.location = url;
|
|
}
|
|
});
|