Skip to content

Commit

Permalink
feat: add window config string (#552)
Browse files Browse the repository at this point in the history
* feature: adds window.open feature config

* feat: adds authClient.login usage description
  • Loading branch information
plitzenberger committed Apr 7, 2022
1 parent a091cb8 commit 54de54d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
21 changes: 18 additions & 3 deletions packages/auth-client/src/index.test.ts
Expand Up @@ -217,17 +217,32 @@ describe('Auth Client login', () => {
const client = await AuthClient.create();
// Try without #authorize hash.
await client.login({ identityProvider: 'http://localhost' });
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow', undefined);

// Try with #authorize hash.
global.open = jest.fn();
await client.login({ identityProvider: 'http://localhost#authorize' });
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith('http://localhost/#authorize', 'idpWindow', undefined);

// Default url
global.open = jest.fn();
await client.login();
expect(global.open).toBeCalledWith('https://identity.ic0.app/#authorize', 'idpWindow');
expect(global.open).toBeCalledWith(
'https://identity.ic0.app/#authorize',
'idpWindow',
undefined,
);

// Default custom window.open feature
global.open = jest.fn();
await client.login({
windowOpenerFeatures: 'toolbar=0,location=0,menubar=0',
});
expect(global.open).toBeCalledWith(
'https://identity.ic0.app/#authorize',
'idpWindow',
'toolbar=0,location=0,menubar=0',
);
});

it('should ignore authorize-ready events with bad origin', async () => {
Expand Down
19 changes: 16 additions & 3 deletions packages/auth-client/src/index.ts
@@ -1,6 +1,5 @@
/** @module AuthClient */
import {
Actor,
AnonymousIdentity,
DerEncodedPublicKey,
Identity,
Expand Down Expand Up @@ -64,6 +63,11 @@ export interface AuthClientLoginOptions {
* @default BigInt(8) hours * BigInt(3_600_000_000_000) nanoseconds
*/
maxTimeToLive?: bigint;
/**
* Auth Window feature config string
* @example "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100"
*/
windowOpenerFeatures?: string;
/**
* Callback once login has completed
*/
Expand Down Expand Up @@ -327,13 +331,15 @@ export class AuthClient {
* @param {AuthClientLoginOptions} options
* @param options.identityProvider Identity provider
* @param options.maxTimeToLive Expiration of the authentication in nanoseconds
* @param options.windowOpenerFeatures Configures the opened authentication window
* @param options.onSuccess Callback once login has completed
* @param options.onError Callback in case authentication fails
* @example
* const authClient = await AuthClient.create();
* authClient.login({
* identityProvider: 'http://<canisterID>.localhost:8000',
* maxTimeToLive: BigInt (7) * BigInt(24) * BigInt(3_600_000_000_000) // 1 week
* maxTimeToLive: BigInt (7) * BigInt(24) * BigInt(3_600_000_000_000), // 1 week
* windowOpenerFeatures: "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100",
* onSuccess: () => {
* console.log('Login Successful!');
* },
Expand All @@ -353,6 +359,11 @@ export class AuthClient {
* @default BigInt(8) hours * BigInt(3_600_000_000_000) nanoseconds
*/
maxTimeToLive?: bigint;
/**
* Auth Window feature config string
* @example "toolbar=0,location=0,menubar=0,width=500,height=500,left=100,top=100"
*/
windowOpenerFeatures?: string;
/**
* Callback once login has completed
*/
Expand Down Expand Up @@ -393,7 +404,9 @@ export class AuthClient {
window.addEventListener('message', this._eventHandler);

// Open a new window with the IDP provider.
this._idpWindow = window.open(identityProviderUrl.toString(), 'idpWindow') ?? undefined;
this._idpWindow =
window.open(identityProviderUrl.toString(), 'idpWindow', options?.windowOpenerFeatures) ??
undefined;

// Check if the _idpWindow is closed by user.
const checkInterruption = (): void => {
Expand Down

0 comments on commit 54de54d

Please sign in to comment.