简体   繁体   English

登录后如何重定向

[英]How to redirect after login

I am trying to redirect a user to different page after user is authenticated. 我在用户通过身份验证后尝试将用户重定向到其他页面。 I am using jwt authentication and I tried with $location , $window for redirection but its throwing error $state.go is not a function . 我正在使用jwt身份验证,我尝试使用$location$window进行重定向但是它的抛出错误$state.go is not a function I am new to angular and I am guessing there should be way to redirect may using a service in angular but I am still new to factories and service. 我是角度新手,我猜应该有方法重定向可能使用角度服务,但我仍然是工厂和服务的新手。

I have my state provider like this in state.js file: 我在state.js文件中有我的状态提供程序:

myApp.config(function ($stateProvider, $urlRouterProvider) {

    // default route
    $urlRouterProvider.otherwise("/Home");
    var header = {
        templateUrl: 'commonViews/Header.html',
        controller: function ($scope) {
        }

    };
    var footer = {
        templateUrl: 'commonViews/Footer.html',
        controller: function ($scope) {
        }
    };
    // ui router states
$stateProvider
    .state('Home', {
        url: "/Home",
        views: {
            header: header,
            content: {
                templateUrl: 'views/HomePage.html',
                controller: function ($scope) {
                }
            },
            footer: footer
        }
    })
    .state('LoggedIn', {
        url: "/LoggedIn",
        views: {
            'header': header,
            'content': {
                templateUrl: 'views/LoggedIn.html',
                controller: function () {
                }
            },
            'footer': footer
        }
    });
});

and loginController.js : loginController.js

myApp.controller('loginController', ['$scope', '$http', 'jwtHelper', '$localStorage', '$state', function ($scope, $http, jwtHelper, $localStorage, $sessionStorage, $state)
{
$scope.email = "";
$scope.password = "";
$scope.token = "";

$scope.loginForm = function () {

    var data = {email: $scope.email, password: $scope.password};
    var url = 'rs/loginResource/login';
    $http.post(url, data).then(function (response)
    {
        $localStorage.token = response.data.token;
        console.log("Encoded Token in localstorage is:" + $localStorage.token);

        if ($localStorage.token) {
         // $location.url("/LoggedIn");
            $state.go('/LoggedIn');
        }

    }, function (error)
    {
        console.log("error", error);
    });
};
}]);

further I have to perform refresh token based on expiration time etc, so is it better to have separate the functions like using a service to do the signup and signin? 此外,我必须根据到期时间等执行刷新令牌,因此最好是单独使用服务进行注册和登录等功能吗?

The problem is the definition of your controller and the way you're handling your injections. 问题在于控制器的定义以及处理注射的方式。 And no, referring to your own answer to your question, the problem is not the "order" of the injections. 不,指的是你自己对问题的回答,问题不在于注射的“顺序”。 It's a bit worse. 这有点糟糕。

myApp.controller('loginController', ['$scope', '$http', 'jwtHelper', '$localStorage', '$state', function ($scope, $http, jwtHelper, $localStorage, $sessionStorage, $state)

in this code you're mapping '$scope' to a $scope variable, '$http' to $http , 'jwtHelper' to jwtHelper , '$localStorage' to $localStorage and '$state' to $sessionStorage , and you're not mapping anything to $state . 在这段代码中,你将'$scope'映射到$scope变量, '$http'映射到$http'jwtHelper' jwtHelperjwtHelper'$localStorage' jwtHelper$localStorage'$state' jwtHelper$sessionStorage ,你'不要将任何东西映射到$state So obviously you get an error when you try to call a method on an undefined $state variable. 因此,当您尝试在未定义的$state变量上调用方法时,显然会出现错误。 So in short, you're injecting 5 dependencies and you're assigning 6 variables to your dependencies, which in turn results in things not doing what they're supposed to do. 简而言之,您正在注入5个依赖项,并且您正在为依赖项分配6个变量,这反过来会导致事情无法执行它们应该执行的操作。

您可以使用Angular $窗口:

$window.location.href = '/index.html';

$state. go $state. go accepts the view name , not the URL . $state. go接受视图名称 ,而不是URL Replace '/LoggedIn' with Logged and you should be good. Logged替换'/LoggedIn'你应该很好。

You need to add $window as a dependency to your controller if you are using $window, 如果使用$ window,则需要将$ window添加为控制器的依赖项,

myApp.controller('loginController', ['$scope', '$http', 'jwtHelper', '$localStorage', '$state','$window', function ($scope, $http, jwtHelper, $localStorage, $sessionStorage, $state,$window)
{
$window.location.href = '/index.html';
}

otherwise change the route like this, here also you need to inject $state, 否则改变这样的路线,这里你也需要注入$ state,

  myApp.controller('loginController', ['$scope', '$http', 'jwtHelper', '$localStorage', '$state','$window','$state', function ($scope, $http, jwtHelper, $localStorage, $sessionStorage, $state,$window,$state)
{
      $state.go('LoggedIn');
}

Use $state.go instead of $window and location . 使用$ state.go而不是$windowlocation
Add $state injector on controller 在控制器上添加$ state注入器
write code $state.go('LoggedIn'); 编写代码$state.go('LoggedIn');
Instead $state.go('/LoggedIn'); 而是$state.go('/LoggedIn');

Write state name('LoggedIn') instead of url('/LoggedIn'). 写状态名称('LoggedIn')而不是url('/ LoggedIn')。

Hopefully this will work in your case. 希望这将适用于您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM