I am using simple email/password and google signin (firebase) login authentication in my Angular project. I want to retrieve User Id (UID) of the user who is currently logged in the product. Is there a way angular provides to retrieve the UID?
this.authenticationService.SignUp(this.institutes.email,"123456");
console.log(this.af.auth.currentUser.uid);
I am using simple email/password and google signin (firebase) login authentication in my Angular project. I want to retrieve User Id (UID) of the user who is currently logged in the product. Is there a way angular provides to retrieve the UID?
this.authenticationService.SignUp(this.institutes.email,"123456");
console.log(this.af.auth.currentUser.uid);
Authentication Script:
SignUp(email: string, password: string) {
this.angularFireAuth
.auth
.createUserWithEmailAndPassword(email, password)
.then(res => {
console.log('You are Successfully signed up!', res);
this.angularFireAuth
.auth
.signInWithEmailAndPassword(email, password)
.then(res => {
console.log('You are Successfully logged in!');
return 1;
})
.catch(err => {
console.log('Something is wrong:',err.message);
});
})
.catch(error => {
console.log('Something is wrong:', error.message);
});
}
I have signed up a new account and logged in the user at the same time. I tried to retreive UID of the user after logging in, but I am receiving the folloing error: Error:Object is possibly 'null'. Error is on line: console.log(this.af.auth.currentUser.uid);
Share Improve this question edited Nov 19, 2019 at 7:23 Akhilesh Pothuri asked Nov 18, 2019 at 20:32 Akhilesh PothuriAkhilesh Pothuri 2252 gold badges7 silver badges20 bronze badges3 Answers
Reset to default 3To retrieve the uid, you can use AngularFire and do the following:
export class AppComponent {
constructor(public afAuth: AngularFireAuth) {
}
getUid() {
this.afAuth.auth.currentUser.uid;
}
}
You can check here for more information:
https://github./angular/angularfire/blob/master/docs/auth/getting-started.md
Peter answered how to get the UID of the logged-in user when you use AngularFire2. If you're not using AngularFire (or even if you are), you can get the UID of the current user with:
firebase.auth().currentUser.uid
This is how you can retrieve the user id at the end of 2021.
import { AngularFireAuth } from "@angular/fire/auth";
constructor
// paramenters
(
public angularFireAuth: AngularFireAuth
)
// code block
{
}
this.angularFireAuth.currentUser.then( data => {
console.log(data);
console.log(data.uid);
)}
package.json
"firebase": "^7.0 || ^8.0",
"@angular/fire": "^6.1.5"