Skip to content

Commit 1d3dff2

Browse files
committed
Allow enabling of anonymous provider via tenant configuration
1 parent 224f65f commit 1d3dff2

File tree

5 files changed

+60
-0
lines changed

5 files changed

+60
-0
lines changed

src/auth/tenant.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ import {
2424
export interface TenantOptions {
2525
displayName?: string;
2626
emailSignInConfig?: EmailSignInProviderConfig;
27+
anonymousSignInEnabled?: boolean;
2728
}
2829

2930
/** The corresponding server side representation of a TenantOptions object. */
3031
export interface TenantOptionsServerRequest extends EmailSignInConfigServerRequest {
3132
displayName?: string;
33+
enableAnonymousUser?: boolean;
3234
}
3335

3436
/** The tenant server response interface. */
@@ -37,6 +39,7 @@ export interface TenantServerResponse {
3739
displayName?: string;
3840
allowPasswordSignup?: boolean;
3941
enableEmailLinkSignin?: boolean;
42+
enableAnonymousUser?: boolean;
4043
}
4144

4245
/** The interface representing the listTenant API response. */
@@ -53,6 +56,7 @@ export class Tenant {
5356
public readonly tenantId: string;
5457
public readonly displayName?: string;
5558
public readonly emailSignInConfig?: EmailSignInConfig;
59+
public readonly anonymousSignInEnabled?: boolean;
5660

5761
/**
5862
* Builds the corresponding server request for a TenantOptions object.
@@ -71,6 +75,9 @@ export class Tenant {
7175
if (typeof tenantOptions.displayName !== 'undefined') {
7276
request.displayName = tenantOptions.displayName;
7377
}
78+
if (typeof tenantOptions.anonymousSignInEnabled !== 'undefined') {
79+
request.enableAnonymousUser = tenantOptions.anonymousSignInEnabled;
80+
}
7481
return request;
7582
}
7683

@@ -99,6 +106,7 @@ export class Tenant {
99106
const validKeys = {
100107
displayName: true,
101108
emailSignInConfig: true,
109+
anonymousSignInEnabled: true,
102110
};
103111
const label = createRequest ? 'CreateTenantRequest' : 'UpdateTenantRequest';
104112
if (!validator.isNonNullObject(request)) {
@@ -155,6 +163,7 @@ export class Tenant {
155163
allowPasswordSignup: false,
156164
});
157165
}
166+
this.anonymousSignInEnabled = !!response.enableAnonymousUser;
158167
}
159168

160169
/** @return {object} The plain object representation of the tenant. */
@@ -163,6 +172,7 @@ export class Tenant {
163172
tenantId: this.tenantId,
164173
displayName: this.displayName,
165174
emailSignInConfig: this.emailSignInConfig && this.emailSignInConfig.toJSON(),
175+
anonymousSignInEnabled: this.anonymousSignInEnabled,
166176
};
167177
}
168178
}

src/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,11 @@ declare namespace admin.auth {
11501150
passwordRequired?: boolean;
11511151
};
11521152

1153+
/**
1154+
* Whether anonymous provider is enabled.
1155+
*/
1156+
anonymousSignInEnabled?: boolean;
1157+
11531158
/**
11541159
* @return A JSON-serializable representation of this object.
11551160
*/
@@ -1182,6 +1187,11 @@ declare namespace admin.auth {
11821187
*/
11831188
passwordRequired?: boolean;
11841189
};
1190+
1191+
/**
1192+
* Whether anonymous provider is enabled.
1193+
*/
1194+
anonymousSignInEnabled?: boolean;
11851195
}
11861196

11871197
/**

test/integration/auth.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,20 +510,23 @@ describe('admin.auth', () => {
510510
enabled: true,
511511
passwordRequired: true,
512512
},
513+
anonymousSignInEnabled: false,
513514
};
514515
const expectedUpdatedTenant: any = {
515516
displayName: 'testTenantUpdated',
516517
emailSignInConfig: {
517518
enabled: false,
518519
passwordRequired: true,
519520
},
521+
anonymousSignInEnabled: false,
520522
};
521523
const expectedUpdatedTenant2: any = {
522524
displayName: 'testTenantUpdated',
523525
emailSignInConfig: {
524526
enabled: true,
525527
passwordRequired: false,
526528
},
529+
anonymousSignInEnabled: false,
527530
};
528531

529532
// https://mochajs.org/
@@ -562,6 +565,20 @@ describe('admin.auth', () => {
562565
});
563566
});
564567

568+
it('createTenant() can enable anonymous users', async () => {
569+
const tenant = await admin.auth().tenantManager().createTenant({
570+
displayName: 'testTenantWithAnon',
571+
emailSignInConfig: {
572+
enabled: false,
573+
passwordRequired: true,
574+
},
575+
anonymousSignInEnabled: true,
576+
});
577+
createdTenants.push(tenant.tenantId);
578+
579+
expect(tenant.anonymousSignInEnabled).to.be.true;
580+
});
581+
565582
// Sanity check user management + email link generation + custom attribute APIs.
566583
// TODO: Confirm behavior in client SDK when it starts supporting it.
567584
describe('supports user management, email link generation, custom attribute and token revocation APIs', () => {
@@ -900,6 +917,25 @@ describe('admin.auth', () => {
900917
});
901918
});
902919

920+
it('updateTenant() should be able to enable/disable anon provider', async () => {
921+
const tenantManager = admin.auth().tenantManager();
922+
let tenant = await tenantManager.createTenant({
923+
displayName: 'testTenantUpdateAnon',
924+
});
925+
createdTenants.push(tenant.tenantId);
926+
expect(tenant.anonymousSignInEnabled).to.be.false;
927+
928+
tenant = await tenantManager.updateTenant(tenant.tenantId, {
929+
anonymousSignInEnabled: true,
930+
});
931+
expect(tenant.anonymousSignInEnabled).to.be.true;
932+
933+
tenant = await tenantManager.updateTenant(tenant.tenantId, {
934+
anonymousSignInEnabled: false,
935+
});
936+
expect(tenant.anonymousSignInEnabled).to.be.false;
937+
});
938+
903939
it('listTenants() should resolve with expected number of tenants', () => {
904940
const allTenantIds: string[] = [];
905941
const tenantOptions2 = deepCopy(tenantOptions);

test/unit/auth/tenant-manager.spec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe('TenantManager', () => {
4747
displayName: 'TENANT-DISPLAY-NAME',
4848
allowPasswordSignup: true,
4949
enableEmailLinkSignin: false,
50+
enableAnonymousUser: true,
5051
};
5152

5253
before(() => {
@@ -383,6 +384,7 @@ describe('TenantManager', () => {
383384
enabled: true,
384385
passwordRequired: true,
385386
},
387+
anonymousSignInEnabled: true,
386388
};
387389
const expectedTenant = new Tenant(GET_TENANT_RESPONSE);
388390
const expectedError = new FirebaseAuthError(
@@ -475,6 +477,7 @@ describe('TenantManager', () => {
475477
enabled: true,
476478
passwordRequired: true,
477479
},
480+
anonymousSignInEnabled: true,
478481
};
479482
const expectedTenant = new Tenant(GET_TENANT_RESPONSE);
480483
const expectedError = new FirebaseAuthError(

test/unit/auth/tenant.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ describe('Tenant', () => {
233233
enabled: true,
234234
passwordRequired: false,
235235
},
236+
anonymousSignInEnabled: false,
236237
});
237238
});
238239
});

0 commit comments

Comments
 (0)