Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //E.g Github comment "login, facebook login , Google login feature added"
- import {Component} from '@angular/core';
- import {IonicPage, NavController, NavParams, ModalController, Events, Platform} from 'ionic-angular';
- import {FormBuilder, FormGroup, Validators, AbstractControl} from '@angular/forms';
- import {RemoteServiceProvider} from '../../providers/remote-service/remote-service';
- import {RegistrationPage} from '../registration/registration';
- import {HomePage} from '../home/home';
- import {LoginhelpPage} from '../loginhelp/loginhelp';
- import {GooglePlus} from '@ionic-native/google-plus';
- import {Facebook} from '@ionic-native/facebook';
- import {SocialregPage} from '../socialreg/socialreg';
- import {OneSignal} from '@ionic-native/onesignal';
- /**
- * Page for normal login, facebook login , Google login
- * Add by Rahi
- * Date : 8th nov, 17
- */
- @IonicPage()
- @Component({
- selector: 'page-login',
- templateUrl: 'login.html',
- })
- export class LoginPage {
- pages: any;
- loginForm: FormGroup;
- submitAttempt: boolean = false; // bool var for to set and show error on button click
- registrationPage = RegistrationPage;
- isfbLoggedIn: boolean = false;
- onesignalplayerid = ''
- onesignaltoken = ''
- constructor(public navCtrl: NavController, public navParams: NavParams, public formBuilder: FormBuilder,
- public remotService: RemoteServiceProvider, public modalCtrl: ModalController, private storage: Storage
- , public events: Events, private fb: Facebook, private googlePlus: GooglePlus, private oneSignal: OneSignal, platform: Platform) {
- //Initialize login form validation
- this.loginForm = formBuilder.group({
- username: ['', Validators.compose([Validators.minLength(4), Validators.maxLength(50), Validators.required])],
- password: ['', Validators.compose([Validators.minLength(4), Validators.maxLength(20), Validators.required])]
- });
- window.localStorage['fbprofileinfo'] = ''; // make previously stores fb info empty
- // if it is in real device get onesignal player ID for push notification
- if (platform.is('cordova')) {
- this.oneSignal.getIds().then((ids) => {
- // console.log("One signal ids"+ids.userId);
- this.onesignalplayerid = ids.userId;
- this.onesignaltoken = ids.pushToken;
- });
- }
- }
- /*
- * Google login handler
- * Add by Rahi
- * Date : 8th nov, 17
- */
- googleLogIn() {
- this.googlePlus.login({})
- .then(res => {
- // console.log("google login Response", res);
- var googleparams = {
- google_id: res.userId,
- email: res.email,
- }
- this.remotService.presentLoading("Please wait ...");
- this.remotService.postData(googleparams, 'googlelogin').subscribe((response) => {
- this.remotService.dismissLoader();
- if (response.success == 1) {
- var dataRes = response.data;
- window.localStorage['usertype'] = dataRes.user_type;
- window.localStorage['userid'] = dataRes.user_id;
- window.localStorage['token'] = dataRes.token;
- window.localStorage['username'] = dataRes.username;
- window.localStorage['premium_user'] = dataRes.premium_user;
- window.localStorage['name'] = dataRes.name;
- // fire event in app.component to show the header
- this.events.publish('user:loggedin');
- this.remotService.presentToast('Logged in successfully.');
- } else {
- var params = {data: res, type: 'google'}
- this.navCtrl.push(SocialregPage, params);
- //window.localStorage['fbprofileinfo'] = JSON.stringify(res);
- }
- }, () => {
- this.remotService.dismissLoader();
- this.remotService.presentToast('Error!');
- });
- })
- .catch(err => console.error(err));
- }
- /*
- * FB login handler
- * Add by Rahi
- * Date : 8th nov, 17
- */
- faceBooklogin() {
- this.fb.login(['public_profile', 'user_friends', 'email'])
- .then(res => {
- // if connected get facebook user details
- if (res.status === "connected") {
- this.isfbLoggedIn = true;
- this.getFbUserDetail(res.authResponse.userID);
- } else {
- this.isfbLoggedIn = false;
- }
- })
- .catch(e => console.log('Error logging into Facebook', e));
- }
- /*
- * FB Get user details after getting authtoken & Check if user exist
- * Add by Rahi
- * Date : 8th nov, 17
- */
- getFbUserDetail(userid) {
- this.fb.api("/" + userid + "/?fields=id,email,name,picture,token_for_business", ["public_profile"])
- .then(res => {
- console.log(res);
- var facemail = res.hasOwnProperty("email") ? res.email : null;
- var fbParams = {
- email: facemail,
- facebook_id: res.token_for_business,
- };
- this.remotService.presentLoading("Please wait ...");
- this.remotService.postData(fbParams, 'Facebooklogin').subscribe((response) => {
- this.remotService.dismissLoader();
- if (response.success == 1) {
- // If user exist log in user
- var dataRes = response.data;
- console.log(dataRes);
- window.localStorage['usertype'] = dataRes.user_type;
- window.localStorage['userid'] = dataRes.user_id;
- window.localStorage['token'] = dataRes.token;
- window.localStorage['username'] = dataRes.username;
- window.localStorage['premium_user'] = dataRes.premium_user;
- window.localStorage['name'] = dataRes.name;
- // fire event in app.component to show the header
- this.events.publish('user:loggedin');
- this.remotService.presentToast('Logged in successfully.');
- } else {
- var params = {data: res, type: 'facebook'}
- this.navCtrl.push(SocialregPage, params);
- //window.localStorage['fbprofileinfo'] = JSON.stringify(res);
- }
- }, () => {
- this.remotService.dismissLoader();
- this.remotService.presentToast('Error!');
- });
- //this.users = res;
- })
- .catch(e => {
- this.remotService.presentToast("Error logging in using facebook.");
- console.log(e);
- });
- }
- }
Add Comment
Please, Sign In to add comment