Tuesday, December 10, 2019

Create Ionic 4 Login and Register UI with Angular

In this Ionic 4 tutorial, we will learn how to create a responsive login and registration form with Angular 8 using Ionic UI components.
We have created authentication tutorials on the following platforms Angular, Firebase, Node, Express and MongoDB.
Complete Angular Firebase Authentication Tutorial.
Create Angular 9 JWT Authentication with Node and Express Server.
Creating a login and registration form is merely effortless all thanks goes to the Ionic’s built-in UI components. Ionic makes frontend developers job easy and short to build the login UI.
To make the Ionic 4 Form layout, we will also take the help of Ionic 4 Grid system, Form elements such as input fields and Buttons.
Create Ionic 4 Login and Register UI with Angular

Our Ionic 4 Angular 8 Form app will have login and registration pages so we will also learn to implement Ionic 4 routing to navigate between components.
Table of Contents

  • Install Ionic Angular Project
  • Generate Components
  • Configure Ionic Routing
  • Create Ionic 4 Login UI Form
  • Create Register Form Page
  • Create Forgot Password
  • Conclusion

Install Ionic Angular Project
To get started with Login & Registration UI, Install the blank Ionic/Angular project by running the following command.
ionic start ionic-form-ui blank --type=angular
Get inside the project directory.
cd ionic-form-ui
Install the lab mode by running the below command.
npm i @ionic/lab --save-dev
Run the command to start your Ionic app.
ionic serve -l
Ionic 4 Forms Project
Generate Components
To create the Ionic form we need to generate components for Login, Sign Up, and Forgot Password pages. In Ionic we call pages to components, run the following command to create the pages.
ng generate page login
ng generate page registration
ng generate page forgot-password
We have generated the following components which you can see in your IDE or text-editor as well.
Configure Ionic Routing
In the home page we will create the log-in and sign-up button, clicking on these button user will navigate to their respective page. In the next step, we will learn how to enable the routing in Ionic 4 app.
import { NgModule } from '@angular/core';
import { PreloadAllModules, RouterModule, Routes } from '@angular/router';
const routes: Routes = [
  { path: '', redirectTo: 'home', pathMatch: 'full' },
  { path: 'home', loadChildren: () => import('./home/home.module').then( m => m.HomePageModule)},
  {
    path: 'login',
    loadChildren: () => import('./login/login.module').then( m => m.LoginPageModule)
  },
  {
    path: 'registration',
    loadChildren: () => import('./registration/registration.module').then( m => m.RegistrationPageModule)
  },
  {
    path: 'forgot-password',
    loadChildren: () => import('./forgot-password/forgot-password.module').then( m => m.ForgotPasswordPageModule)
  },
];
@NgModule({
  imports: [
    RouterModule.forRoot(routes, { preloadingStrategy: PreloadAllModules })
  ],
  exports: [RouterModule]
})
export class AppRoutingModule { }
Add Angular [routerLink]="['...']" directive in Ionic buttons to enable the navigation between components. Open home.page.htmlfile and add the following code.
<ion-header>
  <ion-toolbar>
    <ion-title>
      Ionic 4 Form
    </ion-title>
  </ion-toolbar>
</ion-header>
<ion-content class="auth-form">
  <ion-grid>
    <ion-row>
      <ion-col align-self-center>
        <ion-button [routerLink]="['/registration']" expand="block" color="primary">Register</ion-button>
        <span class="divider line one-line">or</span>
        <span class="already">Already a user?</span>
        <ion-button [routerLink]="['/login']" expand="block" color="danger">Sign In</ion-button>
      </ion-col>
    </ion-row>
  </ion-grid>
</ion-content>
Open home.page.scss file to add the CSS style for the home screen.
.divider {
    display: flex;
 
    &:before,
    &:after {
      content: "";
      flex: 1;
    }
}

.line {
    align-items: center;
    margin: 1em -1em;
    color: #cccccc;
 
    &:before,
    &:after {
      height: 1px;
      margin: 0 1em;
    }
}
.one-line {
    &:before,
    &:after {
       background: #cccccc;
    }
}
.auth-form ion-grid,
.auth-form ion-row {
    height: 100%;
    justify-content: center;
}
.already {
    display: block;
    text-align: center;
    padding-bottom: 10px;
}
Create Ionic 4 Login UI Form
To create login form template we need to use the Ionic UI components such as ion-input and ion-button.
Open login.page.html file and paste the following code in it.
<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>Log In</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <form>
    <ion-item lines="full">
      <ion-label position="floating">Email</ion-label>
      <ion-input type="text" required></ion-input>
    </ion-item>
    <ion-item lines="full">
      <ion-label position="floating">Password</ion-label>
      <ion-input type="password" required></ion-input>
    </ion-item>
    <ion-row>
      <ion-col>
        <ion-button type="submit" color="danger" expand="block">Sign In</ion-button>
        <a [routerLink]="['/forgot-password']" class="small-text">Forgot Password?</a>
      </ion-col>
    </ion-row>
  </form>
</ion-content>
To go back to the previous page we used the ion-back-button component, we build the login form in Ionic 4 / Angular using input, button components.
Create Register Form Page
To create a sign up form we will add the first name, last name, email and password field in the Ionic’s register page and the open the registration.page.html file and add the following code in it.
<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>Register</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <form>
    <ion-item lines="full">
      <ion-label position="floating">First name</ion-label>
      <ion-input type="text" required></ion-input>
    </ion-item>
    <ion-item lines="full">
      <ion-label position="floating">Last name</ion-label>
      <ion-input type="text" required></ion-input>
    </ion-item>
    <ion-item lines="full">
      <ion-label position="floating">Email</ion-label>
      <ion-input type="text" required></ion-input>
    </ion-item>
    <ion-item lines="full">
      <ion-label position="floating">Password</ion-label>
      <ion-input type="password" required></ion-input>
    </ion-item>
    <ion-row>
      <ion-col>
        <ion-button type="submit" color="danger" expand="block">Sign Up</ion-button>
      </ion-col>
    </ion-row>
  </form>
</ion-content>
Create Forgot Password
Next, we will create a forgot password form in the Ionic page. If a user forgets his or her password then he will enter his email address and will get instruction on his registered email id to create a new password.
Open the forgot-password.page.html file and add the following code inside of it.
<ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-back-button></ion-back-button>
    </ion-buttons>
    <ion-title>Reset Your Password</ion-title>
  </ion-toolbar>
</ion-header>
<ion-content>
  <form>
    <ion-item lines="full">
      <ion-label position="floating">Email</ion-label>
      <ion-input type="email" required></ion-input>
    </ion-item>
    <ion-row>
      <ion-col>
        <ion-button type="submit" color="danger" expand="block">Send</ion-button>
      </ion-col>
    </ion-row>
    <small>
      Please provide the username or email address that you used when you signed
      up for your Evernote account.
    </small>
  </form>
</ion-content>
Forgot Password in Ionic
Conclusion
Thats it for now finally we have completed Ionic 4 Login UI tutorial and learned how to create basic sign-in and sign-up form in Ionic / Angular app. You can get the completed code ionic 4 geolocation example of this project on this Git repo.

No comments:

Post a Comment