In my Angular app i've implemented e-mail confirmation, so when a new user account is created, a confirmation e-mail will be sent to the registered e-mail address.
The user can log in unconfirmed, but they have access to limited functionality of the app until they visit the link in the confirmation e-mail (the confirmation link opens the application in a separate tab).
The relevant part of my AuthService
looks like this:
private readonly KEY_AUTH = '_auth';
private currentAuthSubject = new BehaviorSubject<Auth>(
JSON.parse(localStorage.getItem(this.KEY_AUTH)));
// My Components subscribe to this Observable to get notified on Auth changes
public currentAuth = this.currentAuthSubject.asObservable();
// This function is used to set the authentication object on login
// (or delete it on logout by calling setAuth(null))
setAuth(auth: Auth) {
if (auth) {
localStorage.setItem(this.KEY_AUTH, JSON.stringify(auth));
} else { localStorage.removeItem(this.KEY_AUTH); }
this.currentAuthSubject.next(auth);
}
// This function is called when the user is already logged in and being confirmed
// (by clicking the confirmation link)
setAuthConfirmed() {
const auth = this.currentAuthSubject.value;
if (auth) {
auth.confirmed = true;
localStorage.setItem(this.KEY_AUTH, JSON.stringify(auth));
this.currentAuthSubject.next(auth);
}
}
The confirmation basically works fine, but i have the following problematic case:
- In tab A the unconfirmed user is logged in to the application (limited functionality).
- In tab B they open their e-mail and click on the confirmation link.
- The app opens in tab C, the confirmation happens and the user has full functionality.
And here comes the problem:
In tab A the user still has the limited functionality, even when navigating between components that are subscribed to
currentAuth
.In this tab the
confirmed
field of theAuth
object is stillfalse
.This can be resolved only by refreshing the page.
Is there a way to update the auth state in tab A without having to manually refresh the whole page?
I'd really appreciate any advice.