From 6caea6c2580c8943bc2d67c276fcea9aa2bb7b27 Mon Sep 17 00:00:00 2001 From: Elliana May Date: Mon, 11 Dec 2023 02:26:48 +0800 Subject: [PATCH] reduce duplication --- app/services/github-app-auth.server.ts | 59 ++++++++------------------ 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/app/services/github-app-auth.server.ts b/app/services/github-app-auth.server.ts index 3a8f71991..b7da23979 100644 --- a/app/services/github-app-auth.server.ts +++ b/app/services/github-app-auth.server.ts @@ -163,50 +163,40 @@ export class GitHubAppAuthStrategy extends Strategy< let stateUrl = url.searchParams.get("state"); debug("State from URL", stateUrl); - if (!stateUrl) { + + const mkError = async ( + message: string, + error: Error = new Error(message), + ) => { return await this.failure( - "Missing state on URL.", + message, request, sessionStorage, options, - new Error("Missing state on URL."), + error, ); + }; + + if (!stateUrl) { + return await mkError("Missing state on URL."); } let stateSession = session.get(this.sessionStateKey); debug("State from session", stateSession); if (!stateSession) { - return await this.failure( - "Missing state on session.", - request, - sessionStorage, - options, - new Error("Missing state on session."), - ); + return await mkError("Missing state on session."); } if (stateSession === stateUrl) { debug("State is valid"); session.unset(this.sessionStateKey); } else { - return await this.failure( - "State doesn't match.", - request, - sessionStorage, - options, - new Error("State doesn't match."), - ); + return await mkError("State doesn't match."); } let code = url.searchParams.get("code"); if (!code) { - return await this.failure( - "Missing code.", - request, - sessionStorage, - options, - new Error("Missing code."), - ); + return await mkError("Missing code."); } try { @@ -239,28 +229,13 @@ export class GitHubAppAuthStrategy extends Strategy< // Allow responses to pass-through if (error instanceof Response) throw error; if (error instanceof Error) { - return await this.failure( - error.message, - request, - sessionStorage, - options, - error, - ); + return await mkError(error.message, error); } if (typeof error === "string") { - return await this.failure( - error, - request, - sessionStorage, - options, - new Error(error), - ); + return await mkError(error); } - return await this.failure( + return await mkError( "Unknown error", - request, - sessionStorage, - options, new Error(JSON.stringify(error, null, 2)), ); }