angular-fullstack / generator-angular-fullstack

Yeoman generator for an Angular app with an Express server
https://awk34.gitbook.io/generator-angular-fullstack
6.12k stars 1.24k forks source link

checking authentication:true for $stateProvider #1142

Closed johntmyers closed 9 years ago

johntmyers commented 9 years ago

I saw that back in Sep there was PR #683 ... when I scaffold out a new app, browsing to /settings actually brings me to a blank page, the redirect never happens.

When I remove the event.preventDefault(); it works just fine.

kingcody commented 9 years ago

@johntmyers a couple questions, what version of the generator did you use? Also, mind posting your .yo-rc.json file?

johntmyers commented 9 years ago

generator-angular-fullstack@2.1.1

{
  "generator-angular-fullstack": {
    "insertRoutes": true,
    "registerRoutesFile": "server/routes.js",
    "routesNeedle": "// Insert routes below",
    "routesBase": "/api/",
    "pluralizeRoutes": true,
    "insertSockets": true,
    "registerSocketsFile": "server/config/socketio.js",
    "socketsNeedle": "// Insert sockets below",
    "filters": {
      "babel": true,
      "js": true,
      "html": true,
      "css": true,
      "uirouter": true,
      "bootstrap": true,
      "uibootstrap": true,
      "socketio": true,
      "mongoose": true,
      "auth": true,
      "oauth": true,
      "googleAuth": true,
      "facebookAuth": true
    }
  },
  "generator-ng-component": {
    "routeDirectory": "client/app/",
    "directiveDirectory": "client/app/",
    "filterDirectory": "client/app/",
    "serviceDirectory": "client/app/",
    "basePath": "client",
    "moduleName": "",
    "filters": [
      "uirouter"
    ],
    "extensions": [
      "babel",
      "js",
      "html",
      "css"
    ],
    "directiveSimpleTemplates": "",
    "directiveComplexTemplates": "",
    "filterTemplates": "",
    "serviceTemplates": "",
    "factoryTemplates": "",
    "controllerTemplates": "",
    "decoratorTemplates": "",
    "providerTemplates": "",
    "routeTemplates": ""
  }
}
kingcody commented 9 years ago

Try this:

---  .run(function ($rootScope, $location, Auth) {
+++  .run(function ($rootScope, $state, Auth) {
    // Redirect to login if route requires auth and you're not logged in
    $rootScope.$on('$stateChangeStart', function (event, next) {
      Auth.isLoggedInAsync(function(loggedIn) {
        if (next.authenticate && !loggedIn) {
          event.preventDefault();
---          $location.path('/login');
+++          $state.go('login');
        }
      });
    });
  });
johntmyers commented 9 years ago

Yep, that works. Should we be using $state when using ui-router then?

kingcody commented 9 years ago

We should, and it has actually been taken care of in the canary branch: https://github.com/DaftMonk/generator-angular-fullstack/commit/6aadee6d956cefb9a787eb0851d287654dfca111#diff-11

Awk34 commented 9 years ago

I'll close this, as the solution has been posted and fixed in dev branch