IONIC 3 - How to redirect page after success login parse?


#1

Hi,
IONIC 3.20.0, CORDOVA 8.0.0, ANGULAR 5

How to redirect page after success login parse?

Bellow My code part

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';
import { Usuario } from '../autenticacao/usuario.model';
import { ConfigProvider } from '../../providers/config/config';
import { TabsPage } from '../tabs/tabs';

import * as Parse from 'parse';
let parse = require('parse');

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
  providers: [
    ConfigProvider
  ]
})
export class LoginPage {
  public caddados = new Usuario();
  constructor(public navCtrl: NavController, 
              public navParams: NavParams,
              private configProvider: ConfigProvider
            ) {

  }

goConfirm(caddados: Usuario){
Parse.User.logIn(caddados.name,caddados.password, {
  success: function(user) {
  }, error: function(user, error) { 
      if(error.code == 101) {
          user.save({
            username: caddados.name,
            email: caddados.email,
            password: caddados.password
            }, {
              success: function(response) {
                 ---> I trying all this below, but nothing works! <--
                //setTimeout(()=>this.navCtrl.setRoot('TabsPage'));
                //setTimeout(()=>this.navCtrl.setRoot('login'))
                //this.nav.setRoot(TabsPage);
                //this.nav.setRoot(TabsPage);
                //window.location.href = 'page-tabs';
                //window.location.href="user_home.html";  
                //window.location.href='tabs.html';
                //this.navCtrl.push(TabsPage);
                //this.nav.push(TabsPage);
                //this.goTransPage();

#2

Format your code properly. That is unreadable.


#3

Ok, I did, Is better now?


#4

Okay, that’s an improvement regarding formatting for sure but quite a bit of it is still a bit confusing. I don’t know anything about parse, or what or why a 101 error code is, but my first question is does that success block actually run? Put a breakpoint in there and see

Because looking at your code, on successful login you do absolutely nothing, but on error, if that error is 101(?), save the user returned in the error (?), and if that save of the user returned in the error is successful…then run your redirect.

Just looking at that makes zero sense to me, maybe something with that Parse library, no idea. But check if that block of code even runs. My guess is no.


#5

If success of Parse.User.logIn because exist User, then I do absolutely nothing, bu if f that error is 101, I going save User, do you undestand?

i already checked, the return error code is 101 and user.save() it’s working, it’s finished saving user, but after that, my page redirect don’t works.


#6

The below code also don’t works

goConfirm(caddados: Usuario){

  var user = new Parse.User();
  user.set("username", caddados.nome);
  user.set("password", caddados.senha);
  user.set("email", caddados.email);    
  user.signUp( {
    success: function(user) {
      // Hooray! Let them use the app now.
      this.navCtrl.push(TabsPage);  <--- Don't Works
    },
    error: function(user, error) {
      // Show the error message somewhere and let the user try again.
      alert("Error: " + error.code + " " + error.message);
    }
  });

#7

@rlouie asked the important question:


#8

Honestly, I don’t believe this. The Javascript SDK only allows you to run save if you’ve already got a legit user credential.


#9
    Parse.User.logIn(caddados.nome,caddados.senha, {
      success: function(user) {
      }, error: function(user, error) { 
          // The login failed. Check error to see why.
          if(error.code == 101) {
              user.save({
                username: caddados.nome,  //'sampeusername',
                email: caddados.email,    //'sample@email.com',
                password: caddados.senha  //'123456'
                }, {
                  success: function(response) {
                    console.log('success: function(response) is run...')
                    this.navCtrl.push(TabsPage);


#10

This question dont is about save user, it’s about page redirect after SignUp or SignIn of User.


#11

Well, I have to guess this is something to do with how Parse works then, so I can’t gaurantee an answer for you, since I don’t use it. But I’d recommend you switch away from whatever magic parse thinks it’s doing and use their promise syntax (although based on their docs it seems like they might use their onw promise implementation). Try something like this, I have no idea if this will wokr though.

Parse.User.logIn(caddados.nome, caddados.senha)
  .catch((user, error) => {
    if (error.code === 101) {
      return user.save({
        username: caddados.nome,  //'sampeusername',
        email: caddados.email,    //'sample@email.com',
        password: caddados.senha  //'123456'
      });
    }
  })
  .then(successResponse => {
    return this.navCtrl.push(TabsPage);
  })
  .catch(error => console.log(error));
  

That’s kind of thrown together, could be cleaned up a little but…worth a shot.

Just occurred to me that your logIn function probably doesn’t return a promise so…you might need more changes.


#12


#13

Ya, that’s exactly what I said at the end of my comment, your logIn function doesn’t return a promise. So…you would also have to change that to return a promise. Idk what your login function does so hard for me to advise you any further on that without knowing what that does


#14

I had got solv, see below.

    var user = new Parse.User();
    user.set("username", caddados.nome);
    user.set("password", caddados.senha);
    user.set("email", caddados.email);    
    user.signUp( {
      success: function(user) {
      },
      error: function(user, error) {
        alert("Error: " + error.code + " " + error.message);
      }
    }).then(()=>{this.navCtrl.push(TabsPage);});

#15

For those wondering, the ‘this’ in your success callback does not refer to the instance of LoginPage and thus does not have access to the navCtrl. The reason your ‘.then()’ callback works is because it uses the fat arrow syntax which auto binds ‘this’ to the instance of LoginPage.