Unverified Commit 6ab4ecaa authored by Phillip Kuznetsov's avatar Phillip Kuznetsov Committed by Copybara
Browse files

Flatten callback code

Summary: cleanup callback code to be a bit more readable.

Test Plan: just reorg to be easier to read

Reviewers: michelle, nlanam

Reviewed By: nlanam

Differential Revision: https://phab.corp.pixielabs.ai/D8451

GitOrigin-RevId: c4c3fd1f96ab3ac55e3843ca3014521e45d78942
parent 42b9410f
Showing with 133 additions and 128 deletions
+133 -128
...@@ -103,148 +103,153 @@ export const AuthCallbackPage: React.FC = () => { ...@@ -103,148 +103,153 @@ export const AuthCallbackPage: React.FC = () => {
const [config, setConfig] = React.useState<CallbackConfig>(null); const [config, setConfig] = React.useState<CallbackConfig>(null);
const classes = useStyles(); const classes = useStyles();
React.useEffect(() => { const setErr = (errType: ErrorType, errMsg: string) => {
const setErr = (errType: ErrorType, errMsg: string) => { setConfig((c) => ({
setConfig((c) => ({ ...c,
...c, err: {
err: { errorType: errType,
errorType: errType, errMessage: errMsg,
errMessage: errMsg, },
}, loading: false,
loading: false, }));
})); };
};
const handleHTTPError = (err: AxiosError) => {
if (err.code === '401' || err.code === '403' || err.code === '404') {
setErr('auth', err.response.data);
} else {
setErr('internal', err.response.data);
}
};
const performSignup = async (accessToken: string) => {
try {
const response = await Axios.post('/api/auth/signup', { accessToken });
await trackAuthEvent('User signed up', response.data.userInfo.userID, response.data.userInfo.email);
return true;
} catch (err) {
analytics.track('User signup failed', { error: err.response.data });
handleHTTPError(err as AxiosError);
return false;
}
};
const performUILogin = async (accessToken: string, orgName: string) => { const handleHTTPError = (err: AxiosError) => {
try { if (err.code === '401' || err.code === '403' || err.code === '404') {
const response = await Axios.post('/api/auth/login', { setErr('auth', err.response.data);
accessToken, } else {
orgName, setErr('internal', err.response.data);
}); }
await trackAuthEvent('User logged in', response.data.userInfo.userID, response.data.userInfo.email); };
return true;
} catch (err) {
analytics.track('User signup failed', { error: err.response.data });
handleHTTPError(err as AxiosError);
return false;
}
};
const sendTokenToCLI = async (accessToken: string, redirectURI: string) => {
try {
const response = await redirectGet(redirectURI, { accessToken });
return response.status === 200 && response.data === 'OK';
} catch (error) {
handleHTTPError(error as AxiosError);
// If there's an error, we just return a failure.
return false;
}
};
const handleAccessToken = (accessToken: string) => {
const params = QueryString.parse(window.location.search.substr(1));
let mode: AuthCallbackMode;
switch (params.mode) {
case 'cli_get':
case 'cli_token':
case 'ui':
({ mode } = params);
break;
default:
mode = 'ui';
}
const location = params.location && String(params.location); const performSignup = async (accessToken: string) => {
const signup = !!params.signup; try {
const orgName = params.org_name && String(params.org_name); const response = await Axios.post('/api/auth/signup', { accessToken });
const redirectURI = params.redirect_uri && String(params.redirect_uri); await trackAuthEvent('User signed up', response.data.userInfo.userID, response.data.userInfo.email);
return true;
} catch (err) {
analytics.track('User signup failed', { error: err.response.data });
handleHTTPError(err as AxiosError);
return false;
}
};
setConfig({ const performUILogin = async (accessToken: string, orgName: string) => {
mode, try {
signup, const response = await Axios.post('/api/auth/login', {
token: accessToken, accessToken,
loading: true, orgName,
}); });
await trackAuthEvent('User logged in', response.data.userInfo.userID, response.data.userInfo.email);
return true;
} catch (err) {
analytics.track('User login failed', { error: err.response.data });
handleHTTPError(err as AxiosError);
return false;
}
};
const doAuth = async () => { const sendTokenToCLI = async (accessToken: string, redirectURI: string) => {
let signupSuccess = false; try {
let loginSuccess = false; const response = await redirectGet(redirectURI, { accessToken });
return response.status === 200 && response.data === 'OK';
} catch (error) {
handleHTTPError(error as AxiosError)
// If there's an error, we just return a failure.
return false;
}
};
const doAuth = async (
mode: AuthCallbackMode,
signup: boolean,
redirectURI: string,
location: string,
orgName: string,
accessToken: string,
) => {
let signupSuccess = false;
let loginSuccess = false;
if (signup) { if (signup) {
// We always need to perform signup, even if the mode is CLI. // We always need to perform signup, even if the mode is CLI.
signupSuccess = await performSignup(accessToken); signupSuccess = await performSignup(accessToken);
}
// eslint-disable-next-line default-case
switch (mode) {
case 'cli_get':
loginSuccess = await sendTokenToCLI(accessToken, redirectURI);
if (loginSuccess) {
setConfig((c) => ({
...c,
loading: false,
}));
RedirectUtils.redirect('/auth/cli-auth-complete', {});
return;
} }
// eslint-disable-next-line default-case
switch (mode) { // Don't fallback to manual auth if there is an actual
case 'cli_get': // authentication error.
loginSuccess = await sendTokenToCLI(accessToken, redirectURI); if (config.err?.errorType === 'auth') {
if (loginSuccess) { break;
setConfig((c) => ({
...c,
loading: false,
}));
RedirectUtils.redirect('/auth/cli-auth-complete', {});
return;
}
// Don't fallback to manual auth if there is an actual
// authentication error.
if (config.err?.errorType === 'auth') {
break;
}
mode = 'cli_token';
// If it fails, switch to token auth.
setConfig((c) => ({
...c,
mode,
}));
break;
case 'cli_token':
// Nothing to do, it will just render.
break;
case 'ui':
if (!signup) {
loginSuccess = await performUILogin(accessToken, orgName);
}
// We just need to redirect if in signup or login were successful since
// the cookies are installed.
if ((signup && signupSuccess) || loginSuccess) {
RedirectUtils.redirect(redirectURI || location || '/', {});
}
} }
// If it fails, switch to token auth.
setConfig((c) => ({ setConfig((c) => ({
...c, ...c,
loading: false, mode: 'cli_token',
})); }));
}; break;
case 'cli_token':
// Nothing to do, it will just render.
break;
case 'ui':
if (!signup) {
loginSuccess = await performUILogin(accessToken, orgName);
}
// We just need to redirect if in signup or login were successful since
// the cookies are installed.
if ((signup && signupSuccess) || loginSuccess) {
RedirectUtils.redirect(redirectURI || location || '/', {});
}
}
doAuth(); setConfig((c) => ({
}; ...c,
loading: false,
}));
};
GetOAuthProvider().handleToken().then((a: Token) => { const handleAccessToken = (accessToken: string) => {
handleAccessToken(a); const params = QueryString.parse(window.location.search.substr(1));
}).catch((err) => { let mode: AuthCallbackMode;
switch (params.mode) {
case 'cli_get':
case 'cli_token':
case 'ui':
({ mode } = params);
break;
default:
mode = 'ui';
}
const location = params.location && String(params.location);
const signup = !!params.signup;
const orgName = params.org_name && String(params.org_name);
const redirectURI = params.redirect_uri && String(params.redirect_uri);
setConfig({
mode,
signup,
token: accessToken,
loading: true,
});
doAuth(mode, signup, redirectURI, location, orgName, accessToken);
};
React.useEffect(() => {
GetOAuthProvider().handleToken().then(handleAccessToken).catch((err) => {
setErr('internal', `${err}`); setErr('internal', `${err}`);
}); });
}, []); }, []);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment