Angular-Firebase authentication

Manuel Hernández Lemus
3 min readMar 14, 2019

--

Create project step by step

1.- Create Angular project

ng new project-name --routing --style=scss

2.- Connect Firebase to Angular

Create project firebase on 👉 [Firebase console], once you have created the firebase project you will be redirected to the following screen:

3.- Configure environment

Add credentials in our Angular project configuration file environment.ts located in src/environments/environment.ts

environment.ts

4.- Install dependencies

npm install bootstrap jquery popper.js --save

Configure angular.json, add styles and scripts

Part of angular.json

5.- Install AngularFire and Firebase

npm install firebase @angular/fire --save

6.- Imports on app.module.ts

Part of app.module.ts

7.- Create User interface

ng g i interfaces/User
User interface

8.- Create service

ng g s providers/auth

In the auth.service.ts file, import AngularFireAuth, firebase and Observable. Create an observable user variable, inject FirebaseAuth in the constructor and create sign in methods.

9.- Create login component

ng g c components/login --skipTests --inlineStyle

In the button we add ngClick directive with the method loginWithGoogle()

login.component.html

Import service and injects on constructor in login.component.ts:

login.component.ts

Create method loginWithGoogle() and through the injected service we call the method created on the service.

loginWithGoogle method

10.- Create dashboard component

ng g c components/dashboard --skipTests --inlineStyle
dashboard.component.hml

With userdata variable call properties from interface, in this case ‘displayName, email, uid and photoURL’, remember that the userdata of type UserData stores the data that is brought from the firebase service.

The button call method logout() with the ngClick directive for close session.

dashboard.component.ts

Create variable of User type and assigns empty value to the properties.

In constructor injects the service and router and subscribe to the authentication state, userdara variable saves the data from firebase authentication. This verify if user is logged.

Now, create method `logout()` to close session.

11.- Create routes on app.routing.module.ts and enable HashLocationStrategy

app.routing.module.ts

Finish!!! Run the project

ng serve --o

💻🚀 👉 Code on Github

--

--