AuthorizeStep - Redirect not working

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

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

 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?

1 Like

And i tried

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

too :confused:

1 Like

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
or
au-script-code-sandbox

1 Like

Hello Bigopon,

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

Best

1 Like

Have you added the step to the router config?

        config.addAuthorizeStep(this.authorizeStep);

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

2 Likes

It was my fault, i tried to redirect like this

 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 :slight_smile:

1 Like

@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.
:slight_smile:

1 Like

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

1 Like

True… :slight_smile:
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() :face_with_raised_eyebrow:

1 Like

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

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

while the following block does redirect

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

correct?

1 Like

Hello @khuongduybui,

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

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

    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

1 Like

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

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

1 Like

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

1 Like

Yes, that was the problem.

I finally got it to work:

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. -.-

1 Like

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

1 Like

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

2 Likes