angular-auth-oidc-client icon indicating copy to clipboard operation
angular-auth-oidc-client copied to clipboard

Redirect after login

Open evoronovich opened this issue 7 years ago • 11 comments

Hi! I have the such question: I have auth-guard, which is checking isAuthorized(); If no authorized, I save redirect_url in local storage, it's redirecting user to login page, user login, in the IDS we have redirect url to the '/callback'. In the AppRouting I have such entry: {path: '', redirectTo: 'dashboard', pathMatch: 'full'}, The question how app-routing and oidc client should be configured to redirect user to the page, that he has requested before? Ex. User enters http://localhost/jobs, unauthorized, login, redirecting to the dashboard (but I need redirect to jobs). There are some info in documentation about it, but it's not clear for me. Could you help?

evoronovich avatar Feb 07 '19 09:02 evoronovich

@evoronovich I have docs for this in the API documentation

damienbod avatar Apr 26 '19 06:04 damienbod

Where is his documented exactly? The authorize() function seems to use the static url from this.authConfiguration.redirect_url. The only thing I could think of is to pass an urlHandler to authorize() and rewrite the url but that seems hacky.

codemasterover9000 avatar Aug 15 '19 14:08 codemasterover9000

I will add a code exmample for this. This describes the event which can be used.

https://github.com/damienbod/angular-auth-oidc-client/blob/master/API_DOCUMENTATION.md#onauthorizationresult-observable

You should not use the guard on the default route. You need to save your path somewhere in localstorage or sessionstorage before the authorize request, and use this after in the event.

damienbod avatar Aug 19 '19 06:08 damienbod

Thanks!

codemasterover9000 avatar Aug 19 '19 07:08 codemasterover9000

Hello, @damienbod, I cannot find any example for this. the url that you sent does not exist now. can you send it again. :) Thank you.

HedayatTajik avatar Jul 14 '21 08:07 HedayatTajik

Hi @HedayatTajik The docs were moved and we didn't update this issue:

here's the new link:

https://nice-hill-002425310.azurestaticapps.net/docs/documentation/public-api

Greetings Damien

damienbod avatar Jul 15 '21 08:07 damienbod

Thank you


Von: damienbod @.> Gesendet: Donnerstag, 15. Juli 2021 10:54 An: damienbod/angular-auth-oidc-client @.> Cc: Hedayat Tajik @.>; Comment @.> Betreff: Re: [damienbod/angular-auth-oidc-client] Redirect after login (#386)

The docs were moved, didn't update this issue:

https://nice-hill-002425310.azurestaticapps.net/docs/documentation/public-api

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/damienbod/angular-auth-oidc-client/issues/386#issuecomment-880520697, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AQYQ66JT3S5VKYLHA5CWX5LTX2O4LANCNFSM4GU3W4HQ.

HedayatTajik avatar Jul 16 '21 09:07 HedayatTajik

I'm having a similar problem. I'm using angular-auth-oidc-client 15.0.3.

export class AuthGuard implements CanActivate {
  constructor(private oidcSecurityService: OidcSecurityService) { }

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    return this.oidcSecurityService.checkAuth()
      .pipe(map((response) => {
        if (response?.isAuthenticated) return true;
        
        const url = state.url;
        this.oidcSecurityService.authorize(null, {redirectUrl: url});
        return false;
      }));
  }
}

The authorization server is responding with 404 to the authorize request. The URL requested looks like this: `https://authority/oauth/authorize?client_id=xxx&redirect_uri=%2Fmypage&response_type=code&scope=openid%20profile%20email&...

If I hardcode the full url in the authorize call: this.oidcSecurityService.authorize(null, {redirectUrl: 'https://mydomain/root_app_path/mypage'});`

I get the following error from the authority: "The specified 'redirect_uri' parameter doesn't match the client redirection endpoint the authorization code was initially sent to."

If I change the redirectUrl in the OpenIdConfiguration to hardcode my very page, then everything works:

export const OidcConfig: OpenIdConfiguration = {
    // ...
    redirectUrl: 'https://mydomain/root_app_path/mypage',
    // ...
};

Do you have any pointers please?

Thanks.

marculescu avatar Feb 13 '23 07:02 marculescu

Passing the redirect URI in the config is the recommended way because the IDP has to know all redirectUrls in prior, and he matches them. You have to implement the "when the IDP redirects to my app then I want to redirect to the route the user was before" by yourself or you can also try the "AutoLoginPartialRoutesGuard" from the lib.

FabianGosebrink avatar Feb 13 '23 07:02 FabianGosebrink

Thanks for the quick reply. I incorrectly assumed that providing the redirectUrl in authorize() will actually do what AutoLoginPartialRoutesGuard does for me. For anyone else landing here, it's worth reading @FabianGosebrink's explanation here.

marculescu avatar Feb 13 '23 11:02 marculescu

Thanks for linking the response. I should add this response to the docs :D Would have searched it as well. Thanks!

FabianGosebrink avatar Feb 13 '23 12:02 FabianGosebrink