86 lines
2.4 KiB
JavaScript
86 lines
2.4 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"
|
|
};
|
|
|
|
const STATES = {
|
|
auth: "{{ state.auth }}",
|
|
error: "{{ state.error }}",
|
|
loading: "{{ state.loading }}",
|
|
repoSelected: "{{ state.repo_selected }}",
|
|
repoCreated: "{{ state.repo_created }}",
|
|
buttonOn: "{{ state.button_on }}",
|
|
featureOn: "{{ state.feature_on }}",
|
|
confirmOn: "{{ state.confirm_on }}",
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
|
|
// Parse a query string into an object
|
|
function parseQueryString(string) {
|
|
if(string == "") { return {}; }
|
|
var segments = string.split("&").map(s => s.split("=") );
|
|
var queryString = {};
|
|
segments.forEach(s => queryString[s[0]] = s[1]);
|
|
return queryString;
|
|
}
|
|
|
|
get("#login").on("click", async (e) => {
|
|
e.preventDefault();
|
|
if (G_AUTH) {
|
|
window.localStorage.clear();
|
|
document.cookie = "";
|
|
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;
|
|
}
|
|
});
|
|
|
|
var G_AUTH = window.localStorage.getItem("token") !== null;
|
|
get("body").setClass(STATES["auth"], G_AUTH);
|