Skip to content

Commit 5b5852b

Browse files
committed
fix(davinci-client): return-url-from-external-idp
updating the api for the social login external idp method. Instead of redirecting for the application, we will return a url for the application to redirect to themselves.
1 parent 4ec5c25 commit 5b5852b

File tree

5 files changed

+63
-32
lines changed

5 files changed

+63
-32
lines changed

.changeset/clear-cars-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@forgerock/davinci-client': patch
3+
---
4+
5+
Return a url from the externalIdp function for app developers to use to redirect their url

e2e/davinci-app/components/social-login-button.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import type { IdpCollector } from '@forgerock/davinci-client/types';
99
export default function submitButtonComponent(
1010
formEl: HTMLFormElement,
1111
collector: IdpCollector,
12-
updater: () => void,
12+
url: string,
1313
) {
1414
const button = document.createElement('button');
1515
console.log('collector', collector);
1616
button.value = collector.output.label;
1717
button.innerHTML = collector.output.label;
18-
button.onclick = () => updater();
18+
button.onclick = () => {
19+
window.location.assign(url);
20+
};
1921

2022
formEl?.appendChild(button);
2123
}

e2e/davinci-app/main.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,18 @@ const urlParams = new URLSearchParams(window.location.search);
239239
);
240240
} else if (collector.type === 'IdpCollector') {
241241
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
242-
collector;
243-
socialLoginButtonComponent(formEl, collector, davinciClient.externalIdp(collector));
242+
const url = davinciClient.externalIdp(collector);
243+
if (typeof url === 'string') {
244+
socialLoginButtonComponent(formEl, collector, url);
245+
} else {
246+
const error = url;
247+
const errorDiv = formEl.querySelector('#error-div');
248+
if (errorDiv && clientInfo?.status === 'continue') {
249+
errorDiv.innerHTML = `
250+
<div>${error.error.message}</div>
251+
`;
252+
}
253+
}
244254
} else if (collector.type === 'FlowCollector') {
245255
flowLinkComponent(
246256
formEl, // You can ignore this; it's just for rendering

packages/davinci-client/src/lib/client.store.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,15 @@ import type {
3333
ObjectValueCollectors,
3434
PhoneNumberInputValue,
3535
} from './collector.types.js';
36-
import type { InitFlow, NodeStates, Updater, Validator } from './client.types.js';
36+
import type {
37+
InitFlow,
38+
InternalErrorResponse,
39+
NodeStates,
40+
Updater,
41+
Validator,
42+
} from './client.types.js';
3743
import { returnValidator } from './collector.utils.js';
38-
import { authorize } from './davinci.utils.js';
44+
import { returnRedirectUrlForSocialLogin } from './davinci.utils.js';
3945
import { StartNode } from './node.types.js';
4046

4147
/**
@@ -108,12 +114,16 @@ export async function davinci<ActionType extends ActionTypes = ActionTypes>({
108114
* @param collector IdpCollector
109115
* @returns {function}
110116
*/
111-
externalIdp: (collector: IdpCollector) => {
117+
externalIdp: (collector: IdpCollector): string | InternalErrorResponse => {
112118
const rootState: RootState = store.getState();
113119

114120
const serverSlice = nodeSlice.selectors.selectServer(rootState);
115121

116-
return () => authorize(serverSlice, collector, log);
122+
const result = returnRedirectUrlForSocialLogin(serverSlice, collector, log);
123+
if (typeof result == 'string') {
124+
return result;
125+
}
126+
return result;
117127
},
118128

119129
/**

packages/davinci-client/src/lib/davinci.utils.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -246,39 +246,43 @@ export function handleResponse(
246246
}
247247
}
248248

249-
export function authorize(
249+
export function returnRedirectUrlForSocialLogin(
250250
serverSlice: RootState['node']['server'],
251251
collector: IdpCollector,
252252
logger: ReturnType<typeof loggerFn>,
253-
): InternalErrorResponse | void {
253+
): InternalErrorResponse | string {
254254
if (serverSlice && '_links' in serverSlice) {
255255
const continueUrl = serverSlice._links?.['continue']?.href ?? null;
256256
if (continueUrl) {
257257
window.localStorage.setItem('continueUrl', continueUrl);
258258
if (collector.output.url) {
259-
window.location.assign(collector.output.url);
259+
return collector.output.url;
260+
} else {
261+
logger.error('No url found in collector, social login needs a url in the collector');
262+
return {
263+
error: {
264+
message:
265+
'No url found in collector, social login needs a url in the collector to navigate to',
266+
type: 'network_error',
267+
},
268+
type: 'internal_error',
269+
};
260270
}
261-
} else {
262-
logger.error('No url found in collector, social login needs a url in the collector');
263-
return {
264-
error: {
265-
message:
266-
'No url found in collector, social login needs a url in the collector to navigate to',
267-
type: 'network_error',
268-
},
269-
type: 'internal_error',
270-
};
271271
}
272-
logger.error(
273-
'No Continue Url found, social login needs a continue url to be saved in localStorage',
274-
);
275-
return {
276-
error: {
277-
message:
278-
'No Continue Url found, social login needs a continue url to be saved in localStorage',
279-
type: 'network_error',
280-
},
281-
type: 'internal_error',
282-
};
283272
}
273+
/**
274+
* If we have no continue url
275+
* we have to return an error
276+
**/
277+
logger.error(
278+
'No Continue Url found, social login needs a continue url to be saved in localStorage',
279+
);
280+
return {
281+
error: {
282+
message:
283+
'No Continue Url found, social login needs a continue url to be saved in localStorage',
284+
type: 'network_error',
285+
},
286+
type: 'internal_error',
287+
};
284288
}

0 commit comments

Comments
 (0)