Guest User

Untitled

a guest
Aug 5th, 2018
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. <ion-header>
  2. <ion-toolbar>
  3. <ion-title>
  4. Login
  5. </ion-title>
  6. </ion-toolbar>
  7. </ion-header>
  8. <ion-content>
  9. <form (ngSubmit)="login()" [formGroup]="loginForm">
  10. <ion-grid>
  11. <ion-row>
  12. <ion-col>
  13. <ion-item>
  14. <ion-input type="text" placeholder="Email" formControlName="email" style="width:50%;"></ion-input>
  15. </ion-item>
  16. </ion-col>
  17. </ion-row>
  18. <ion-row>
  19. <ion-col col-3>
  20. <ion-item>
  21. <ion-input type="password" placeholder="Password" formControlName="password" style="width:50%;"></ion-input>
  22. </ion-item>
  23. </ion-col>
  24. </ion-row>
  25. </ion-grid>
  26. <div padding-horizontal>
  27. <div class="form-error">{{loginError}}</div>
  28. </div>
  29. <ion-grid>
  30. <ion-row>
  31. <ion-col>
  32. <ion-button type="submit" [disabled]="!loginForm.valid" color="primary">Log in</ion-button>
  33. &nbsp;&nbsp;
  34. <a href="#">Forgot password?</a>
  35. </ion-col>
  36. </ion-row>
  37. <ion-row>
  38. <ion-col>
  39. <ion-button icon-left block clear (click)="loginWithGoogle()" color="secondary">
  40. <ion-icon name="logo-google"></ion-icon>
  41. Log in with Google
  42. </ion-button>
  43. <ion-button icon-left block clear (click)="signup()" color="primary">
  44. <ion-icon name="person-add"></ion-icon>
  45. Sign up
  46. </ion-button>
  47. </ion-col>
  48. </ion-row>
  49. </ion-grid>
  50. </form>
  51. </ion-content>
  52.  
  53. import { Component, OnInit } from '@angular/core';
  54. import { AuthService } from '../common/services/auth.service';
  55. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  56. import { Router } from '@angular/router';
  57.  
  58. @Component({
  59. selector: 'app-login',
  60. templateUrl: './login.page.html',
  61. styleUrls: ['./login.page.scss'],
  62. })
  63.  
  64. export class LoginPage implements OnInit {
  65. loginForm: FormGroup;
  66. loginError: string;
  67.  
  68. constructor(private auth:AuthService,
  69. private router:Router,
  70. private fb: FormBuilder)
  71. {
  72. this.loginForm = this.fb.group({
  73. email: ['', Validators.compose([Validators.required, Validators.email])],
  74. password: ['', Validators.compose([Validators.required, Validators.minLength(6)])]
  75. });
  76. }
  77.  
  78. ngOnInit() {}
  79.  
  80. login() {
  81. alert('login');
  82. let data = this.loginForm.value;
  83.  
  84. if (!data.email) {
  85. return;
  86. }
  87.  
  88. let credentials = {
  89. email: data.email,
  90. password: data.password
  91. };
  92.  
  93. this.auth.signInWithEmail(credentials)
  94. .then(
  95. () => this.router.navigate(['/home']),
  96. error => this.loginError = error.message
  97. );
  98. }
  99. }
  100.  
  101. import { NgModule } from '@angular/core';
  102. import { CommonModule } from '@angular/common';
  103. import { FormsModule,ReactiveFormsModule } from '@angular/forms';
  104. import { Routes, RouterModule } from '@angular/router';
  105. import { IonicModule } from '@ionic/angular';
  106.  
  107. import { LoginPage } from './login.page';
  108.  
  109. const routes: Routes = [
  110. {
  111. path: '',
  112. component: LoginPage
  113. }
  114. ];
  115.  
  116. @NgModule({
  117. imports: [
  118. CommonModule,
  119. FormsModule,
  120. ReactiveFormsModule,
  121. IonicModule,
  122. RouterModule.forChild(routes),
  123. ],
  124. entryComponents: [LoginPage],
  125. declarations: [LoginPage]
  126. })
  127. export class LoginPageModule {}
  128.  
  129. import { Injectable } from '@angular/core';
  130. import { AngularFireAuth } from 'angularfire2/auth';
  131. import * as firebase from 'firebase/app';
  132. import AuthProvider = firebase.auth.AuthProvider;
  133.  
  134. @Injectable({
  135. providedIn: 'root'
  136. })
  137. export class AuthService {
  138. private user: firebase.User;
  139.  
  140. constructor(public afAuth: AngularFireAuth) {
  141. afAuth.authState.subscribe(user => {
  142. this.user = user;
  143. });
  144. }
  145.  
  146. signInWithEmail(credentials) {
  147. console.log('Sign in with email');
  148. return this.afAuth.auth.signInWithEmailAndPassword(credentials.email,
  149. credentials.password);
  150. }
  151. }
Add Comment
Please, Sign In to add comment