Angular & FirebaseAuth

October 23, 2019 Angular 0 minutes, 45 secondes

Développement Angular Authentification basique et Google via Firebase

Authentification basique et Google via Firebase

Ce post présente l'authentification et l'inscription de vos utilisateurs via Firebase en Angular.

Voici le code du constructeur de mon service d'authentification :

  constructor(private afAuth: AngularFireAuth,
              private db: AngularFireDatabase,
              private router: Router) {
    }

Inscription basique :

Code Angular pour gérer une inscription basique.

register(input: UserConn) {
    return this.afAuth.auth.createUserWithEmailAndPassword(input.email, input.password)
    .then(usr => {
      this.router.navigateByUrl('/').then(() => this.router.navigateByUrl(''));
    })
    .catch(err => {
      console.log('ERROR on basic registration: ', err.code, err.message);
      throw err;
    });
  }

Authentification basique

Code Angular pour gérer une authentification basique.

  login(user: UserConn, currentUrl: string): Promise<any> {
    return this.afAuth.auth.signInWithEmailAndPassword(user.email, user.password)
    .then(usr => {
      this.completeLogin(user.email);
    })
    .catch(err => {
      console.log('ERROR on basic authentification: ', err.message);
      throw err;
    });
  }

Authentification et inscription Google

  loginGoogle() {
    const myprovider = new firebase.auth.GoogleAuthProvider();

    return this.afAuth.auth.signInWithPopup(myprovider)
    .then(data => {
      this.completeLogin(data.user.email);
    })
    .catch(err => {
      console.log('ERROR on Google Authentification: ', err);
      throw err;
    });
  }

alt