Add middleware and logic for bootstrapping the session auth
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2021-06-05 13:24:12 -05:00
parent 91abcdf8ef
commit f00233d49a
14 changed files with 201 additions and 9 deletions

View File

@@ -4,6 +4,7 @@ import {Awaitable, Maybe} from '../util'
import {Authenticatable, AuthenticatableRepository} from './types'
import {UserAuthenticatedEvent} from './event/UserAuthenticatedEvent'
import {UserFlushedEvent} from './event/UserFlushedEvent'
import {UserAuthenticationResumedEvent} from './event/UserAuthenticationResumedEvent'
/**
* Base-class for a context that authenticates users and manages security.
@@ -101,6 +102,19 @@ export abstract class SecurityContext {
}
}
/**
* Assuming a user is still authenticated in the context,
* try to look up and fill in the user.
*/
async resume(): Promise<void> {
const credentials = await this.getCredentials()
const user = await this.repository.getByCredentials(credentials)
if ( user ) {
this.authenticatedUser = user
await this.bus.dispatch(new UserAuthenticationResumedEvent(user, this))
}
}
/**
* Write the current state of the security context to whatever storage
* medium the context's host provides.
@@ -119,4 +133,11 @@ export abstract class SecurityContext {
getUser(): Maybe<Authenticatable> {
return this.authenticatedUser
}
/**
* Returns true if there is a currently authenticated user.
*/
hasUser(): boolean {
return Boolean(this.authenticatedUser)
}
}