Site icon StackBlogger

How to implement authentication in Ionic tabs project

Ionic is a hybrid mobile app development JavaScript framework which enables web apps developer to easily build applications that can run on many mobile OS platforms like iOS, Android and Windows. There are many starter projects available in Ionic marketplace which developer can use to start with. The starter project include Sidemenu projects, Tab project and many more. Here we are going to see how we can implement the authentication in Ionic tabs project.

Ionic tabs project is a starter boilerplate provided by Ionic marketplace free of cost for anyone to start with the Ionic app development. It consist a few tabs like Home, Contact Us and About Us. The authentication is by default not provided in the starter boilerplate. We will learn how we can implement the authentication in tabs project.

Steps to implement authentication in Ionic tabs project are as follows-

1. Create a tab starter project

Our first step is to create a tab starter boilerplate in Ionic. Let’s run the below command to create the boilerplate-

The above command will generate an Ionic Tab Starter project.

run

$ cd authentication-in-ionic-tab-project

2. Add a service provider

Add a service provider in the project by running following command-

3. Edit the service provider

Open the project in some editor (I am using Visual Studio code) and edit the service provider file. Open the auth-service.ts file in editor and paste below code in that-

Here I have my server running at http://localhost:3009/ URL.

4. Setup the login page

Generate a login page and edit its code to call auth service provider. Run command below to generate a login page-

Now open the login.html file and paste below code in that-

Open login.ts file and paste following code in that-

5. Setup authentication handler

We need to setup a Core Authentication handler file which will run on every request and will check for user authenticity. If user is not authorized or the token is expired, it will throw the user to login page.

Create a manually file auth-service-core.ts in /src/app/core folder.

Note: If core folder doesn’t exist in /src/app, please create one manually.

Paste the following code in auth-service-core.tsfile-

Here we need to install a new dependency @auth0/angular-jwt. Run below command to install it-

Import the @auth0/angular-jwtmodule in app.module.tsfile.

Open app.component.ts file and paste following code in that-

Now run the project using ionic servecommand. When you run the project, it will throw an error on browser Cannot find module 'rxjs/internal/Observable'. To solve the error, follow the blog How to solve Observable error.

If you don’t want to open another blog post, you can just paste this two line in package.json file.

And run npm install

Now again run the project using command ionic serveIt will throw the error- Error: No component factory found for LoginPage. Did you add it to @NgModule.entryComponents?

We need to import LoginPage component in app.module.ts file. Do it like this-

Run the project now. You will see the login screen on browser.

Try to enter some credentials and hit Login button-

The page is working properly. Now enter a valid credential and hit Login button-

You can see the page is redirected from Login to Home page.

 

That’s the all stuff for now.

Would love to hear your comments pls

Thanks 🙂