Skip to content

Instantly share code, notes, and snippets.

@whisher
Created October 1, 2019 19:49
Show Gist options
  • Save whisher/9df2503916db215687a507646712c976 to your computer and use it in GitHub Desktop.
Save whisher/9df2503916db215687a507646712c976 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import { select, Store } from '@ngrx/store';
import { Observable } from 'rxjs';
import { ErrorDto } from '@models/error';
import {
Auth,
CreateLoginDto,
LoginDto,
SendNewActivationTokenDto,
TokenDto
} from '@models/auth';
import { State } from '../reducers';
import {
selectAccount,
selectError,
selectIsAuthenticated,
selectLoading,
selectRegistered,
selectToken
} from '../selectors';
import * as AuthActions from '../actions';
@Injectable()
export class AuthFacade {
get account$(): Observable<Auth | null> {
return this.store.pipe(select(selectAccount));
}
get error$(): Observable<ErrorDto | null> {
return this.store.pipe(select(selectError));
}
get isAuthenticated$() {
return this.store.pipe(select(selectIsAuthenticated));
}
get loading$(): Observable<boolean> {
return this.store.pipe(select(selectLoading));
}
get registered$(): Observable<boolean> {
return this.store.pipe(select(selectRegistered));
}
get token$(): Observable<TokenDto | null> {
return this.store.pipe(select(selectToken));
}
constructor(private store: Store<State>) {}
activateUser(token: string): void {
this.store.dispatch(AuthActions.activateUser({ token }));
}
logged(): void {
this.store.dispatch(AuthActions.logged());
}
login(credentials: LoginDto): void {
this.store.dispatch(AuthActions.login({ credentials }));
}
logout(): void {
this.store.dispatch(AuthActions.logout());
}
sendNewActivationToken(payload: SendNewActivationTokenDto): void {
this.store.dispatch(AuthActions.newActivationToken({ payload }));
}
notActivationSent(): void {
this.store.dispatch(AuthActions.notActivationSent());
}
reset(): void {
this.store.dispatch(AuthActions.loginReset());
}
signup(account: CreateLoginDto): void {
this.store.dispatch(AuthActions.signup({ account }));
}
signedUp(): void {
this.store.dispatch(AuthActions.signedUp());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment