Skip to content

Commit

Permalink
remove usage of local storage for checking state in spa exp
Browse files Browse the repository at this point in the history
  • Loading branch information
gxueatlassian committed Aug 8, 2023
1 parent 468d460 commit b46a7b7
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions spa/src/services/oauth-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import Api from "../../api";
import { AxiosError } from "axios";
import { popup, reportError } from "../../utils";

const STATE_KEY = "oauth-localStorage-state";

let username: string | undefined;
let email: string | undefined;

let oauthState: string | undefined;

async function checkValidity(): Promise<boolean | AxiosError> {
if (!Api.token.hasGitHubToken()) return false;

Expand All @@ -25,7 +25,7 @@ async function checkValidity(): Promise<boolean | AxiosError> {
async function authenticateInGitHub(onWinClosed: () => void): Promise<void> {
const res = await Api.auth.generateOAuthUrl();
if (res.data.redirectUrl && res.data.state) {
window.localStorage.setItem(STATE_KEY, res.data.state);
oauthState = res.data.state;
const win = popup(res.data.redirectUrl);
if (win) {
const winCloseCheckHandler = setInterval(() => {
Expand All @@ -39,10 +39,12 @@ async function authenticateInGitHub(onWinClosed: () => void): Promise<void> {
}

async function finishOAuthFlow(code: string, state: string): Promise<boolean | AxiosError> {

if (!code && !state) return false;

const prevState = window.localStorage.getItem(STATE_KEY);
window.localStorage.removeItem(STATE_KEY);
const prevState = oauthState;
oauthState = undefined;

if (state !== prevState) return false;

try {
Expand Down

0 comments on commit b46a7b7

Please sign in to comment.