diff --git a/packages/dbx-core/src/lib/auth/auth.role.ts b/packages/dbx-core/src/lib/auth/auth.role.ts new file mode 100644 index 000000000..ff1260fb9 --- /dev/null +++ b/packages/dbx-core/src/lib/auth/auth.role.ts @@ -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; + +/** + * 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'; diff --git a/packages/dbx-core/src/lib/auth/auth.state.ts b/packages/dbx-core/src/lib/auth/auth.state.ts new file mode 100644 index 000000000..3a0bcfd32 --- /dev/null +++ b/packages/dbx-core/src/lib/auth/auth.state.ts @@ -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'; diff --git a/packages/dbx-core/src/lib/auth/index.ts b/packages/dbx-core/src/lib/auth/index.ts new file mode 100644 index 000000000..f999ca6af --- /dev/null +++ b/packages/dbx-core/src/lib/auth/index.ts @@ -0,0 +1,3 @@ +export * from './service'; +export * from './auth.state'; +export * from './auth.role'; diff --git a/packages/dbx-core/src/lib/auth/service/auth.service.rxjs.ts b/packages/dbx-core/src/lib/auth/service/auth.service.rxjs.ts new file mode 100644 index 000000000..6383774c6 --- /dev/null +++ b/packages/dbx-core/src/lib/auth/service/auth.service.rxjs.ts @@ -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): Observable { + return isLoggedInObs.pipe(onTrueToFalse(), map(_ => undefined)); +} diff --git a/packages/dbx-core/src/lib/auth/service/auth.service.ts b/packages/dbx-core/src/lib/auth/service/auth.service.ts new file mode 100644 index 000000000..2e738c543 --- /dev/null +++ b/packages/dbx-core/src/lib/auth/service/auth.service.ts @@ -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; + + /** + * Emits an event every time the user was signed in but signs out. + */ + abstract readonly signedOut$: Observable; + + /** + * Current state of the user. + */ + abstract readonly authUserState$: Observable; + + /** + * Role set for the current user. + */ + abstract readonly authRoles$: Observable; + +} diff --git a/packages/dbx-core/src/lib/auth/service/index.ts b/packages/dbx-core/src/lib/auth/service/index.ts new file mode 100644 index 000000000..f1bba9c4d --- /dev/null +++ b/packages/dbx-core/src/lib/auth/service/index.ts @@ -0,0 +1,2 @@ +export * from './auth.service.rxjs'; +export * from './auth.service'; diff --git a/packages/dbx-core/src/lib/index.ts b/packages/dbx-core/src/lib/index.ts index f807bafd2..04e197ee0 100644 --- a/packages/dbx-core/src/lib/index.ts +++ b/packages/dbx-core/src/lib/index.ts @@ -1,4 +1,5 @@ export * from './action'; +export * from './auth'; export * from './button'; export * from './router'; export * from './pipe';