Skip to content

Commit 9a03632

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 9a03632

File tree

5 files changed

+62
-31
lines changed

5 files changed

+62
-31
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: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,28 @@
55
* of the MIT license. See the LICENSE file for details.
66
*/
77
import type { IdpCollector } from '@forgerock/davinci-client/types';
8+
import { InternalErrorResponse } from 'packages/davinci-client/src/lib/client.types.js';
89

910
export default function submitButtonComponent(
1011
formEl: HTMLFormElement,
1112
collector: IdpCollector,
12-
updater: () => void,
13+
updater: () => string | InternalErrorResponse,
1314
) {
1415
const button = document.createElement('button');
1516
console.log('collector', collector);
1617
button.value = collector.output.label;
1718
button.innerHTML = collector.output.label;
18-
button.onclick = () => updater();
19+
button.onclick = () => {
20+
const url = updater();
21+
if (typeof url === 'string') {
22+
window.location.assign(url);
23+
} else {
24+
/**
25+
* this is an error now
26+
**/
27+
console.error(url);
28+
}
29+
};
1930

2031
formEl?.appendChild(button);
2132
}

e2e/davinci-app/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,6 @@ 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;
243242
socialLoginButtonComponent(formEl, collector, davinciClient.externalIdp(collector));
244243
} else if (collector.type === 'FlowCollector') {
245244
flowLinkComponent(

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ 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';
3944
import { StartNode } from './node.types.js';
45+
import { returnRedirectUrlForSocialLogin } from './davinci.utils.js';
4046

4147
/**
4248
* Create a client function that returns a set of methods
@@ -108,12 +114,18 @@ 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+
return () => {
123+
const result = returnRedirectUrlForSocialLogin(serverSlice, collector, log);
124+
if (typeof result == 'string') {
125+
return result;
126+
}
127+
return result;
128+
};
117129
},
118130

119131
/**

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)