Skip to content

Commit

Permalink
reduce duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
Mause committed Dec 11, 2023
1 parent ebc2239 commit 6caea6c
Showing 1 changed file with 17 additions and 42 deletions.
59 changes: 17 additions & 42 deletions app/services/github-app-auth.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,50 +163,40 @@ export class GitHubAppAuthStrategy<User> 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 {
Expand Down Expand Up @@ -239,28 +229,13 @@ export class GitHubAppAuthStrategy<User> 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)),
);
}
Expand Down

0 comments on commit 6caea6c

Please sign in to comment.