Sunday, 30 April 2017

I used node-jsonwebtoken but the app stuck on login

I used node-jsonwebtoken to create token for user but when the user login. the app doesn't response to log out order and my codes look fine cause I don't have any errors in Console So please can any one review what I wrote and tell me where's my errors
controller file called loginCTRL:

angular.module('logincontrol',['authserve'])

.controller('logctrl', function(auth, $timeout, $location)
{
var app=this;
if(auth.isloggedIn()){
  console.log('user log in');
} else{
  console.log('user is not log in');
};
this.dologin = function(loginData){

        app.waiting=true;

            auth.login(app.loginData).then(function(data){
              app.show = data.data.success
             if(data.data.success){
                app.waiting=false;
                //disapear the error msg
                app.errorMsg=false;
                //create Success Msg
               app.successMsg= data.data.message+'.....Loading';
               //redirect to home
               $timeout(function() {
                $location.path('/contact');
               }, 3000);
            }
            else{
                app.waiting=false;
                //disapear the sucess msg
                app.successMsg=false;
                //create error msg
                app.errorMsg = data.data.message;

            }
            });
        };
        this.logout = function(){
          auth.logout();
          $location.path('/logout')
          $timeout(function() {
                $location.path('/');
               }, 3000);


        };
});

service file called authserve:

angular.module('authserve', [])

.factory('auth', function($http, authtoken){

    var authFactory = {};
//User.create(regData)
    authFactory.login = function(loginData){
        return $http.post('/sign/authenticate', loginData).then(function(data){
            authtoken.setToken (data.data.token);
            return data;
        });
    };
    //authtokenFactory.is loggedIn();
    authFactory.isloggedIn = function(){
if(authtoken.getToken()){
    return true;
}
else {
    return false;
};
    };

    //auth.logout();
    authFactory.logout = function(){
        authtoken.setToken();
    };
    return authFactory;
})

.factory('authtoken', function($window){

    var authtokenFactory = {};
//User.create(regData)
    authtokenFactory.setToken = function(token){
        if(token){
            $window.localStorage.setItem('token', token);
        }
        else{
            $window.localStorage.removeItem('token');
        };

    };
    //authtokenFactory.getToken();
    authtokenFactory.getToken = function(){
        return $window.localStorage.getItem('token');
    };
    return authtokenFactory;
});

inside router:

var jwt = require('jsonwebtoken');
var secret = 'jobapp';

........

else {
            var token = jwt.sign({username: user.username, email: user.email}, secret, { expiresIn: '1h'} );
                res.json({success:true, message:'User authenticated! ^_^', token: token});
            }
        }

in html file I write:

<li><a href="#" ng-click="main.logout();">Logout</a></li>



via A.Q

No comments:

Post a Comment