Sin descripción

login.page.ts 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import { Component, OnInit } from '@angular/core';
  2. import { Facebook, FacebookLoginResponse } from '@awesome-cordova-plugins/facebook/ngx';
  3. import { WpdataService } from "../services/wpdata.service";
  4. import { Storage } from '@ionic/storage-angular';
  5. import { Router } from '@angular/router';
  6. import { ModalController, Platform } from '@ionic/angular';
  7. import { ProfilePage } from '../profile/profile.page';
  8. import { RegisterFormPage } from '../register-form/register-form.page';
  9. import { ForgotPasswordPage } from '../forgot-password/forgot-password.page';
  10. import { SignInWithApple, AppleSignInResponse, AppleSignInErrorResponse, ASAuthorizationAppleIDRequest } from '@awesome-cordova-plugins/sign-in-with-apple/ngx';
  11. @Component({
  12. selector: 'app-login',
  13. templateUrl: './login.page.html',
  14. styleUrls: ['./login.page.scss'],
  15. })
  16. export class LoginPage implements OnInit {
  17. registerForm = { email: "", password:"" };
  18. isAndroid = false;
  19. constructor(private fb: Facebook, private wp: WpdataService, private storage: Storage, private router:Router, public modalController: ModalController, private signInWithApple: SignInWithApple, private platform: Platform) { }
  20. //constructor() { }
  21. ngOnInit() {
  22. if (this.platform.is('android')) {
  23. this.isAndroid = true;
  24. console.log("Android");
  25. }else {
  26. console.log("Other");
  27. }
  28. }
  29. async ionViewWillEnter() {
  30. /*
  31. let u = await this.storage.get('user');
  32. console.log(u);
  33. if( u != null ) {
  34. //this.router.navigate(['/profile']);
  35. this.modalController.dismiss({
  36. 'dismissed': true
  37. });
  38. const modal = await this.modalController.create({
  39. component: ProfilePage,
  40. });
  41. return await modal.present();
  42. }*/
  43. }
  44. async openRegister() {
  45. this.modalController.dismiss({
  46. 'dismissed': true
  47. });
  48. const modal = await this.modalController.create({
  49. component: RegisterFormPage,
  50. });
  51. return await modal.present();
  52. }
  53. async openForgotPassword() {
  54. this.modalController.dismiss({
  55. 'dismissed': true
  56. });
  57. const modal = await this.modalController.create({
  58. component: ForgotPasswordPage,
  59. });
  60. return await modal.present();
  61. }
  62. async userLogin() {
  63. console.log(this.registerForm);
  64. if( this.registerForm.email == "" || this.registerForm.password == "" ) {
  65. alert("Please enter email and password");
  66. return;
  67. }
  68. let res = await this.wp.loginUser(this.registerForm);
  69. console.log(res);
  70. if(res.error == true) {
  71. alert(res.msg)
  72. }else {
  73. alert("Login Success");
  74. this.storage.set('user', {name: res.output.firstName + " "+res.output.lastName, email: res.output.email});
  75. this.dismiss();
  76. }
  77. }
  78. doFbLogin() {
  79. this.fb.login(['public_profile', 'email'])
  80. .then((res: FacebookLoginResponse) =>{
  81. console.log('Logged into Facebook!', res);
  82. this.fb.api('me?fields=id,name,email,first_name,last_name,picture.width(720).height(720).as(picture_large)', []).then(async profile => {
  83. console.log(profile);
  84. let abc = await this.wp.storeFbData(profile);
  85. console.log(JSON.parse(abc.data));
  86. this.storage.set('user', profile);
  87. //this.router.navigate(['/profile']);
  88. this.modalController.dismiss({
  89. 'dismissed': true
  90. });
  91. const modal = await this.modalController.create({
  92. component: ProfilePage,
  93. });
  94. return await modal.present();
  95. });
  96. })
  97. .catch(e => console.log('Error logging into Facebook', e));
  98. }
  99. dismiss() {
  100. // using the injected ModalController this page
  101. // can "dismiss" itself and optionally pass back data
  102. this.modalController.dismiss({
  103. 'dismissed': true
  104. });
  105. }
  106. appleSignIn() {
  107. this.signInWithApple.signin({
  108. requestedScopes: [
  109. ASAuthorizationAppleIDRequest.ASAuthorizationScopeFullName,
  110. ASAuthorizationAppleIDRequest.ASAuthorizationScopeEmail
  111. ]
  112. })
  113. .then((res: AppleSignInResponse) => {
  114. // https://developer.apple.com/documentation/signinwithapplerestapi/verifying_a_user
  115. //alert('Send token to apple for verification: ' + res.identityToken);
  116. console.log("send token");
  117. console.log(res);
  118. //alert(res);
  119. this.wp.storeFbData(res).then( data => {
  120. console.log(data);
  121. }, error => {
  122. console.log(error);
  123. } );
  124. this.storage.set('user', { name: res.fullName.givenName + " "+res.fullName.familyName, email: res.email });
  125. this.dismiss();
  126. })
  127. .catch((error: AppleSignInErrorResponse) => {
  128. alert(error.code + ' ' + error.localizedDescription);
  129. console.error(error);
  130. });
  131. }
  132. }