Skip to content

Commit

Permalink
Provide option to pass custom redirect url (#813)
Browse files Browse the repository at this point in the history
  • Loading branch information
poovamraj authored Nov 30, 2023
1 parent 42783ac commit 8101381
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 2 deletions.
6 changes: 6 additions & 0 deletions android/src/main/java/com/auth0/react/A0Auth0Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public void webAuth(String scheme, String redirectUri, String state, String nonc
if(leeway != 0) {
builder.withIdTokenVerificationLeeway(leeway);
}
if(redirectUri != null) {
builder.withRedirectUri(redirectUri);
}
builder.withParameters(cleanedParameters);
builder.start(reactContext.getCurrentActivity(), new com.auth0.android.callback.Callback<Credentials, AuthenticationException>() {
@Override
Expand All @@ -203,6 +206,9 @@ public void webAuthLogout(String scheme, boolean federated, String redirectUri,
if(federated) {
builder.withFederated();
}
if(redirectUri != null) {
builder.withReturnToUrl(redirectUri);
}
builder.start(reactContext.getCurrentActivity(), new com.auth0.android.callback.Callback<Void, AuthenticationException>() {
@Override
public void onSuccess(Void credentials) {
Expand Down
2 changes: 2 additions & 0 deletions src/internal-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type AgentParameters = {
export type AgentLogoutOptions = {
customScheme?: string;
federated?: boolean;
returnToUrl?: string;
useLegacyCallbackUrl?: boolean;
};

Expand All @@ -135,6 +136,7 @@ export interface AgentLoginOptions {
customScheme?: string;
leeway?: number;
ephemeralSession?: boolean;
redirectUrl?: string;
safariViewControllerPresentationStyle?: number;
additionalParameters?: { [key: string]: string };
useLegacyCallbackUrl?: boolean;
Expand Down
24 changes: 24 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ export interface WebAuthorizeParameters {
* The invitation URL for those users who have been invited to join a specific organization.
*/
invitationUrl?: string;
/**
* Specify a custom redirect URL to be used. Normally, you wouldn't need to call this method manually as the default value is autogenerated for you.
*
* If you are using this, ensure a proper redirect URL is constructed in the following format
* Android: {YOUR_APP_PACKAGE_NAME}.auth0://{AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/callback
* iOS: {PRODUCT_BUNDLE_IDENTIFIER}.auth0://{AUTH0_DOMAIN}/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback
*
* If you have `useLegacyCallbackUrl` set to true then the redirect URL should in the format
* Android: {YOUR_APP_PACKAGE_NAME}://{AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/callback
* iOS: {PRODUCT_BUNDLE_IDENTIFIER}://{AUTH0_DOMAIN}/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback
*/
redirectUrl?: string;
/**
* Any additional arbitrary parameters to send along in the URL.
*/
Expand Down Expand Up @@ -148,6 +160,18 @@ export interface ClearSessionParameters {
* @see https://auth0.com/docs/authenticate/login/logout/log-users-out-of-idps
*/
federated?: boolean;
/**
* Specify a custom redirect URL to be used. Normally, you wouldn't need to call this method manually as the default value is autogenerated for you.
*
* If you are using this, ensure a proper redirect URL is constructed in the following format
* Android: {YOUR_APP_PACKAGE_NAME}.auth0://{AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/callback
* iOS: {PRODUCT_BUNDLE_IDENTIFIER}.auth0://{AUTH0_DOMAIN}/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback
*
* If you have `useLegacyCallbackUrl` set to true then the redirect URL should in the format
* Android: {YOUR_APP_PACKAGE_NAME}://{AUTH0_DOMAIN}/android/{YOUR_APP_PACKAGE_NAME}/callback
* iOS: {PRODUCT_BUNDLE_IDENTIFIER}://{AUTH0_DOMAIN}/ios/{PRODUCT_BUNDLE_IDENTIFIER}/callback
*/
returnToUrl?: string;
}

/**
Expand Down
63 changes: 63 additions & 0 deletions src/webauth/__tests__/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ describe('Agent', () => {
{ test: 'test' }
);
});

it('should ensure login is called with proper parameters when redirect URL is set', async () => {
let domain = 'test.com';
let clientId = 'client id value';
const mock = jest
.spyOn(nativeUtils, '_ensureNativeModuleIsInitialized')
.mockImplementation(() => Promise.resolve(true));
const mockLogin = jest
.spyOn(NativeModules.A0Auth0, 'webAuth')
.mockImplementation(() => Promise.resolve(true));
await agent.login(
{
clientId: clientId,
domain: domain,
},
{
redirectUrl: 'redirect://redirect.com',
}
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'com.my.app.auth0',
'redirect://redirect.com',
undefined,
undefined,
undefined,
undefined,
undefined,
0,
undefined,
undefined,
0,
false,
99,
{}
);
});
});

describe('logout', () => {
Expand Down Expand Up @@ -167,6 +204,32 @@ describe('Agent', () => {
'test://test.com/ios/com.my.app/callback'
);
});

it('should ensure logout is called with proper parameters when redirect url is set', async () => {
let domain = 'test.com';
let clientId = 'client id value';
const mock = jest
.spyOn(nativeUtils, '_ensureNativeModuleIsInitialized')
.mockImplementation(() => Promise.resolve(true));
const mockLogin = jest
.spyOn(NativeModules.A0Auth0, 'webAuthLogout')
.mockImplementation(() => Promise.resolve(true));
await agent.logout(
{
clientId: clientId,
domain: domain,
},
{
returnToUrl: 'redirect://redirect.com',
}
);
expect(mock).toBeCalledWith(NativeModules.A0Auth0, clientId, domain);
expect(mockLogin).toBeCalledWith(
'com.my.app.auth0',
false,
'redirect://redirect.com'
);
});
});

describe('getScheme', () => {
Expand Down
2 changes: 2 additions & 0 deletions src/webauth/__tests__/webauth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('WebAuth', () => {
maxAge: 120,
organization: 'org',
invitationUrl: 'invitation url',
redirectUri: 'redirect://redirect.com',
additionalParameters: { test: 'test' },
};
let options = {
Expand Down Expand Up @@ -225,6 +226,7 @@ describe('WebAuth', () => {
it('should clearSession with provided parameters', async () => {
let parameters = {
federated: true,
returnToUrl: 'https://redirect.redirect.com',
};
let options = {
customScheme: 'scheme',
Expand Down
6 changes: 4 additions & 2 deletions src/webauth/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Agent {
options.useLegacyCallbackUrl ?? false,
options.customScheme
);
let redirectUri = this.callbackUri(parameters.domain, scheme);
let redirectUri =
options.redirectUrl ?? this.callbackUri(parameters.domain, scheme);
let credentials = await A0Auth0.webAuth(
scheme,
redirectUri,
Expand Down Expand Up @@ -93,7 +94,8 @@ class Agent {
options.useLegacyCallbackUrl ?? false,
options.customScheme
);
let redirectUri = this.callbackUri(parameters.domain, scheme);
let redirectUri =
options.returnToUrl ?? this.callbackUri(parameters.domain, scheme);
await _ensureNativeModuleIsInitialized(
NativeModules.A0Auth0,
parameters.clientId,
Expand Down

0 comments on commit 8101381

Please sign in to comment.