Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.26 KB | None | 0 0
  1. <ion-header>
  2. <ion-navbar>
  3. <button ion-button menuToggle>
  4. <ion-icon name="menu"></ion-icon>
  5. </button>
  6. <ion-title>Login</ion-title>
  7. </ion-navbar>
  8. </ion-header>
  9.  
  10.  
  11. <ion-content padding>
  12.  
  13. <form [formGroup]="loginForm" (ngSubmit)="submit()">
  14. <ion-item>
  15. <ion-label floating>Email</ion-label>
  16. <ion-input type="text" formControlName="email" id="email" [(ngModel)]="personModel.emailAddress"></ion-input>
  17. </ion-item>
  18. <control-messages class="error-box" [control]="loginForm.controls.email"></control-messages>
  19. <ion-item>
  20. <ion-label floating>Password</ion-label>
  21. <ion-input type="password" formControlName="password" id="password"></ion-input>
  22. </ion-item>
  23. <control-messages class="error-box" [control]="loginForm.controls.password"></control-messages>
  24. <br/>
  25. <ion-buttons>
  26. <button ion-button class="form-button-text" type="submit" [disabled]="!loginForm.valid" block round>Sign In</button>
  27. </ion-buttons>
  28. </form>
  29. <br/><br/>
  30. <p (click)="forgotPassword()" class="small-text">Forgot email or password?</p>
  31. <br/><br/><br/><br/>
  32. <button ion-button color="light" (click)="register()" color="dark" clear block round class="form-button-text">Quick Sign up</button>
  33.  
  34. </ion-content>
  35.  
  36. import { Component, Input, Inject, forwardRef } from '@angular/core';
  37. import { NavController, NavParams, ViewController, AlertController, MenuController, Events, Loading, LoadingController } from 'ionic-angular';
  38. import { FirebaseAuth } from 'angularfire2';
  39. import { ValidationService } from '../validation/validationService';
  40. import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
  41. import { RegisterPage } from '../register/register';
  42. import { ForgotPage } from '../forgot/forgot';
  43. import { PersonModel } from '../model/personModel';
  44. import { PersonService } from '../service/personService';
  45. import { UtilityService } from '../utils/utilityService';
  46. import { PersonPage } from '../person/person';
  47.  
  48. @Component({
  49. templateUrl: 'loginemail.html'
  50. })
  51.  
  52. export class LoginEmailPage {
  53.  
  54. public loginForm: FormGroup;
  55. public errorMessage: string;
  56. public personModel: PersonModel = null;
  57. public personService: PersonService = null;
  58. public personLoggedIn: boolean = false;
  59. public menu: MenuController = null;
  60. public utilityService: UtilityService = null;
  61. public events: Events = null;
  62. public loading: Loading = null;
  63. public alertCtrl: AlertController = null;
  64. public fireAuth: firebase.auth.Auth;
  65. public userProfile: firebase.database.Reference;
  66.  
  67. @Input() control: FormControl;
  68.  
  69. constructor(@Inject(forwardRef(() => UtilityService)) utilityService, public auth: FirebaseAuth, menu: MenuController, public nav: NavController,
  70. public navParams: NavParams, public builder: FormBuilder, public viewCtrl: ViewController, alertCtrl: AlertController,
  71. personService: PersonService, events: Events, public loadingCtrl: LoadingController) {
  72. this.fireAuth = firebase.auth();
  73. this.userProfile = firebase.database().ref('/userProfile');
  74. this.loginForm = builder.group({
  75. 'email': ['', [Validators.required, Validators.minLength(3), Validators.maxLength(55), ValidationService.emailValidator, (control) => ValidationService.personEmailNotExists(control, this.personService)]],
  76. 'password': ['', [Validators.required, Validators.minLength(5), Validators.maxLength(45), ValidationService.passwordValidator]]
  77. });
  78. this.alertCtrl = alertCtrl;
  79. this.events = events;
  80. this.utilityService = utilityService;
  81. this.menu = menu;
  82. this.personModel = this.navParams.get('personModel');
  83. if (!this.personModel) {
  84. this.personModel = new PersonModel();
  85. }
  86. this.personService = personService;
  87. }
  88.  
  89. submit() {
  90. this.loading = this.loadingCtrl.create({
  91. content: 'Please wait...'
  92. });
  93. if (this.loginForm.dirty && this.loginForm.valid) {
  94. this.loading.present().then(() => {
  95. this.checkCredentials(this.loginForm.value.email, this.loginForm.value.password).then(() => {
  96. if (this.personLoggedIn === true) {
  97. this.loginFirebaseUser(this.loginForm.value.email, this.loginForm.value.password).then((authData) => {
  98. let user: firebase.User = this.fireAuth.currentUser;
  99. if (!user) {
  100. this.auth.subscribe((authData) => {
  101. this.login(authData.auth);
  102. });
  103. } else {
  104. this.login(user);
  105. }
  106. }).catch((error) => {
  107. console.error('Error trying to login ', error);
  108. this.loading.dismiss().then(() => {
  109. this.doAlert(error.message);
  110. });
  111. });
  112. }
  113. this.loading.dismiss();
  114. });
  115. });
  116. }
  117. }
  118.  
  119. login(firebaseUser: firebase.User): void {
  120. let promise: Promise<any> = this.utilityService.login(this.personModel, firebaseUser, this.nav, this.auth, this.fireAuth, false);
  121. if (promise) {
  122. promise.then(() => {
  123. let data = {
  124. person: this.personModel
  125. }
  126. this.events.publish('push:notifications', data);
  127. this.loading.dismiss().then(() => {
  128. if (this.navParams.get('fromReview')) {
  129. this.nav.pop();
  130. } else if (this.navParams.get('fromChat')) {
  131. this.nav.pop();
  132. } else {
  133. this.nav.setRoot(PersonPage);
  134. }
  135. });
  136. }, error => {
  137. this.utilityService.logout(this.auth, this.fireAuth).then(() => {
  138. this.utilityService.setUpMenuItems();
  139. this.auth.logout();
  140. });
  141. this.loading.dismiss().then(() => {
  142. let alert = this.alertCtrl.create({
  143. message: error.message,
  144. buttons: [
  145. {
  146. text: "Ok",
  147. role: 'cancel'
  148. }
  149. ]
  150. });
  151. alert.present();
  152. });
  153. });
  154. } else {
  155. this.loading.dismiss();
  156. }
  157. }
  158.  
  159. checkCredentials(email: string, password: string): any {
  160. let promiseUsername: Promise<PersonModel> = this.personService.getPersonByEmail(email);
  161. return promiseUsername.then((personModel: PersonModel) => {
  162. if (personModel.emailAddress != email) {
  163. this.doAlert('Email does not exist.');
  164. } else {
  165. if (personModel.emailAddress === this.loginForm.value.email) {
  166. this.personModel = personModel;
  167. this.personLoggedIn = true;
  168. } else {
  169. this.personLoggedIn = false;
  170. this.doAlert('Password does not match Username.');
  171. }
  172. }
  173. });
  174. }
  175.  
  176. doAlert(msg: string) {
  177. this.loading.dismiss().then(() => {
  178. let alert = this.alertCtrl.create({
  179. title: 'Login',
  180. subTitle: msg,
  181. buttons: ['Dismiss']
  182. });
  183. alert.present().then(() => {
  184. this.loading.dismiss();
  185. });
  186. });
  187. }
  188.  
  189.  
  190. register() {
  191. this.nav.push(RegisterPage, {
  192. })
  193. }
  194.  
  195. forgotPassword() {
  196. this.nav.push(ForgotPage, {
  197. personModel: this.personModel
  198. });
  199. }
  200.  
  201. loginFirebaseUser(email: string, password: string): firebase.Promise<boolean> {
  202. return this.fireAuth.signInWithEmailAndPassword(email, password).then(() => {
  203. console.log('signInWithEmailAndPassword', email, password);
  204. }).catch((error)=> {
  205. console.error('Error signInWithEmailAndPassword', email, password, error.name, error.message);
  206. throw new Error(error.message);
  207. });
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement