-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NbOAuth2AuthStrategy show errors from login response #6010
Comments
up the issue since I never got an answer in 9 months. I have solved the issue by extending the import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, from, of as observableOf } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { NbAuthIllegalTokenError, NbAuthResult, NbAuthStrategyClass, NbOAuth2AuthStrategy, NbOAuth2AuthStrategyOptions } from '@nebular/auth';
import axios, { AxiosHeaders, AxiosError } from "axios";
@Injectable()
export class KeycloakAuthStrategy extends NbOAuth2AuthStrategy {
static setup(options: NbOAuth2AuthStrategyOptions): [NbAuthStrategyClass, NbOAuth2AuthStrategyOptions] {
return [KeycloakAuthStrategy, options];
}
private httpHeadersToAxiosHeaders(header: HttpHeaders): AxiosHeaders {
const axiosHeaders = new AxiosHeaders({});
header.keys().forEach(key => {
axiosHeaders.set(key, header.get(key))
});
return axiosHeaders;
}
passwordToken(username: string, password: string): Observable<NbAuthResult> {
const module = 'token';
const url = this.getActionEndpoint(module);
const requireValidToken = this.getOption(`${module}.requireValidToken`);
const axiosPromise = axios.post(url, this.buildPasswordRequestData(username, password), { headers: this.httpHeadersToAxiosHeaders(this.getHeaders()) });
return from(axiosPromise).pipe(
map((res) => {
res = res.data;
return new NbAuthResult(
true,
res,
this.getOption('redirect.success'),
[],
this.getOption('defaultMessages'),
this.createToken(res, requireValidToken),
);
}),
catchError((res) => this.handleResponseError(res)),
);
}
protected handleResponseError(res: any): Observable<NbAuthResult> {
let errors = [];
if (res instanceof HttpErrorResponse) {
if (res.error.error_description) {
errors.push(res.error.error_description);
} else {
errors = this.getOption('defaultErrors');
}
} else if (res instanceof NbAuthIllegalTokenError) {
errors.push(res.message);
} else if (res instanceof AxiosError) {
errors.push(res.response.data["error_description"]);
} else {
errors.push('Something went wrong.');
}
return observableOf(new NbAuthResult(false, res, this.getOption('redirect.failure'), errors, []));
}
} |
Issue type
Issue description
I have setup a login through Keycloak. When I login with wrong credentials, I do see Keycloak responding with "Invalid credentials". I would like to display this error, but "Something went wrong" is always shown. How can I show the error coming from Keycloak? I have already checked similar issues like #442 and #1835 but there is no "errors" property in the NbOAuth2AuthStrategy setup.
Current behavior:
When I login with invalid credentials, the error "Something went wrong" is shown.
Expected behavior:
The error in the login response should be shown
Steps to reproduce:
Login with invalid credentials using the NbOAuth2AuthStrategy
Related code:
Other information:
Angular, Nebular
The text was updated successfully, but these errors were encountered: