# AuthorizeStep - Redirect not working

**URL:** <https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970>\
**Category:** Help Requests\
**Created:** [October 4, 2019, 1:59pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970 "2019-10-04T13:59:35Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 4, 2019, 1:59pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/1 "2019-10-04T13:59:35Z")

</div>

Hi!

I am trying to write an Aurelia-Starter for some “serverless infrastructure” start-up.  
I followed the current docs for writing an AuthorizeStep, but the redirect doesn’t work.

Step

```auto
export class AuthorizeStep {
  run(navigationInstruction, next) {
    if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
      db.ready().then(() => {
        if (!db.User.me) {
          devLog.debug('user not logged in');
          return next.cancel(
            new Redirect('login')
          );
        }
      });
    }
    return next();
  }
}

```

Routes

```auto
 config.map([
      {
        route: ['', 'home'],
        name: 'home',
        moduleId: PLATFORM.moduleName('home'),
        title: 'home',
        nav: true,
        settings: {auth: true} },
      {
        route: ['login'],
        name: 'login',
        moduleId: PLATFORM.moduleName('login'),
        title: 'login',
        // dont include this in the navigation model (router.navigation)
        nav: false
      }
    ]);

```

Am i missing something?

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 4, 2019, 2:07pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/2 "2019-10-04T14:07:00Z")

</div>

And i tried

```auto
     return next.cancel(
            new RedirectToRoute('login')
          );

```

too 😕

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [October 4, 2019, 10:54pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/3 "2019-10-04T22:54:02Z")

</div>

As usual, what was the error? Is there any chance you could help with a simple repro? YOu can base it on this  
[[JavaScript] Sandbox](https://codesandbox.io/s/y41qjr36j)  
or  
[au-script-code-sandbox](https://codesandbox.io/s/wnr6zxv6vl)

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 7, 2019, 7:04am UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/4 "2019-10-07T07:04:18Z")

</div>

Hello Bigopon,

didn´t find the time to check the forum this weekend.  
I´ll upload the project to Github this evening.

Best

---

<div class="post-metadata">

**Author:** ![mroeling](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/mroeling/32/781_2.png) [@mroeling](https://discourse.aurelia.io/u/mroeling)\
**Post date:** [October 7, 2019, 9:05am UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/5 "2019-10-07T09:05:33Z")

</div>

Have you added the step to the router config?

```auto
        config.addAuthorizeStep(this.authorizeStep);

```

And if so, I join bigupon’s request for the error and/or repo. 🙂

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 7, 2019, 6:54pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/6 "2019-10-07T18:54:44Z")

</div>

It was my fault, i tried to redirect like this

```auto
 db.ready().then(() => {
        if (!db.User.me) {
          devLog.debug('user not logged in');
          return next.cancel(
            new RedirectToRoute('login')
          );
        }
 });

```

and cancel was never called.

Thx @ all 🙂

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 8, 2019, 12:59pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/7 "2019-10-08T12:59:11Z")

</div>

@bigopon  
I enjoyed trying to put the problem in your sandbox!  
It also kind of solved the problem, since when i stripped away the imported database stuff, the redirect worked.  
🙂

---

<div class="post-metadata">

**Author:** ![bigopon](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/bigopon/32/18_2.png) [@bigopon](https://discourse.aurelia.io/u/bigopon)\
**Post date:** [October 8, 2019, 11:07pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/8 "2019-10-08T23:07:07Z")

</div>

Glad to hear that. Most of the times, its us doing silly stuff 😆

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 9, 2019, 11:19am UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/9 "2019-10-09T11:19:49Z")

</div>

True… 🙂  
What confuses me tough is, that with the code above the logging worked, but the redirect didn´t - until i lifted everything out of the the anonymous function passed to db.ready().then() 🤨

---

<div class="post-metadata">

**Author:** ![khuongduybui](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/khuongduybui/32/44_2.png) [@khuongduybui](https://discourse.aurelia.io/u/khuongduybui)\
**Post date:** [October 9, 2019, 1:27pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/10 "2019-10-09T13:27:56Z")

</div>

> [@Alfhir](#):
>
> What confuses me…

So basically you are saying the following block prints ‘user not logged in’ but does not redirect

```auto
        if (!db.User.me) {
          devLog.debug('user not logged in');
          return next.cancel(
            new RedirectToRoute('login')
          );
        }

```

while the following block does redirect

```auto
          return next.cancel(
            new RedirectToRoute('login')
          );

```

correct?

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 13, 2019, 3:16pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/11 "2019-10-13T15:16:38Z")

</div>

Hello @khuongduybui,

I forgot about answering your question, please excuse me. What I was trying to say, was that

```auto
run(navigationInstruction, next) {
    if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
      db.ready().then(() => {
        if (!db.User.me) {
          devLog.debug('user not logged in');
          return next.cancel(
            new RedirectToRoute('login')
          );
        }
      });
    }
    return next();
  }

```

did not work, but

```auto
    if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
      if (!db.User.me) {
        devLog.debug('user not logged in');
        return next.cancel(
          new RedirectToRoute('login')
        );
      }
    }

```

worked.

Best

---

<div class="post-metadata">

**Author:** ![khuongduybui](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/khuongduybui/32/44_2.png) [@khuongduybui](https://discourse.aurelia.io/u/khuongduybui)\
**Post date:** [October 13, 2019, 4:41pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/12 "2019-10-13T16:41:03Z")

</div>

I understand that taking `db.ready()` out makes redirection work.

What I want to clarify is does `devLog.debug`work in both cases and only redirection is problematic?

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [October 13, 2019, 4:53pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/13 "2019-10-13T16:53:08Z")

</div>

Something seems strange here, you’re not awaiting db.ready so it will straight away fall through to return next.

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 14, 2019, 6:53pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/14 "2019-10-14T18:53:22Z")

</div>

Yes, that was the problem.

I finally got it to work:

```auto
export class AuthorizeStep {
  async run(navigationInstruction, next) {
    let destination = next();
    if (navigationInstruction.getAllInstructions().some(i => i.config.settings.auth)) {
      await db.ready();
      if (!db.User.me) {
        devLog.debug('user not logged in');
        destination = next.cancel(
          new RedirectToRoute('login')
        );
      }
    }
    return destination;
  }
}

```

But before the redirect the page that should not get accessed is rendered for the blink of an eye. -.-

---

<div class="post-metadata">

**Author:** ![Alfhir](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/alfhir/32/1510_2.png) [@Alfhir](https://discourse.aurelia.io/u/Alfhir)\
**Post date:** [October 14, 2019, 6:59pm UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/15 "2019-10-14T18:59:39Z")

</div>

Sorry, but i could not reproduce the case where “user not logged in” was logged but no redirect happened.

---

<div class="post-metadata">

**Author:** ![zewa666](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/zewa666/32/19_2.png) [@zewa666](https://discourse.aurelia.io/u/zewa666)\
**Post date:** [October 15, 2019, 5:24am UTC](https://discourse.aurelia.io/t/authorizestep-redirect-not-working/2970/16 "2019-10-15T05:24:54Z")

</div>

You could save the let by returning next.cancel since with using async the conditional flow is now sync
