mirror of
https://github.com/tdurieux/anonymous_github.git
synced 2026-09-12 21:58:57 +02:00
feat: unify GitHub sign-in and connect OAuth for gists
This commit is contained in:
+28
-3
@@ -284,7 +284,7 @@ export const homeController = function (state, http, location, window, timeout)
|
||||
};
|
||||
// Signed-out visitors cannot open the dashboard; send them to sign in.
|
||||
state.featureHref = function (f) {
|
||||
return f.needsUser && !state.user ? "/github/login" : f.href;
|
||||
return f.needsUser && !state.user ? "/signin" : f.href;
|
||||
};
|
||||
state.featureTarget = function (f) {
|
||||
return f.needsUser && !state.user ? "_self" : f.target || undefined;
|
||||
@@ -890,10 +890,18 @@ export const anonymizeController = function (state, http, html, params, location
|
||||
state.sourceUrl = "";
|
||||
state.githubConnection = undefined;
|
||||
state.githubConnections = null;
|
||||
state.grantGitHubAccess = () => {
|
||||
state.gistOAuthRequired = false;
|
||||
const saveGitHubDraft = () => {
|
||||
const draft = {};
|
||||
for (const key of ["sourceUrl", "terms", "repoId", "pullRequestId", "gistId", "source", "options", "conference", "githubConnection"]) draft[key] = state[key];
|
||||
sessionStorage.setItem("github-access-draft", JSON.stringify({ path: location.path(), savedAt: Date.now(), draft }));
|
||||
};
|
||||
state.connectGistOAuth = () => {
|
||||
saveGitHubDraft();
|
||||
window.location.href = "/github/login?returnTo=" + encodeURIComponent(location.path());
|
||||
};
|
||||
state.grantGitHubAccess = () => {
|
||||
saveGitHubDraft();
|
||||
const returnTo = location.path();
|
||||
const repository = parseRepoFullName(state.sourceUrl) || "";
|
||||
const route = state.githubConnections?.appConnected ? "/github/app/install" : "/github/app/login";
|
||||
@@ -1110,7 +1118,7 @@ export const anonymizeController = function (state, http, html, params, location
|
||||
state.options.expirationDate = new Date(res.data.options.expirationDate);
|
||||
}
|
||||
if (await restoreGitHubDraft()) return;
|
||||
state.details = (await http.get(`/api/gist/source/${res.data.source.gistId}`)).data;
|
||||
await getGistDetails();
|
||||
|
||||
},
|
||||
() => { location.url("/404"); }
|
||||
@@ -1142,6 +1150,7 @@ export const anonymizeController = function (state, http, html, params, location
|
||||
state.readme = "";
|
||||
state.html_readme = "";
|
||||
state.detectedType = null;
|
||||
state.gistOAuthRequired = false;
|
||||
|
||||
let o;
|
||||
try {
|
||||
@@ -1449,12 +1458,23 @@ export const anonymizeController = function (state, http, html, params, location
|
||||
const o = parseGithubUrl(state.sourceUrl);
|
||||
try {
|
||||
resetValidity();
|
||||
state.gistOAuthRequired = false;
|
||||
if (state.githubConnections?.oauthConnected === false) {
|
||||
state.gistOAuthRequired = true;
|
||||
setValidity("sourceUrl", "missing", false);
|
||||
return;
|
||||
}
|
||||
const res = await http.get(`/api/gist/source/${o.gistId}`);
|
||||
state.details = res.data;
|
||||
if (!state.gistId) {
|
||||
state.gistId = "gist-" + o.gistId.substring(0, 6) + "-" + generateRandomId(4);
|
||||
}
|
||||
} catch (error) {
|
||||
if (error.data?.error === "github_oauth_required") {
|
||||
state.gistOAuthRequired = true;
|
||||
setValidity("sourceUrl", "missing", false);
|
||||
return;
|
||||
}
|
||||
if (error.data) {
|
||||
translate("ERRORS." + error.data.error).then((translation) => {
|
||||
state.addToast({ title: "Error", date: new Date(), body: translation });
|
||||
@@ -2832,3 +2852,8 @@ export const connectionsController = function (state, http) {
|
||||
};
|
||||
state.loadConnections();
|
||||
};
|
||||
|
||||
export const signinController = function (state, http) {
|
||||
state.accountRecovery = false;
|
||||
http.get("/github/account-recovery").then(res => { state.accountRecovery = res.data.required; }).catch(() => {});
|
||||
};
|
||||
|
||||
@@ -3,7 +3,7 @@ import * as admin from "./admin.js";
|
||||
|
||||
export const pageRoutes = [
|
||||
{path: "/connections", template: "partials/connections.htm", title: "GitHub connections – Anonymous GitHub", preserveExplorer: false, setup: (state, services) => pages.connectionsController(state, services.http)},
|
||||
{path: "/signin", template: "partials/signin.htm", title: "Sign in – Anonymous GitHub", preserveExplorer: false, setup: () => {}},
|
||||
{path: "/signin", template: "partials/signin.htm", title: "Sign in – Anonymous GitHub", preserveExplorer: false, setup: (state, services) => pages.signinController(state, services.http)},
|
||||
{path: "/", template: "partials/home.htm", title: "Anonymous GitHub – Share the code, not the author", preserveExplorer: false, setup: (state, services) => pages.homeController(state, services.http, services.location, services.window, services.timeout)},
|
||||
{path: "/dashboard", template: "partials/dashboard.htm", title: "Your anonymizations – Anonymous GitHub", preserveExplorer: false, setup: (state, services) => pages.unifiedDashboardController(state, services.http, services.location, services.promises, services.window, services.quotaService)},
|
||||
{"path":"/pr-dashboard","redirect":"/dashboard"},
|
||||
|
||||
Vendored
+21
-21
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user