Skip to content
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

feat(auth): Display username on masthead #322

Merged
merged 4 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/app/Login/BasicAuthForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export const BasicAuthForm: React.FunctionComponent<FormProps> = (props) => {

const handleSubmit = React.useCallback((evt) => {
props.onSubmit(evt, `${username}:${password}`, 'Basic', rememberMe);
context.login.setUsername(username);
}, [props, props.onSubmit, username, password, context.login, rememberMe]);

// FIXME Patternfly Form component onSubmit is not triggered by Enter keydown when the Form contains
Expand Down
64 changes: 45 additions & 19 deletions src/app/Shared/Services/Login.service.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*/
import { Observable, ObservableInput, of, ReplaySubject } from 'rxjs';
import { fromFetch } from 'rxjs/fetch';
import { catchError, first, map, tap } from 'rxjs/operators';
import { catchError, concatMap, first, map, tap } from 'rxjs/operators';

export class LoginService {

Expand Down Expand Up @@ -67,35 +67,37 @@ export class LoginService {

token = this.useCacheItemIfAvailable(this.TOKEN_KEY, token);

return fromFetch(`${this.authority}/api/v1/auth`, {
return fromFetch(`${this.authority}/api/v2.1/auth`, {
credentials: 'include',
mode: 'cors',
method: 'POST',
body: null,
headers: this.getAuthHeaders(token, method),
})
.pipe(
map(response => {
concatMap(response => {
if (!this.authMethod.isStopped) {
this.authMethod.next(response.ok ? method : (response.headers.get('X-WWW-Authenticate') || ''));
}
return response.ok;
return response.json();
}),
first(),
tap((jsonResp: AuthV2Response) => {
if(jsonResp.meta.status === 'OK') {
this.completeAuthMethod(method);
this.setUsername(jsonResp.data.result.username);
this.token.next(token);
this.authenticated.next(true);
}
}),
map((jsonResp: AuthV2Response) => {
return jsonResp.meta.status === 'OK';
}),
catchError((e: Error): ObservableInput<boolean> => {
window.console.error(JSON.stringify(e));
this.authMethod.complete();
return of(false);
}),
first(),
tap(v => {
if (v) {
this.authMethod.next(method);
this.authMethod.complete();
this.setCacheItem(this.METHOD_KEY, method);
this.token.next(token);
this.authenticated.next(true);
}
})
);
}

Expand Down Expand Up @@ -137,11 +139,6 @@ export class LoginService {
this.authenticated.next(false);
}

setUsername(username: string): void {
this.setCacheItem(this.USER_KEY, username);
this.username.next(username);
}

rememberToken(token: string): void {
this.setCacheItem(this.TOKEN_KEY, token);
}
Expand All @@ -150,6 +147,17 @@ export class LoginService {
this.removeCacheItem(this.TOKEN_KEY);
}

private setUsername(username: string): void {
this.setCacheItem(this.USER_KEY, username);
this.username.next(username);
}

private completeAuthMethod(method: string): void {
this.authMethod.next(method);
this.setCacheItem(this.METHOD_KEY, method);
this.authMethod.complete();
}

private getCacheItem(key: string): string {
const item = sessionStorage.getItem(key);
return (!!item) ? item : '';
Expand All @@ -174,3 +182,21 @@ export class LoginService {
}

}

interface ApiResponse {
meta: Meta;
data: Object;
}

interface Meta {
status: string;
type: string;
}

interface AuthV2Response extends ApiResponse {
data: {
result: {
username: string;
}
};
}
jan-law marked this conversation as resolved.
Show resolved Hide resolved