Skip to main content

Angular 4 and Firebase Authentication: Google Sign-in

Any user of the application would like to sign-up in less time. Users already have their social accounts like Google, Facebook, Twitter, etc. The same account can be leveraged in our application. Firebase provides a wonderful way to integrate these identity providers. In this post we will go through the implementation of Google sign-up.
If you have not checked my post on Angular 4 and Firebase Setup then I would recommend you to visit Setup post because this is in continuation to that article.

Step 1:

Create a service and give appropriate name to it. We will put all our sign-up and sign-in related methods in this service and will use it in other components. You can create a folder providers inside src/app and in providers create a file called AFAuth.ts.


As you can see in the above image, we have added two methods: loginWithGoogle to enable user to use his/her Google account for signup and logout to enable him/her to successfully logout of the application. We will use these two methods in our components that we will create in some time.

Now let us import the same service in app.module.ts file.


Step 2:

Before we can use Google login functionality we need to enable it from Firebase Console. Click on Authentication (on the side menu) and go to SIGN-IN METHOD tab and select Google. Enable it and save the changes.



Step 3:

Create Login Page component. We will use Angular CLI command to do so. This will add our loginPage component in app.module.ts file automatically.

ng generate component loginPage




Step 4:

Update login-page.component.ts file to call the loginWithGoogle method in the service. Here, we will create login method which will be called by user when he/she clicks the login button on the screen. We will also add Router now (but will configure later on) to navigate the user to home page if logged in successfully.



Step 5:

Add HTMLto loginPage component HTML.



Make sure your app.module.ts file looks like below. We have added AngularFireAuthModule and configured router by adding routes array. 




Step 6:

Create HomePage component using below command and check whether the same component is added to app.module.ts file.

ng generate component homePage



We will not modify this component as of now. We will use this for just redirection.

Step 7:

Modify routing in app.module.ts file to add HomePage component.


Also add <router-outlet> to app.component.html page.



Step 8:

Enhance the application by adding some more functionality. We will consider real world scenario over here. When user visits you application he/she must see the Login page if not logged in already else Home page.

To incorporate this feature we will update our app.component.ts file as shown in below image.


The above logic is straight forward to understand. However, I would like to mention here about authentication service (afAuthservice.auth) which comes with all authentication features. For example, here is the user object returned by auth service. It contains all the details about the current logged in user. Say, we want to display the name of the user. We can use user.displayName property for it.

Step 9:

Add logout functionality.

We will add logout function in the app.component.ts file.


Now add Logout link button in app.component.html file.




Step 10:

Run the application and try to login with your Google account.

ng serve --open



Now click on Login with Google button to check and see the popup is working or not.



You can check the user's Google account in Firebase Console.



Now you can logout of the application by clicking Logout link button on the top left corner.


Congratulations!!! You have successfully created Authentication module of your application.

Please feel free to drop in your doubts in comments section.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...