Skip to content

Commit 80223d6

Browse files
authored
Align validateClaims to validateCustomClaims (#410)
1 parent 804b3e5 commit 80223d6

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

src/auth.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ export interface SignInCheckOptionsClaimsValidator extends SignInCheckOptionsBas
136136
export function useSigninCheck(
137137
options?: SignInCheckOptionsBasic | SignInCheckOptionsClaimsObject | SignInCheckOptionsClaimsValidator
138138
): ObservableStatus<SigninCheckResult> {
139-
// If both `requiredClaims` and `validateClaims` are provided, we won't know which one to use
140-
if (options?.hasOwnProperty('requiredClaims') && options?.hasOwnProperty('validateClaims')) {
141-
throw new Error('Cannot have both "requiredClaims" and "validateClaims". Use one or the other.');
139+
// If both `requiredClaims` and `validateCustomClaims` are provided, we won't know which one to use
140+
if (options?.hasOwnProperty('requiredClaims') && options?.hasOwnProperty('validateCustomClaims')) {
141+
throw new Error('Cannot have both "requiredClaims" and "validateCustomClaims". Use one or the other.');
142142
}
143143

144144
const auth = useAuth();
@@ -152,14 +152,14 @@ export function useSigninCheck(
152152
observableId = `${observableId}:requiredClaims:${JSON.stringify((options as SignInCheckOptionsClaimsObject).requiredClaims)}`;
153153
} else if (options?.hasOwnProperty('validateCustomClaims')) {
154154
// TODO(jamesdaniels): Check if stringifying this function breaks in IE11
155-
observableId = `${observableId}:validateClaims:${JSON.stringify((options as SignInCheckOptionsClaimsValidator).validateCustomClaims)}`;
155+
observableId = `${observableId}:validateCustomClaims:${JSON.stringify((options as SignInCheckOptionsClaimsValidator).validateCustomClaims)}`;
156156
}
157157

158158
const observable = user(auth).pipe(
159159
switchMap(user => {
160160
if (!user) {
161161
return of({ signedIn: false, hasRequiredClaims: false });
162-
} else if (options && (options.hasOwnProperty('requiredClaims') || options.hasOwnProperty('validateClaims'))) {
162+
} else if (options && (options.hasOwnProperty('requiredClaims') || options.hasOwnProperty('validateCustomClaims'))) {
163163
return from(user.getIdTokenResult(options?.forceRefresh ?? false)).pipe(
164164
map(idTokenResult => {
165165
let validator: ClaimsValidator;

test/auth.test.tsx

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,44 @@ describe('Authentication', () => {
197197
expect(result.current.data.errors as ClaimCheckErrors);
198198
});
199199

200-
it('accepts a custom claims validator', async () => {
200+
it('accepts a custom claims validator and returns invalid hasRequiredClaims state', async () => {
201+
const withClaimsCustomToken = {
202+
uid: 'aUserWithCustomClaims',
203+
claims: { someClaim: false, someOtherClaim: false }
204+
};
205+
206+
const claimsValidator: ClaimsValidator = userClaims => {
207+
const validClaimsSet = ['someClaim', 'someOtherClaim'];
208+
let hasAnyClaim = false;
209+
210+
for (const claim of validClaimsSet) {
211+
if (userClaims[claim] === true) {
212+
hasAnyClaim = true;
213+
break;
214+
}
215+
}
216+
217+
return {
218+
hasRequiredClaims: hasAnyClaim,
219+
errors: hasAnyClaim ? {} : validClaimsSet
220+
};
221+
};
222+
223+
const { result, waitFor: waitForHookCondition } = renderHook(() => useSigninCheck({ validateCustomClaims: claimsValidator }), {
224+
wrapper: Provider
225+
});
226+
227+
await hooksAct(async () => {
228+
await app.auth().signInWithCustomToken(JSON.stringify(withClaimsCustomToken));
229+
});
230+
231+
await waitForHookCondition(() => result.current.status === 'success');
232+
233+
expect(result.current.data.signedIn).toEqual(true);
234+
expect(result.current.data.hasRequiredClaims).toEqual(false);
235+
});
236+
237+
it('accepts a custom claims validator and returns valid hasRequiredClaims state', async () => {
201238
const withClaimsCustomToken = {
202239
uid: 'aUserWithCustomClaims',
203240
claims: { someClaim: true, someOtherClaim: false }

0 commit comments

Comments
 (0)