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

App state interface #240

Merged
merged 12 commits into from
Dec 1, 2021
16 changes: 16 additions & 0 deletions projects/auth0-angular/src/lib/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,22 @@ export interface AuthConfig {
[key: string]: any;
}

/**
* Angular specific state to be stored before redirect
*/
export interface AppState {
/**
* Target path the app gets routed to after
* handling the callback from Auth0 (defaults to '/')
*/
target?: string;

/**
* Any custom parameter to be stored in appState
*/
[key: string]: any;
}

/**
* Gets and sets configuration for the internal Auth0 client. This can be
* used to provide configuration outside of using AuthModule.forRoot, i.e. from
Expand Down
15 changes: 9 additions & 6 deletions projects/auth0-angular/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ import {

import { Auth0ClientService } from './auth.client';
import { AbstractNavigator } from './abstract-navigator';
import { AuthClientConfig } from './auth.config';
import {
AuthClientConfig,
AppState,
} from './auth.config';
import { AuthState } from './auth.state';

@Injectable({
providedIn: 'root',
})
export class AuthService implements OnDestroy {
private appStateSubject$ = new ReplaySubject<any>(1);
export class AuthService<TAppState extends AppState = AppState> implements OnDestroy {
private appStateSubject$ = new ReplaySubject<TAppState>(1);

// https://stackoverflow.com/a/41177163
private ngUnsubscribe$ = new Subject<void>();
Expand Down Expand Up @@ -132,7 +135,7 @@ export class AuthService implements OnDestroy {
*
* @param options The login options
*/
loginWithRedirect(options?: RedirectLoginOptions): Observable<void> {
loginWithRedirect(options?: RedirectLoginOptions<TAppState>): Observable<void> {
return from(this.auth0Client.loginWithRedirect(options));
}

Expand Down Expand Up @@ -294,8 +297,8 @@ export class AuthService implements OnDestroy {
*
* @param url The URL to that should be used to retrieve the `state` and `code` values. Defaults to `window.location.href` if not given.
*/
handleRedirectCallback(url?: string): Observable<RedirectLoginResult> {
return defer(() => this.auth0Client.handleRedirectCallback(url)).pipe(
handleRedirectCallback(url?: string): Observable<RedirectLoginResult<TAppState>> {
return defer(() => this.auth0Client.handleRedirectCallback<TAppState>(url)).pipe(
withLatestFrom(this.authState.isLoading$),
tap(([result, isLoading]) => {
if (!isLoading) {
Expand Down