2014-08-28 3 views
1

사용자가 / 경로에 로그인합니다.로그인 한 후 사용자를 새로운 경로로 안내하는 방법은 무엇입니까?

  1. 어떻게 내 응용 프로그램 다른보기 /app

    /packages/system/public/views/index.html 
    

    /보기에서 그들을 직접합니까?

    /packages/system/public/views/app.html 
    
  2. 나는 그렇게 만 액세스 할 수있는 사용자 로그인이보기 /app 보안되고 싶어요. 로그인하지 않은 사용자는 /으로 다시 보내야합니다. /packages/users/controllers/meanUser.js에서

답변

1

// Register the login() function 
    $scope.login = function() { 
    $http.post('/login', { 
     email: $scope.user.email, 
     password: $scope.user.password 
    }) 
     .success(function(response) { 
     // authentication OK 
     $scope.loginError = 0; 
     $rootScope.user = response.user; 
     $rootScope.$emit('loggedin'); 
     if (response.redirect) { 
      if (window.location.href === response.redirect) { 
      //This is so an admin user will get full admin page 
      window.location.reload(); 
      } else { 
      window.location = response.redirect; 
      } 
     } else { 
      // Redirect Here 
      $location.url('/'); 
      $location.url('/articles'); // Will take you to the articles view after login 
     } 
     }) 
     .error(function() { 
     $scope.loginerror = 'Authentication failed.'; 
     }); 
    }; 

그들이 당신이 할 수있는 로그인하지 않고 보안 경로를 액세스 할 때 다른 페이지로 리디렉션하는 사용자가 필요한 경우 /packages/articles/public/routes/articles.js의 코드를 참조하십시오.

// This function checks if the user is logged in and redirects to the login page. 
var checkLoggedin = function($q, $timeout, $http, $location) { 
    // Initialize a new promise 
    var deferred = $q.defer(); 

    // Make an AJAX call to check if the user is logged in 
    $http.get('/loggedin').success(function(user) { 
    // Authenticated 
    if (user !== '0') $timeout(deferred.resolve); 

    // Not Authenticated 
    else { 
     $timeout(deferred.reject); 
     $location.url('/login'); 
    } 
    }); 

    return deferred.promise; 
}; 

// These are your defined routes. 
$stateProvider 
    .state('all articles', { 
    url: '/articles', 
    templateUrl: 'articles/views/list.html', 
    // This resolve runs the checkLoggedin function as the route is accessed 
    // and redirects if the user isn't logged in. 
    resolve: { 
     loggedin: checkLoggedin 
    } 
    }); 
+0

위대한 코드입니다. 사용자가 로그인하지 않고/app를 방문하면/auth/login으로 어떻게 보내나요? –

+0

@MichaelCole auth'd 코드가 아니라면 리디렉션을 포함하도록 원래 답변을 편집했습니다. 희망이 도움이됩니다. – itaylorweb

+0

누구나 더 나은 해결책이 있습니까? –