Angular doesn't redirect me to the specified page after user login.
I'm using wso2 Identity Server to authenticate the users. I want to use the Authorization Code Flow.
In my angular application (in which I use the dependency angular-oauth2-oidc) I have a login page that contains only a button that redirects to the wso2 authorization server. After I log in, i'm not redirected to the right page.
I have a service:
const oAuthConfig : AuthConfig = {
issuer: 'https://localhost:9443/oauth2/token',
strictDiscoveryDocumentValidation: false,
redirectUri: 'http://localhost:4200/dashboard',
postLogoutRedirectUri: 'http://localhost:4200/login',
tokenEndpoint: 'https://localhost:9443/oauth2/token',
userinfoEndpoint: 'https://localhost:9443/oauth2/userinfo',
clientId: '{{clientId}}',
responseType: 'code',
scope: 'openid',
showDebugInformation: true
}
@Injectable({
providedIn: 'root'
})
export class OauthWso2IdentityServerService {
constructor(private readonly oauthService: OAuthService) {}
loginWithWso2IdentityServer() {
this.oauthService.configure(oAuthConfig);
this.oauthService.loadDiscoveryDocument().then(() => {
this.oauthService.tryLoginCodeFlow().then(() => {
if(!this.oauthService.hasValidAccessToken()) {
//initalize the login flow
this.oauthService.initCodeFlow()
} else {
this. oauthService.loadUserProfile().then((userProfile) => {
console.log(JSON.stringify(userProfile));
})
}
})
})
}
}
In my LoginComponent i have:
constructor(private wso2: OauthWso2IdentityServerService) {}
login() {
this.wso2.loginWithWso2IdentityServer();
}
login.html:
<button (click)="login()">login</button>
then I implemented a guard:
export class AuthGuard implements CanActivate {
constructor(private oauthService: OAuthService, private router: Router) {}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.oauthService.hasValidIdToken()) {
this.router.navigate(['/dashboard']);
return true;
}
return false;
}
}
in app-routing.module.ts:
{ path: 'dashboard', loadChildren: () => import('./features/dashboard/dashboard.module').then(m => m.DashboardModule), canActivate: [AuthGuard] },
I get redirected to: http://localhost:4200 (that is a blank page)