Hi Damien,
I have a similar pattern in my application.
I'm using abstract states to handle redirection depending on
authentification.
I think you should setup a abstract state on '/' like this :
.state('root', {
abstract : true,
resolve : {
anonymous : redirectIfNoAuthenticatedMethod
},
template : '<ui-view></ui-view>'
})
and then define your routes under this state.
Tell me if your find this helpful.
Charly.
On Tuesday, 5 August 2014 12:22:30 UTC+2, Damien Metcalf wrote:
>
>
> I am still trying to figure out how I can set up the redirects to a page
> if a user is logged in as I mentioned below. Any ideas on how I could do
> this?
>
> On Monday, 4 August 2014 22:45:07 UTC+12, Damien Metcalf wrote:
>>
>> I have given this a bit more thought, and I have decided perhaps the best
>> thing for me to do would be to set it up so when a user visits '/' they are
>> either redirected to '/popular' if they are logged in and if they are not
>> logged in they see '/'. How might I be able to set something like this up?
>>
>> On Thursday, 31 July 2014 03:36:40 UTC+12, Eric Eslinger wrote:
>>>
>>> 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.