I have a similar thing, but instead of having an actual login state, I
loaded all my login controls into the base scope (the scope on the main,
top level element controller), and attached a modal-style high-zindex div
to the site header.

That div has an ng-if on it. So if the user isn't logged in, or logs out, I
set user.apiToken to false, and the "click here to pick an OAUTH provider"
div appears.

The benefit to this is, it keeps the user from having to re-navigate to
their preferred location after a successful login (otherwise it's
annoying). This way users coming in on a deep link (say they got an email
with one) land on the right place without having to deal with fancy state
memory in the login route.

If you really want to do those sorts of things, I'd consider putting the
login test inside a resolve block on your state, and then catch
$stateChangeErrors to decide if they derive from being not-authorized (403)
or not-authenticated (401). 403 (and 404 while we're here) errors tend to
be permanent, and probably *do* need either a redirect or at least a toast,
but 401s are temporary.

Finally, I prefer $state.go 'app.login' over $location.path, as it
continues to exist within the ui-router's state controller rather than
manually setting a url.

e




On Tue, Jul 29, 2014 at 10:58 PM, Damien Metcalf <[email protected]> wrote:

> If user visits the base url or '/' and they are *not logged in* to the
> application I would like them to see the *'welcome' state*. However *if a
> user is logged in* I would like them to see the *'home' state*. So a user
> would be able to browse around the site but when ever they went to '/' they
> will only see the state they have permission to see.
>
> I am using the angular-fullstack generator with express, sockets & MongoDB
> and Ui-Router
>
> Here is how I currently have things:
>
> 'use strict';
>
>
> angular.module('cbuiRouterApp', [
>   'ngCookies',
>   'ngResource',
>   'ngSanitize',
>   'ui.bootstrap',
>   'btford.socket-io',
>   'ui.router',
> ])
>
>   .config(function ($stateProvider, $urlRouterProvider, $locationProvider,
> $httpProvider) {
>     $urlRouterProvider
>       .otherwise('/');
>
>     $stateProvider
>       .state('main', {
>         url: '/',
>         templateUrl: 'app/main/main.html',
>         controller: 'MainCtrl',
>         title: 'Main',
>         mainClass: 'main'
>       })
>       .state('welcome', {
>         url: '/',
>         templateUrl: 'app/welcome/welcome.html',
>         controller: 'WelcomeCtrl',
>         title: 'Welcome',
>         mainClass: 'welcome'
>       })
>            .state('login', {
>         url: '/login',
>         templateUrl: 'app/account/login/login.html',
>         controller: 'LoginCtrl'
>             });
>
>
>     $locationProvider.html5Mode(true);
>     $httpProvider.interceptors.push('authInterceptor');
>   })
>
>
>   .factory('authInterceptor', function ($rootScope, $q, $cookieStore,
> $location) {
>     return {
>       // Add authorization token to headers
>       request: function (config) {
>         config.headers = config.headers || {};
>         if ($cookieStore.get('token')) {
>           config.headers.Authorization = 'Bearer ' + $cookieStore.get(
> 'token');
>         }
>         return config;
>       },
>
>
>       // Intercept 401s and redirect you to login
>       responseError: function(response) {
>         if(response.status === 401) {
>           $location.path('/login');
>           // remove any stale tokens
>           $cookieStore.remove('token');
>           return $q.reject(response);
>         }
>         else {
>           return $q.reject(response);
>         }
>       }
>     };
>   })
>
>
>   .run(function ($rootScope, $location, Auth) {
>     // Redirect to login if route requires auth and you're not logged in
>     $rootScope.$on('$stateChangeStart', function (event, next) {
>       if (next.authenticate && !Auth.isLoggedIn()) {
>         $location.path('/login');
>       }
>     });
>   });
>
>
>
>  --
> You received this message because you are subscribed to the Google Groups
> "AngularJS" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/angular.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to