Refactor: encode state Parameters as URLSearchParams instead of JSON

This commit is contained in:
Govindarajan Nagarajan
2020-08-04 18:42:16 +02:00
parent 2d20cf389c
commit cb80b2b4f8
+10 -4
View File
@@ -42,10 +42,12 @@ export type Options = {
const readState = (stateString: string): OAuthState => {
try {
const state = JSON.parse(decodeURIComponent(stateString));
const state = Object.fromEntries(
new URLSearchParams(decodeURIComponent(stateString)),
);
return {
nonce: state.nonce,
env: state.env,
nonce: state.nonce ?? undefined,
env: state.env ?? undefined,
};
} catch (e) {
return {
@@ -56,7 +58,11 @@ const readState = (stateString: string): OAuthState => {
};
export const encodeState = (state: OAuthState): string => {
return encodeURIComponent(JSON.stringify(state));
const searchParams = new URLSearchParams();
searchParams.append('nonce', state.nonce ?? '');
searchParams.append('env', state.env ?? '');
return encodeURIComponent(searchParams.toString());
};
/*