Advertisement
Guest User

login

a guest
May 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { Component } from '@angular/core';
  2. import { IonicPage, NavController, NavParams, AlertController} from 'ionic-angular';
  3. import { RegisterPage } from '../register/register';
  4. import { FormBuilder, FormGroup } from '@angular/forms';
  5. import { Storage } from '@ionic/storage';
  6. import { Http } from '@angular/http';
  7. import 'rxjs/add/operator/map';
  8. import { TabsPage } from '../tabs/tabs';
  9.  
  10. @IonicPage()
  11. @Component({
  12.   selector: 'page-login',
  13.   templateUrl: 'login.html',
  14. })
  15. export class LoginPage {
  16.  
  17.     credentialsForm: FormGroup;
  18.     usernamee:string;
  19.     passwordd:string;
  20.    
  21. constructor(
  22. public navCtrl: NavController,
  23. public navParams: NavParams,
  24. public alertCtrl: AlertController,
  25. public http : http,
  26. private formBuilder: FormBuilder,
  27. private storage: Storage)  {
  28.     this.credentialsForm = this.formBuilder.group({
  29.         password: [''],
  30.         username: ['']
  31.     });
  32.     this.passwordd = "";
  33.     this.usernamee = "";
  34. }
  35.  
  36. ionViewDidLoad(){
  37.     console.log('ionViewDidLoad LoginPage');
  38. }
  39.  
  40. showAlert(){
  41.     let alert = this.alertCtrl.create({
  42.         title: 'Login Berhasil',
  43.         subtitle: 'Yes, Login Sukses',
  44.         buttons: ['OK']
  45.     });
  46.     alert.present();
  47. }
  48. showAlert2(){
  49.     let alert = this.alertCtrl.create({
  50.         title: 'Login Gagal',
  51.         subtitle: 'No, Gagal',
  52.         buttons: ['OK']
  53.     });
  54.     alert.present();
  55. }
  56.  
  57. cekstorage(){
  58.     console.log('Your Username is', this.credentialsForm.controls['username'].value)
  59.     console.log('Your Password is', this.credentialsForm.controls['password'].value)
  60.     this.storage.get('username').then((val) => {
  61.         this.usernamee = val;
  62.         console.log('Username di storage', val);
  63. });
  64.     this.storage.get('password').then((val) => {
  65.         this.passwordd = val;
  66.         console.log('password di storage', val);
  67. });
  68.     if (this.credentialsForm.controls['username'].value == this.usernamee && this.credentialsForm.controls['password'].value == this.passwordd)
  69.         this.showAlert();
  70.     else
  71.         this.showAlert2();
  72. }
  73. gotoRegister(){
  74.     this.navCtrl.push(RegisterPage);
  75. }
  76.  
  77. doLogin()
  78. {
  79.     let data = {
  80.         username: btoa(this.username),
  81.         password: btoa(this.password)
  82.     };
  83.     let url = 'http://lab.oo/index.php?/login/dologin';
  84.  
  85.     let dataJSON = JSON.stringify(data);
  86.  
  87.     this.http.post(url, dataJSON)
  88.     .map(res => res.json())
  89.     .subscribe(
  90.         data => {
  91.  
  92.             if(data.status == 'true')
  93.             {
  94.                 this.navCtrl.setRoot(TabsPage);
  95.                 console.log('login succes');
  96.             }
  97.             else
  98.             {
  99.                 this.showAlert('Login Gagal, username atau password salah');
  100.                 console.log('login failed');
  101.             }
  102.         });
  103. }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement