File

src/app/login/login.page.ts

Implements

OnInit

Metadata

selector app-login
styleUrls ./login.page.scss
templateUrl ./login.page.html

Index

Properties
Methods

Constructor

constructor(fb: FormBuilder, afa: AngularFireAuth, router: Router, loadingController: LoadingController, modalController: ModalController, swal: SwalService, loggger: EventLoggerService, user: UserService)
Parameters :
Name Type Optional Description
fb FormBuilder No

Form builder for building the forms in angular material

afa AngularFireAuth No

Authentication service from the Angular fire

router Router No

Routing service for the ionic navigation

loadingController LoadingController No

Loading component service to preview preloader

modalController ModalController No

Modal service to present the modal

swal SwalService No

Swal service for the custom implementation for the sweet alerts

loggger EventLoggerService No

Logiing service for the analytics dashboard in Firebase

user UserService No

Methods

checkFormControlIsValid
checkFormControlIsValid(control: string)
Parameters :
Name Type Optional
control string No
Returns : any
Public hasError
hasError(controlName: string, errorName: string)
Parameters :
Name Type Optional
controlName string No
errorName string No
Returns : any
ngOnInit
ngOnInit()
Returns : void

Properties

forgetPassword
Default value : () => {...}
getErrorMessage
Default value : () => {...}
loading
loginAction
Default value : () => {...}
loginForm
Type : FormGroup
previewErrorMessage
Default value : () => {...}
import { PlatformService } from './../services/platform/platform.service';
import { SwalService } from './../services/swal/swal.service';
import { LoadingController, ModalController } from '@ionic/angular';
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AngularFireAuth } from '@angular/fire/auth';
import { Router } from '@angular/router';
import { ForgetPasswordComponent } from '../components/forget-password/forget-password.component';
import { EventLoggerService } from '../services/logger/event-logger.service';
import { FcmService } from '../fcm.service';
import { UserService } from '../services/user/user.service';

@Component({
  selector: 'app-login',
  templateUrl: './login.page.html',
  styleUrls: ['./login.page.scss']
})
export class LoginPage implements OnInit {
  loginForm: FormGroup;
  loading;
  /**
   *
   * @param fb Form builder for building the forms in angular material
   * @param afa Authentication service from the Angular fire
   * @param router Routing service for the ionic navigation
   * @param loadingController Loading component service to preview preloader
   * @param modalController Modal service to present the modal
   * @param swal Swal service for the custom implementation for the sweet alerts
   * @param fcm Firebase Cloud Messaging service for the notification creation
   * @param loggger Logiing service for the analytics dashboard in Firebase
   * @param platformService Service for retrieve the platform information and device information
   */
  constructor(
    private fb: FormBuilder,
    private afa: AngularFireAuth,
    private router: Router,
    private loadingController: LoadingController,
    private modalController: ModalController,
    private swal: SwalService,
    private loggger: EventLoggerService,
    private user: UserService
  ) {
    this.loadingController.create({
      message: 'Please wait verifying!',
      duration: 2000
    });
  }
  ngOnInit() {
    this.loginForm = this.fb.group({
      userEmail: ['', [Validators.required, Validators.email]],
      userPassword: ['', Validators.required]
    });
  }

  public hasError(controlName: string, errorName: string) {
    return this.loginForm.controls[controlName].hasError(errorName);
  }

  loginAction = async () => {
    const userEmail = this.loginForm.controls['userEmail'].value;
    const userPassword = this.loginForm.controls['userPassword'].value;
    const loading = await this.loadingController.create({
      message: 'Veryfying your details!'
    });
    await loading.present();
    this.afa.auth
      .signInWithEmailAndPassword(userEmail, userPassword)
      .then(data => {
        if (data.user.emailVerified) {
          this.swal.displayAutoHideMessage('Welcome back!', '', 'success', 1500);
          this.router.navigateByUrl('menu', { replaceUrl: true });
          // Subscribing for user topics
          this.user.subscribeForUserTopics();
          // Update user token
          this.user.registerToken();
          // Record login attempt
          this.loggger.loginAttempt();
          // Registering for crashlytics
          this.user.registerCrashlytics();
        } else {
          this.swal.displayConfirmation(
            'Error',
            'Please verify your email before login. Click confirm to send the email again',
            reply => {
              if (reply) {
                this.user.sendConfirmationEmail();
              }
            }
          );
        }
      })
      .catch(error => {
        console.log('error :', error, error.message);

        this.previewErrorMessage(error);
      })
      .finally(() => loading.dismiss());
  };

  getErrorMessage = (controller: string) => {
    const formController = this.loginForm.controls[controller];
    return formController.hasError('required')
      ? 'You must enter a value'
      : formController.hasError('email')
      ? 'Not a valid email'
      : '';
  };

  checkFormControlIsValid(control: string) {
    return this.loginForm.controls[control].invalid;
  }

  previewErrorMessage = ({ code, message }) => {
    const swalProps = { title: '', description: '' };
    switch (code) {
      case 'network-request-failed':
        swalProps.description = 'Check your internet Connection';
        swalProps.title = 'Connection Failure';
        break;
      case 'auth/user-disabled':
        swalProps.description = 'Your connection is disabled please contact authority';
        swalProps.title = 'Account disabled';
        break;
      case 'auth/wrong-password':
        swalProps.description = 'Your password is incorrect';
        swalProps.title = 'Wrong Password';
        break;
      default:
        console.log('executing default');
        swalProps.description = message;
        swalProps.title = 'Error';
        break;
    }
    this.swal.viewErrorMessage(swalProps.title, swalProps.description);
  };

  forgetPassword = async () => {
    const modal = await this.modalController.create({
      component: ForgetPasswordComponent
    });
    modal.present();
  };
}
<ion-content class="ion-padding">
  <ion-card id="card">
    <form [formGroup]="loginForm" class="form">
      <ion-grid>
        <ion-row align-items-center justify-content-center>
          <img src="../assets/img/icon.png" alt="libri" id="login-image" />
        </ion-row>
        <ion-row align-items-center justify-content-center>
          <ion-col>
            <mat-form-field class="formField" appearance="outline">
              <mat-hint>Place your University Email</mat-hint>
              <input matInput placeholder="Email" formControlName="userEmail" />
              <mat-error *ngIf="checkFormControlIsValid('userEmail')">{{
                getErrorMessage('userEmail')
              }}</mat-error>
            </mat-form-field>
          </ion-col>
        </ion-row>
        <ion-row align-items-center justify-content-center>
          <ion-col>
            <mat-form-field class="formField" appearance="outline">
              <mat-hint>Enter your password</mat-hint>
              <input
                matInput
                placeholder="Password"
                formControlName="userPassword"
                type="password"
              />
              <mat-error *ngIf="checkFormControlIsValid('userPassword')">{{
                getErrorMessage('userPassword')
              }}</mat-error>
            </mat-form-field>
          </ion-col>
        </ion-row>
        <ion-row align-items-center justify-content-center>
          <ion-col>
            <ion-button fill="clear" expand="block" (click)="forgetPassword()"
              >Forget password</ion-button
            >
          </ion-col>
        </ion-row>
        <ion-row align-items-center justify-content-center>
          <ion-col>
            <ion-button expand="block" [disabled]="loginForm.invalid" (click)="loginAction()"
              >Login</ion-button
            >
          </ion-col>
        </ion-row>
        <ion-row align-items-center justify-content-center>
          <ion-col>
            <ion-button expand="block" color="danger" routerLink="/signup">Sign Up</ion-button>
          </ion-col>
        </ion-row>
      </ion-grid>
    </form>
  </ion-card>
</ion-content>

./login.page.scss

.toolbar {
  background-color: '#fe5200';
}

.formField {
  display: block;
  margin-left: 5%;
  margin-right: 5%;
  margin-bottom: 5vh;
}

@media only screen and (min-width: 200px) {
  #login-image {
    width: 80%;
    height: auto;
  }
}

#card {
  height: 100%;
  background-color: white;
  margin: 0 auto;
}

@media only screen and (min-width: 1024px) {
  #login-image {
    width: 50%;
    height: auto;
  }
  #card {
    width: 60%;
  }
}

@media only screen and (min-width: 1400px) {
  #login-image {
    width: 30%;
    height: auto;
  }
}
ion-content {
  background-color: #056571;
  --background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='100' viewBox='0 0 200 200'%3E%3Cdefs%3E%3ClinearGradient id='a' gradientUnits='userSpaceOnUse' x1='100' y1='33' x2='100' y2='-3'%3E%3Cstop offset='0' stop-color='%23000' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23000' stop-opacity='1'/%3E%3C/linearGradient%3E%3ClinearGradient id='b' gradientUnits='userSpaceOnUse' x1='100' y1='135' x2='100' y2='97'%3E%3Cstop offset='0' stop-color='%23000' stop-opacity='0'/%3E%3Cstop offset='1' stop-color='%23000' stop-opacity='1'/%3E%3C/linearGradient%3E%3C/defs%3E%3Cg fill='%23045660' fill-opacity='0.6'%3E%3Crect x='100' width='100' height='100'/%3E%3Crect y='100' width='100' height='100'/%3E%3C/g%3E%3Cg fill-opacity='0.5'%3E%3Cpolygon fill='url(%23a)' points='100 30 0 0 200 0'/%3E%3Cpolygon fill='url(%23b)' points='100 100 0 130 0 100 200 100 200 130'/%3E%3C/g%3E%3C/svg%3E");
  // --background: linear-gradient(to right, #056571, #649173);
}

.form {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  background: url('../../assets/img/backgrounds/login.svg') no-repeat center center fixed;
}
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""