Advertisement
Guest User

Untitled

a guest
Mar 7th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. import {IonicApp, Page, NavController, Events, Alert} from 'ionic-angular';
  2. import {HomePage} from '../home/home';
  3. import {SignupPage} from '../signup/signup';
  4. import {FormBuilder, Validators, FORM_BINDINGS, ControlGroup} from 'angular2/common'
  5.  
  6. import {HttpService} from '../../providers/http-service';
  7. import {ValidationService} from '../../providers/validator-service';
  8.  
  9. @Page({
  10. providers: [HttpService, ValidationService],
  11. templateUrl: 'build/pages/login/login.html'
  12. })
  13. export class LoginPage {
  14.  
  15. nav: any;
  16. events: any;
  17. http: any;
  18.  
  19. login = {};
  20. submitted = false;
  21. loginForm: ControlGroup;
  22.  
  23. constructor(formBuilder: FormBuilder, nav: NavController, events: Events, http: HttpService) {
  24. this.nav = nav;
  25. this.events = events;
  26. this.http = http;
  27.  
  28. // Build login form with validators
  29. this.loginForm = formBuilder.group({
  30. username: ["", Validators.compose([Validators.required, ValidationService.emailValidator])],
  31. password: ["", Validators.compose([Validators.required, ValidationService.passwordValidator])]
  32. });
  33. }
  34.  
  35. onLogin() {
  36. this.submitted = true;
  37.  
  38. if (this.loginForm.valid) {
  39. // build the request from the form
  40. let request = {};
  41. request['username'] = this.loginForm.value.username;
  42. request['password'] = this.loginForm.value.password;
  43. request['accessToken'] = '';
  44. request['loginMethod'] = 'STANDALONE';
  45.  
  46. // make the request
  47. this.http.makeBackendRequest('POST', 'auth/login', request, this.onLoginSuccess, this.onLoginError, false);
  48. }
  49. }
  50.  
  51. onLoginSuccess(response) {
  52. // publish event to update the database
  53. this.events.publish('user:login', response);
  54.  
  55. // show alert to inform user and redirect him to Home
  56. this.showAlert("Connexion réussi", "Merci d'utiliser notre application, bon networking !", {
  57. text: 'Ok',
  58. handler: () => {
  59. this.nav.setRoot(HomePage);
  60. }
  61. });
  62. }
  63.  
  64. onLoginError(errorMessage) {
  65. let code = errorMessage.status;
  66. if (typeof code == "undefined")
  67. LoginPage.prototype.showAlert("Serveur non-accessible", "Notre serveur n'a pas répondu, veuillez réessayez", "Ok");
  68. else if (code == "404")
  69. LoginPage.prototype.showAlert("Erreur d'Authentification", "Aucun utilisateur trouvé pour cet email.", "Ok");
  70. else if (code == "502")
  71. LoginPage.prototype.showAlert("Serveur non-accessible", "Notre serveur n'a pas répondu, veuillez réessayez", "Ok");
  72. else if (code == "403")
  73. LoginPage.prototype.showAlert("Erreur d'Authentification", "Le mot-de-passe est incorrect.", "Ok");
  74. }
  75.  
  76. public showAlert(title, subTitle, button) {
  77. let alert = Alert.create({
  78. title: title,
  79. subTitle: subTitle,
  80. buttons: [button]
  81. });
  82. this.nav.present(alert);
  83. }
  84.  
  85. onSignup() {
  86. this.nav.setRoot(SignupPage);
  87. }
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement