feat: signal server tag
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import {
|
||||
filter,
|
||||
map,
|
||||
Observable,
|
||||
take
|
||||
} from 'rxjs';
|
||||
|
||||
import { UsersActions } from '../../../../store/users/users.actions';
|
||||
import type { User } from '../../../../shared-kernel';
|
||||
|
||||
export type AuthenticationOutcome =
|
||||
| { kind: 'success'; user: User }
|
||||
| { kind: 'failure'; error: string };
|
||||
|
||||
export function waitForAuthenticationOutcome(
|
||||
actions$: Observable<{ type: string; user?: User; error?: string }>
|
||||
): Observable<AuthenticationOutcome> {
|
||||
return actions$.pipe(
|
||||
filter((action) =>
|
||||
action.type === UsersActions.setCurrentUser.type
|
||||
|| action.type === UsersActions.loadCurrentUserFailure.type
|
||||
),
|
||||
take(1),
|
||||
map((action) => {
|
||||
if (action.type === UsersActions.loadCurrentUserFailure.type) {
|
||||
return {
|
||||
kind: 'failure' as const,
|
||||
error: action.error || 'Authentication failed'
|
||||
};
|
||||
}
|
||||
|
||||
if (!action.user) {
|
||||
return {
|
||||
kind: 'failure' as const,
|
||||
error: 'Authentication failed'
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
kind: 'success' as const,
|
||||
user: action.user
|
||||
};
|
||||
})
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user