Guest User

Untitled

a guest
May 17th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { NavController, AlertController, LoadingController } from 'ionic-angular';
  3. import { UserDetails, IDetailedError } from '@ionic/cloud-angular';
  4. import { Auth } from '@ionic/cloud-angular'
  5. import { User } from '@ionic/cloud-angular';
  6. import { HomePage } from '../home/home';
  7.  
  8.  
  9. @Component({
  10. selector: 'page-login',
  11. templateUrl: 'login.html',
  12. })
  13. export class LoginPage {
  14.  
  15. showLogin:boolean = true;
  16. email:string = '';
  17. password:string = '';
  18. name:string = '';
  19.  
  20. constructor(public navCtrl: NavController, public auth: Auth, public user: User, public alertCtrl: AlertController, public loadingCtrl:LoadingController) {}
  21.  
  22. ionViewDidLoad() {
  23. console.log('Hello LoginPage Page');
  24. }
  25.  
  26. /*
  27. for both of these, if the right form is showing, process the form,
  28. otherwise show it
  29. */
  30. doLogin() {
  31. if(this.showLogin) {
  32. console.log('process login');
  33.  
  34. if(this.email === '' || this.password === '') {
  35. let alert = this.alertCtrl.create({
  36. title:'Register Error',
  37. subTitle:'All fields are required',
  38. buttons:['OK']
  39. });
  40. alert.present();
  41. return;
  42. }
  43.  
  44. let loader = this.loadingCtrl.create({
  45. content: "Logging in..."
  46. });
  47. loader.present();
  48.  
  49. this.auth.login('basic', {'email':this.email, 'password':this.password}).then(() => {
  50. console.log('ok i guess?');
  51. loader.dismissAll();
  52. this.navCtrl.setRoot(HomePage);
  53. }, (err) => {
  54. loader.dismissAll();
  55. console.log(err.message);
  56.  
  57. let errors = '';
  58. if(err.message === 'UNPROCESSABLE ENTITY') errors += 'Email isn\'t valid.<br/>';
  59. if(err.message === 'UNAUTHORIZED') errors += 'Password is required.<br/>';
  60.  
  61. let alert = this.alertCtrl.create({
  62. title:'Login Error',
  63. subTitle:errors,
  64. buttons:['OK']
  65. });
  66. alert.present();
  67. });
  68. } else {
  69. this.showLogin = true;
  70. }
  71. }
  72.  
  73. doRegister() {
  74. if(!this.showLogin) {
  75. console.log('process register');
  76.  
  77. /*
  78. do our own initial validation
  79. */
  80. if(this.name === '' || this.email === '' || this.password === '') {
  81. let alert = this.alertCtrl.create({
  82. title:'Register Error',
  83. subTitle:'All fields are required',
  84. buttons:['OK']
  85. });
  86. alert.present();
  87. return;
  88. }
  89.  
  90. let details: UserDetails = {'email':this.email, 'password':this.password, 'name':this.name};
  91. console.log(details);
  92.  
  93. let loader = this.loadingCtrl.create({
  94. content: "Registering your account..."
  95. });
  96. loader.present();
  97.  
  98. this.auth.signup(details).then(() => {
  99. console.log('ok SignUp');
  100. this.auth.login('basic', {'email':details.email, 'password':details.password}).then(() => {
  101. loader.dismissAll();
  102. this.navCtrl.setRoot(HomePage);
  103. });
  104.  
  105. }, (err:IDetailedError<string[]>) => {
  106. loader.dismissAll();
  107. let errors = '';
  108. for(let e of err.details) {
  109. console.log(e);
  110. if(e === 'required_email') errors += 'Email is required.<br/>';
  111. if(e === 'required_password') errors += 'Password is required.<br/>';
  112. if(e === 'conflict_email') errors += 'A user with this email already exists.<br/>';
  113. //don't need to worry about conflict_username
  114. if(e === 'invalid_email') errors += 'Your email address isn\'t valid.';
  115. }
  116. let alert = this.alertCtrl.create({
  117. title:'Register Error',
  118. subTitle:errors,
  119. buttons:['OK']
  120. });
  121. alert.present();
  122. });
  123.  
  124. } else {
  125. this.showLogin = false;
  126. }
  127. }
  128.  
  129. }
Add Comment
Please, Sign In to add comment