Phonegap how to direct to other page at first open app when already login

181 views Asked by At

I build an app with login and i prefer to in login state even it offline. So, i try to make it autologin with username and password that already saved in localstorage. I want when the app opened, it direct to #konfirmasi page (my main function page) (like wall page page in facebook) instead of #home page. So i have try code javascript like this in index.html:

<script type="text/javascript">
            var u_name=localStorage.getItem("uname");
            var u_pwd=localStorage.getItem("passwd");

            $(document).on('pageinit', '#home', function(){
                if(u_name==""||u_name==null||u_name=="null" ||u_name=="undefined" ||
                    u_pwd==""||u_pwd==null ||u_pwd=="null" || u_pwd=="undefined"
                    ){
                    $.mobile.changePage("#home");
                    console.log('Please Login!');
                } else{
                    $.mobile.changePage("#konfimasi");
                    console.log('You can confirm a code');
                }
            });
        </script>

it's success to log

You can confirm a code

but it still in #home page. So, what's wrong with this code? and how it should be?

This is my login script:

$(document).on('pageinit','#login',function(){
    $(document).on('click','#submit',function(){
        if($('#username').val().length>0&&$('#password').val().length>0){
            var un = $('#username').val();
            var pw = $('#password').val();
            $.ajax({
                url:'http://qrkonfirmasi.16mb.com/delivery/login.php',
                data:{  username : un,
                        password : pw
                    },
                type:'post',
                async:'false',
                dataType: 'json',
                beforeSend:function(){
                    $.mobile.loading('show',{theme:"a",text:"Please wait...",textonly:true,textVisible:true});
                },
                complete:function(){
                    $.mobile.loading('hide');
                },
                success:function(result){
                    console.log(result);
                    if(result.status==true){
                        user.name=result.message;
                        // window.localStorage.setItem('kode', JSON.stringify(result.kode));
                        // var kode = window.localStorage.getItem('kode');
                        // var kode = result.kode;
                        // console.log('Kode: ', kode);
                        // console.log('Kode length: ', JSON.parse(kode).length);
                        console.log('Login Berhasil');
                        // console.log(window.localStorage.getItem('kode'));
                        $.mobile.changePage("#konfirmasi");
                        window.localStorage.setItem('uname', un);
                        window.localStorage.setItem('passwd', pw);
                    }else{
                        alert('Login gagal. Username atau password tidak sesuai');
                    }
                },
                error:function(request,error){
                    alert('Koneksi error. Silahkan coba beberapa saat lagi!');
                }
            });
        }else{
            alert('Masukkan username dan password!');
        }
        return false;
    });
});
0

There are 0 answers