Skip to content

Commit

Permalink
feat: added dbxAuthService
Browse files Browse the repository at this point in the history
  • Loading branch information
dereekb committed Mar 25, 2022
1 parent a8f850e commit 9422182
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/dbx-core/src/lib/auth/auth.role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

/**
* An application role.
*
* Is used in the client to decide which content a user can view.
*/
export type AuthRole = string;

/**
* A set of auth roles for a user.
*/
export type AuthRoleSet = Set<AuthRole>;

/**
* Auth role for a full admin. Is allowed into all sections of the app.
*/
export const AUTH_APP_ADMIN_ROLE = 'admin';

/**
* Auth role for a general user. Is allowed into the app and is logged in.
*/
export const AUTH_APP_USER_ROLE = 'user';
13 changes: 13 additions & 0 deletions packages/dbx-core/src/lib/auth/auth.state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


/**
* An application user state.
*
* Generic states that define the current state of the user:
* - none: the user is not logged in
* - anon: the user is logged in as an anonymous account
* - error: there was an error loading the correct user state
* - new: the user has a full account but has not completed onboarding/setup
* - user: the user has a full account and has completed setup
*/
export type AuthUserState = 'none' | 'anon' | 'new' | 'user' | 'error';
3 changes: 3 additions & 0 deletions packages/dbx-core/src/lib/auth/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './service';
export * from './auth.state';
export * from './auth.role';
12 changes: 12 additions & 0 deletions packages/dbx-core/src/lib/auth/service/auth.service.rxjs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { isNot, onTrueToFalse } from '@dereekb/rxjs';
import { map, Observable, scan } from 'rxjs';

/**
* Convenience operator that emits events when the input observable goes from true to false.
*
* @param isLoggedInObs
* @returns
*/
export function signedOutEventFromIsLoggedIn(isLoggedInObs: Observable<boolean>): Observable<void> {
return isLoggedInObs.pipe(onTrueToFalse(), map(_ => undefined));
}
32 changes: 32 additions & 0 deletions packages/dbx-core/src/lib/auth/service/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Observable } from 'rxjs';
import { AuthRoleSet } from '../auth.role';
import { AuthUserState } from '../auth.state';

/**
* Client auth service used to retrieve info about the current state of client authentication and client roles they may have.
*/
export abstract class DbxAuthService {

/**
* Whether or not the client is logged in.
*
* A user is considered logged in even if there is an anonymous user. For more detailed info, consider using authUserState$.
*/
abstract readonly isLoggedIn$: Observable<boolean>;

/**
* Emits an event every time the user was signed in but signs out.
*/
abstract readonly signedOut$: Observable<void>;

/**
* Current state of the user.
*/
abstract readonly authUserState$: Observable<AuthUserState>;

/**
* Role set for the current user.
*/
abstract readonly authRoles$: Observable<AuthRoleSet>;

}
2 changes: 2 additions & 0 deletions packages/dbx-core/src/lib/auth/service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './auth.service.rxjs';
export * from './auth.service';
1 change: 1 addition & 0 deletions packages/dbx-core/src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './action';
export * from './auth';
export * from './button';
export * from './router';
export * from './pipe';
Expand Down

0 comments on commit 9422182

Please sign in to comment.