В моем приложении при регистрации нового пользователя мне нужно убедиться, что перед переходом к маршруту он получает значение, которое мне нужно внутри claims
при вызове метода getIdTokenResult(true)
.
ЯПередав параметр forceRefresh
в true, но я не могу обновить customClaims , мне нужно выйти из системы и войти в систему, чтобы получить обновленные данные.
Мне нужно убедиться, чтовсякий раз, когда пользователь регистрируется, он ожидает возврата токена с обновленными утверждениями, прежде чем установить состояние и перейти к главному экрану.
private _setUserControlAccess = firebase.functions()
.httpsCallable('setUserControlAccess')
@Effect()
signin$: Observable<Action> = this._actions.pipe(
ofType(authActions.SIGNIN),
map((action: authActions.Signin) => action.payload),
switchMap(user => {
this._matSnackbar.open('Efetuando o registro...', '', {
horizontalPosition: this._horizontalPosition,
verticalPosition: this._verticalPosition,
});
return from(this._angularFireAuth.auth
.createUserAndRetrieveDataWithEmailAndPassword(user.email, user.password)).pipe(
switchMap(response => this._setUserConfigurations(response, user)),
//filter() Add filter here? to check token.claims.firestoreCollection exists?
map(([_, token, user]) => {
const authStateObject = this._getAuthStateObject(token, user);
// before setting the state, I need to ensure that token.claims is updated.
return new authActions.Authenticated(authStateObject);
}),
tap(() => this._router.navigate([''])),
catchError((error) => of(new authActions.AuthError({ error: error })))
)
})
)
private _setUserConfigurations(
response: firebase.auth.UserCredential,
user: any
) {
return forkJoin([
from(response.user.updateProfile({
displayName: user.nome.trim(),
photoURL: 'assets/images/avatars/profile.jpg'
})),
from(response.user.getIdTokenResult(true)),
from(of(response.user)),
from(this._setUserControlAccess({
firestoreCollection: response.user.uid,
admin: true,
assinante: true,
profissional: true,
email: response.user.email
}))
]);
}
private _getAuthStateObject(
token: firebase.auth.IdTokenResult,
user: firebase.User
) {
const authStateObject = {
admin: token.claims.admin,
profissional: token.claims.profissional,
assinante: token.claims.assinante,
firestoreCollection: token.claims.firestoreCollection,
user: user
}
return authStateObject;
}
В облачных функциях:
export const setUserControlAccess = functions.https.onCall(async (data, context) => {
try {
const customUserClaims = {
admin: data.admin,
assinante: data.assinante,
profissional: data.profissional,
firestoreCollection: data.firestoreCollection
};
const user = await admin.auth().getUserByEmail(data.email);
await authentication.setCustomUserClaims(user.uid, customUserClaims);
} catch (error) {
console.error(error);
return error;
}
});