0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor(ui): refactor social sign-in code flow (#969)

* refactor(ui): refactor social signin callback flow

refactor social signin callback flow

* fix(ui): change file structure

change file structure

* refactor(ui): refactor social sign-in code flow

refactor social sign-in code flow
This commit is contained in:
simeng-li 2022-05-27 12:46:49 +08:00 committed by GitHub
parent 7015d81378
commit e8ee5b1149
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 7 additions and 14 deletions

View file

@ -14,7 +14,7 @@ const useSocialCallbackHandler = () => {
const navigate = useNavigate(); const navigate = useNavigate();
const socialCallbackHandler = useCallback(() => { const socialCallbackHandler = useCallback(() => {
const { state, code, error, error_description } = parseQueryParameters(window.location.search); const { state, error, error_description } = parseQueryParameters(window.location.search);
const connectorId = parameters.connector; const connectorId = parameters.connector;
// Connector auth error // Connector auth error
@ -23,7 +23,7 @@ const useSocialCallbackHandler = () => {
} }
// Connector auth missing state // Connector auth missing state
if (!state || !code || !connectorId) { if (!state || !connectorId) {
setToast(t('error.missing_auth_data')); setToast(t('error.missing_auth_data'));
return; return;

View file

@ -41,7 +41,7 @@ const useSocialSignInListener = () => {
void asyncSignInWithSocial({ void asyncSignInWithSocial({
connectorId, connectorId,
code, code,
redirectUri: `${origin}/callback/${connectorId}`, // For validation use only redirectUri: `${window.location.origin}/callback/${connectorId}`, // For validation use only
}); });
}, },
[asyncSignInWithSocial] [asyncSignInWithSocial]
@ -59,19 +59,15 @@ const useSocialSignInListener = () => {
return; return;
} }
const { state, code } = parseQueryParameters(window.location.search); const { state, code, ...rest } = parseQueryParameters(window.location.search);
if (!state || !code) { if (!state || !stateValidation(state, parameters.connector)) {
return;
}
if (!stateValidation(state, parameters.connector)) {
setToast(t('error.invalid_connector_auth')); setToast(t('error.invalid_connector_auth'));
return; return;
} }
void signInWithSocialHandler(parameters.connector, state, code); void signInWithSocialHandler(parameters.connector, state, code ?? JSON.stringify(rest));
}, [parameters.connector, setToast, signInWithSocialHandler, t]); }, [parameters.connector, setToast, signInWithSocialHandler, t]);
}; };

View file

@ -12,7 +12,6 @@ describe('util methods', () => {
}); });
it('queryStringify', () => { it('queryStringify', () => {
expect(queryStringify('foo=test')).toEqual('foo=test');
expect(queryStringify(new URLSearchParams({ foo: 'test' }))).toEqual('foo=test'); expect(queryStringify(new URLSearchParams({ foo: 'test' }))).toEqual('foo=test');
}); });

View file

@ -10,9 +10,7 @@ export const parseQueryParameters = (parameters: string | URLSearchParams) => {
return Object.fromEntries(searchParameters.entries()); return Object.fromEntries(searchParameters.entries());
}; };
export const queryStringify = ( export const queryStringify = (parameters: URLSearchParams | Record<string, string>) => {
parameters: string | URLSearchParams | Record<string, string | string>
) => {
const searchParameters = const searchParameters =
parameters instanceof URLSearchParams ? parameters : new URLSearchParams(parameters); parameters instanceof URLSearchParams ? parameters : new URLSearchParams(parameters);