import { AngularFireAuth } from 'angularfire2/auth';
import * as firebase from 'firebase/app';
@Injectable()
export class GetRequestsProvider {
user: firebase.User;
userIdToken:string;
constructor(public http: HttpClient, public afAuth: AngularFireAuth, authservices: AuthService) {
afAuth.authState.subscribe(user => {
this.user = user;
});
}
getUserToken(){
this.afAuth.auth.onAuthStateChanged((user) => {
user.getIdToken().then((idToken)=>{
this.userIdToken = idToken;
console.log("Here I successfully retrieve the user token: ", this.userIdToken);
console.log(typeof(this.userIdToken)); // ‘userIdToken’ type is ‘string’
});
console.log("However, the variable ‘userIdToken’ remains undefined here: ", this.userIdToken);
}
}
}
I am in the process of implementing the getIdToken method from Firebase for authenticating users within my application.
The current issue I am encountering is that despite obtaining the user's token correctly, I am facing difficulty saving it to a variable or utilizing it in other parts of the codebase.