Skip to content

Commit 51fee7a

Browse files
fix: address review feedback on MFA verify examples and tests
1 parent 39dc67f commit 51fee7a

2 files changed

Lines changed: 51 additions & 10 deletions

File tree

EXAMPLES.md

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,35 +1223,52 @@ export class ChallengeComponent {
12231223
12241224
### Verifying Challenges
12251225
1226-
> [!IMPORTANT] > `verify()` does not update Angular auth state (`isAuthenticated$`, `user$`). Call `getAccessTokenSilently()` after a successful verification to reflect the new session in the UI.
1226+
> [!IMPORTANT] > `verify()` does not update Angular auth state (`isAuthenticated$`, `user$`). Always chain `getAccessTokenSilently()` after a successful verification to reflect the new session in the UI.
12271227
12281228
```ts
12291229
import { Component } from '@angular/core';
12301230
import { AuthService } from '@auth0/auth0-angular';
1231+
import { switchMap, tap } from 'rxjs';
12311232

12321233
@Component({ selector: 'app-verify', template: '' })
12331234
export class VerifyComponent {
12341235
constructor(private auth: AuthService) {}
12351236

12361237
// Verify with OTP code (TOTP authenticator app)
12371238
verifyOtp(mfaToken: string, otp: string) {
1238-
this.auth.mfa.verify({ mfaToken, otp }).subscribe((tokens) => {
1239-
console.log('Access token:', tokens.access_token);
1240-
});
1239+
this.auth.mfa
1240+
.verify({ mfaToken, otp })
1241+
.pipe(
1242+
switchMap(() => this.auth.getAccessTokenSilently()) // refresh isAuthenticated$, user$
1243+
)
1244+
.subscribe();
12411245
}
12421246

12431247
// Verify with OOB code (SMS / Voice / Email / Push)
12441248
verifyOob(mfaToken: string, oobCode: string, bindingCode?: string) {
1245-
this.auth.mfa.verify({ mfaToken, oobCode, bindingCode }).subscribe((tokens) => {
1246-
console.log('Access token:', tokens.access_token);
1247-
});
1249+
this.auth.mfa
1250+
.verify({ mfaToken, oobCode, bindingCode })
1251+
.pipe(
1252+
switchMap(() => this.auth.getAccessTokenSilently()) // refresh isAuthenticated$, user$
1253+
)
1254+
.subscribe();
12481255
}
12491256

12501257
// Verify with recovery code (fallback for any authenticator)
1258+
// When a recovery code is consumed, Auth0 may return a replacement recovery_code
1259+
// in the response. Prompt the user to save it — losing the new code locks them out.
12511260
verifyRecoveryCode(mfaToken: string, recoveryCode: string) {
1252-
this.auth.mfa.verify({ mfaToken, recoveryCode }).subscribe((tokens) => {
1253-
console.log('Access token:', tokens.access_token);
1254-
});
1261+
this.auth.mfa
1262+
.verify({ mfaToken, recoveryCode })
1263+
.pipe(
1264+
tap((tokens) => {
1265+
if (tokens.recovery_code) {
1266+
console.warn('Save your new recovery code:', tokens.recovery_code);
1267+
}
1268+
}),
1269+
switchMap(() => this.auth.getAccessTokenSilently()) // refresh isAuthenticated$, user$
1270+
)
1271+
.subscribe();
12551272
}
12561273
}
12571274
```

projects/auth0-angular/src/lib/auth.service.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,30 @@ describe('AuthService', () => {
15661566
},
15671567
});
15681568
});
1569+
1570+
it('should not update isAuthenticated$ or user$ after a successful verify', (done) => {
1571+
// verify() intentionally does not update Angular auth state — callers must
1572+
// follow up with getAccessTokenSilently() to reflect the new MFA session.
1573+
const service = createService();
1574+
let isAuthEmissions = 0;
1575+
let userEmissions = 0;
1576+
1577+
service.isAuthenticated$.subscribe(() => isAuthEmissions++);
1578+
service.user$.subscribe(() => userEmissions++);
1579+
1580+
loaded(service)
1581+
.pipe(
1582+
mergeMap(() =>
1583+
service.mfa.verify({ mfaToken: '__mfa_token__', otp: '123456' })
1584+
),
1585+
delay(0)
1586+
)
1587+
.subscribe(() => {
1588+
expect(isAuthEmissions).toBe(1);
1589+
expect(userEmissions).toBe(1);
1590+
done();
1591+
});
1592+
});
15691593
});
15701594
});
15711595
});

0 commit comments

Comments
 (0)