Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { CommunicationService } from './../../providers/communication-service';
- import { GlobalService } from './../../providers/global-service';
- import { Component } from '@angular/core';
- import { NavController, NavParams } from 'ionic-angular';
- import 'rxjs/add/operator/map';
- import { Home } from '../home/home';
- import { Regist } from '../regist/regist';
- import { Session } from '../../providers/session';
- import { HttpService } from '../../providers/http-service';
- import { Facebook, FacebookLoginResponse } from '@ionic-native/facebook';
- @Component({
- selector: 'page-login',
- templateUrl: 'login.html'
- })
- export class Login {
- public login = {
- 'email': '',
- 'password': ''
- };
- public currentMail: string = '';
- register: Regist;
- public showPassword: boolean = false;
- public mode_login;
- public prompt_register_hp;
- public fb_userId;
- public fb_result;
- public loading;
- public loginText: boolean = true;
- public processingText: boolean = false;
- public translate;
- public data;
- constructor(
- public navParam: NavParams,
- public navCtrl: NavController,
- public session: Session,
- public http: HttpService,
- public GlobalService: GlobalService,
- public communicationService: CommunicationService,
- private fb: Facebook
- ) { }
- ionViewDidLoad() {
- try {
- let loginParam: Object = this.navParam.get('loginParam');
- if (loginParam != null) {
- this.currentMail = loginParam['email'];
- this.login.email = this.currentMail;
- if (loginParam.hasOwnProperty('error') && loginParam['error'] === true) {
- // Error
- let alert = this.GlobalService.showMyAlert({
- title: loginParam['title'],
- subtitle: loginParam['message'],
- buttons: ['OK']
- });
- alert.present();
- } else {
- // not error
- let alert = this.GlobalService.showMyAlert({
- title: loginParam['title'],
- subtitle: loginParam['message'],
- buttons: ['OK']
- });
- alert.present();
- }
- }
- } catch (e) {
- this.GlobalService.consoleLog("Message Error", e);
- }
- }
- loginForm() {
- let params = {
- "link": "user/login",
- "data": this.login
- }
- let loader = this.GlobalService.showLoading({
- "spinner": "hide",
- "content": "Loading..",
- "dismissOnPageChange": true
- });
- loader.present()
- this.http.post(params).then((response) => {
- let data = response['data'][0];
- let params = {
- link: "user/profile",
- data: {
- id_user: data['user_id']
- }
- }
- this.http
- .get(params)
- .then((response) => {
- let session = {
- [Session.USER_ID]: data['user_id'],
- [Session.USER_NAME]: data['user_name'],
- [Session.USER_EMAIL]: data['user_email'],
- [Session.USER_FULL_NAME]: data['nama_lengkap'],
- [Session.IS_LOGIN]: true,
- [Session.LANGUAGE]: 'en',
- [Session.USER_PHONE]: response['data']['hp'],
- }
- console.log(session);
- this.session.setSession(session).then(result => {
- this.communicationService.announceMain('Login')
- if (this.navParam.get('pop') == true) {
- this.navCtrl.pop()
- } else {
- this.navCtrl.setRoot(Home, null);
- }
- });
- loader.dismiss();
- }).catch(error => {
- // if error
- console.log('error', error);
- loader.dismiss();
- });
- }, error => {
- loader.dismiss();
- this.GlobalService.consoleLog("Login Error", error);
- });
- }
- toRegister() {
- this.navCtrl.push(Regist, null);
- }
- resendMail(form) {
- let email = form.value.email;
- if (this.currentMail == '' && email == '') {
- let alert = this.GlobalService.showMyAlert({
- subtitle: "Email is null, try reinput your email",
- buttons: ['OK']
- });
- alert.present();
- } else {
- let loader = this.GlobalService.showLoading();
- loader.present()
- let send = {
- link: "verification/email_reg",
- data: {
- email: this.currentMail
- }
- }
- if (this.currentMail == '' && email != '') {
- send.data.email = email;
- }
- this.http.get(send).then((response) => {
- loader.dismiss();
- let alert = this.GlobalService.showMyAlert({
- title: "Success",
- subtitle: response['data']['msg'],
- buttons: ['OK']
- });
- alert.present();
- }, error => {
- loader.dismiss();
- this.GlobalService.consoleLog("Resend Email Error:", error.message);
- }).catch((cat) => {
- this.GlobalService.consoleLog("GET ERROR PROMISE", cat);
- });
- }
- }
- checkEnter(event:any) {
- let ASCII_ENTER = 13;
- if ( event.keyCode == ASCII_ENTER) {
- // if ( this.login_data.code_area.length > 1 && this.login_data.hp.length > 5 && this.login.value.password.length > 4 ) {
- this.loginForm();
- }
- }
- //START FACEBOOK FUNCTION
- loginWithFacebook(){
- this.fb.login(['public_profile', 'user_friends', 'email'])
- .then((res: FacebookLoginResponse) => {
- let loader = this.GlobalService.showLoading({
- "spinner": "hide",
- "content": "Loading..",
- "dismissOnPageChange": true
- });
- loader.present()
- console.log(Login, " Logged into Facebook!", res);
- this.fb_userId = res.authResponse.userID;
- let params = new Array();
- this.fb.api("/me?locale=en_US&fields=name,email", params)
- .then((user) => {
- this.fb_result = user;
- let response = {
- "UserID": this.fb_userId,
- "UserName": this.fb_result.name,
- "UserEmail": this.fb_result.email,
- "hp": ""
- };
- console.log(Login, " response: ", response);
- let link = MyApp.GOX_SERVER_NON_API + 'user/json_login_fb';
- let THRESHOLD = 5000;
- let postParams = {
- "link": link,
- "data": response,
- "show_error": 0
- };
- MyApp.consoleLog(Login.TAG + " Isi Post Param: ", postParams);
- this.httpServ.postServiceRTO(postParams, THRESHOLD).then((data) => {
- this.processLogin(data, response);
- }, error => {
- // dismiss loading when error login
- this.loading.dismiss();
- this.loginText = true;
- this.processingText = false;
- MyApp.consoleError(Login.TAG + "Couldn't Connect to Server", error);
- });
- })
- })
- .catch((error) => {
- MyApp.consoleLog(Login.TAG, " Error login into Facebook", error);
- this.globalService.showMyAlert({
- title: this.translate.get('gox.alert_title_msg').value,
- message: error,
- buttons: [{
- text: this.translate.get('gox.label_ok').value
- }]
- });
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement