简体   繁体   中英

User always being redirected to Home page instead of requested page

In my Angular project, users are being authenticated through OIDC provider using the library angular-auth-oidc-client . So, when user is not authenticated or session expired and requests a page like https://localhost:4202/account, user is being redirected to OIDC login page and after successful login, user is always being redirected to home page instead of requested page

To Reproduce:

Steps to reproduce the behavior:

  1. Replace Auth configuration with your own config
  2. Run the project with
npm run start
  1. Go to https://localhost:4202/account. It will redirect to OIDC login page then after successful login it redirects user to https://localhost:4202 instead of https://localhost:4202/account

The issue was not storing redirectedUrl in storage and retrieve it back after successful navigation. The library already has AutoLoginPartialRoutesGuard which can be used in custom guards. See the source code in Github and sample admin guard that extends AutoLoginPartialRoutesGuard below. This will make sure user redirects back to original URL

Admin Auth Guard :


@Injectable({
    providedIn: 'root',
})
export class AdminUserAuthGuard implements CanActivate {
    constructor(
        private router: Router,
        private autoLoginPartialRoutesGuard: AutoLoginPartialRoutesGuard
    ) {}


    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
        if (this.autoLoginPartialRoutesGuard.canActivate(route, state) && this.currentUserState.hasSysAdminRole) {
            return true;
        }
        if (this.autoLoginPartialRoutesGuard.canActivate(route, state) && !this.currentUserState.hasSysAdminRole) {
            this.router.navigate(['/unauthorized']);
        }
        return false;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM