Skip to content

Commit

Permalink
feat(clerk-js): Introduce UserSettings.instanceIsPasswordBased
Browse files Browse the repository at this point in the history
  • Loading branch information
nikosdouvlis committed Feb 23, 2022
1 parent 111b993 commit f72a555
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
25 changes: 24 additions & 1 deletion packages/clerk-js/src/core/resources/UserSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,30 @@ describe('UserSettings', () => {
expect(res).toEqual(['web3_metamask_signature']);
});

it('returns enabled social provier strategies', function () {
it('returns if the instance is passwordless or password-based', function () {
let sut = new UserSettings({
attributes: {
password: {
enabled: true,
required: true,
},
},
} as any as UserSettingsJSON);

expect(sut.instanceIsPasswordBased).toEqual(true);

sut = new UserSettings({
attributes: {
password: {
enabled: true,
required: false,
},
},
} as any as UserSettingsJSON);
expect(sut.instanceIsPasswordBased).toEqual(false);
});

it('returns enabled social provider strategies', function () {
const sut = new UserSettings({
social: {
oauth_google: {
Expand Down
31 changes: 11 additions & 20 deletions packages/clerk-js/src/core/resources/UserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
Web3Strategy,
} from '@clerk/types';

import {BaseResource} from './internal';
import { BaseResource } from './internal';

/**
* @internal
Expand All @@ -23,42 +23,35 @@ export class UserSettings extends BaseResource implements UserSettingsResource {

socialProviderStrategies: OAuthStrategy[] = [];
web3FirstFactors: Web3Strategy[] = [];
enabledFirstFactorIdentifiers: Array<
keyof UserSettingsResource['attributes']
> = [];
enabledFirstFactorIdentifiers: Array<keyof UserSettingsResource['attributes']> = [];

public constructor(data: UserSettingsJSON) {
super();
this.fromJSON(data);
}

get instanceIsPasswordBased() {
return this.attributes.password.enabled && this.attributes.password.required;
}

protected fromJSON(data: UserSettingsJSON): this {
this.social = data.social;
this.attributes = data.attributes;
this.signIn = data.sign_in;
this.signUp = data.sign_up;
this.socialProviderStrategies = this.getSocialProviderStrategies(
data.social,
);
this.socialProviderStrategies = this.getSocialProviderStrategies(data.social);
this.web3FirstFactors = this.getWeb3FirstFactors(data.attributes);
this.enabledFirstFactorIdentifiers = this.getEnabledFirstFactorIdentifiers(
data.attributes,
);
this.enabledFirstFactorIdentifiers = this.getEnabledFirstFactorIdentifiers(data.attributes);
return this;
}

private getEnabledFirstFactorIdentifiers(
attributes: Attributes,
): Array<keyof UserSettingsResource['attributes']> {
private getEnabledFirstFactorIdentifiers(attributes: Attributes): Array<keyof UserSettingsResource['attributes']> {
if (!attributes) {
return [];
}

return Object.entries(attributes)
.filter(
([name, attr]) =>
attr.used_for_first_factor && !name.startsWith('web3'),
)
.filter(([name, attr]) => attr.used_for_first_factor && !name.startsWith('web3'))
.map(([name]) => name) as Array<keyof UserSettingsResource['attributes']>;
}

Expand All @@ -68,9 +61,7 @@ export class UserSettings extends BaseResource implements UserSettingsResource {
}

return Object.entries(attributes)
.filter(
([name, attr]) => attr.used_for_first_factor && name.startsWith('web3'),
)
.filter(([name, attr]) => attr.used_for_first_factor && name.startsWith('web3'))
.map(([, desc]) => desc.first_factors)
.flat() as any as Web3Strategy[];
}
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ export interface UserSettingsResource extends ClerkResource {
socialProviderStrategies: OAuthStrategy[];
web3FirstFactors: Web3Strategy[];
enabledFirstFactorIdentifiers: Attribute[];
instanceIsPasswordBased: boolean;
}

0 comments on commit f72a555

Please sign in to comment.