Use status request to check an existing token and to retrieve auth methods available

This commit is contained in:
Giuseppe Digilio
2020-01-16 14:36:20 +01:00
parent 6f8f4b31bf
commit 6560d1d112
4 changed files with 62 additions and 11 deletions

View File

@@ -16,7 +16,13 @@ import { AuthStatus } from './models/auth-status.model';
import { AuthTokenInfo, TOKENITEM } from './models/auth-token-info.model';
import { isEmpty, isNotEmpty, isNotNull, isNotUndefined } from '../../shared/empty.util';
import { CookieService } from '../services/cookie.service';
import { getAuthenticationToken, getRedirectUrl, isAuthenticated, isTokenRefreshing } from './selectors';
import {
getAuthenticationMethods,
getAuthenticationToken,
getRedirectUrl,
isAuthenticated,
isTokenRefreshing
} from './selectors';
import { AppState, routerStateSelector } from '../../app.reducer';
import {
CheckAuthenticationTokenAction,
@@ -28,6 +34,7 @@ import { Base64EncodeUrl } from '../../shared/utils/encode-decode.util';
import { RemoteDataBuildService } from '../cache/builders/remote-data-build.service';
import { RouteService } from '../services/route.service';
import { AuthMethod } from './models/auth.method';
import { NormalizedAuthStatus } from './models/normalized-auth-status.model';
export const LOGIN_ROUTE = '/login';
export const LOGOUT_ROUTE = '/logout';
@@ -121,7 +128,14 @@ export class AuthService {
* Checks if token is present into the request cookie
*/
public checkAuthenticationCookie(): Observable<AuthStatus> {
return this.authRequestService.postToEndpoint('login');
// Determine if the user has an existing auth session on the server
const options: HttpOptions = Object.create({});
let headers = new HttpHeaders();
headers = headers.append('Accept', 'application/json');
options.headers = headers;
return this.authRequestService.getRequest('status', options).pipe(
map((status: NormalizedAuthStatus) => Object.assign(new AuthStatus(), status))
);
}
/**
@@ -158,7 +172,13 @@ export class AuthService {
* Checks if token is present into browser storage and is valid. (NB Check is done only on SSR)
*/
public checkAuthenticationToken() {
this.store.dispatch(new CheckAuthenticationTokenAction());
this.store.pipe(
select(getAuthenticationMethods),
filter((authMethods: AuthMethod[]) => isEmpty(authMethods)),
take(1)
).subscribe(() => {
this.store.dispatch(new CheckAuthenticationTokenAction());
});
}
/**