2021-06-05 18:24:12 +00:00
|
|
|
import {Middleware} from '../../http/routing/Middleware'
|
|
|
|
import {Inject, Injectable} from '../../di'
|
2021-11-26 20:32:25 +00:00
|
|
|
import {SecurityContext} from '../context/SecurityContext'
|
2021-06-05 18:24:12 +00:00
|
|
|
import {ResponseObject} from '../../http/routing/Route'
|
|
|
|
import {error} from '../../http/response/ErrorResponseFactory'
|
|
|
|
import {NotAuthorizedError} from '../NotAuthorizedError'
|
|
|
|
import {HTTPStatus} from '../../util'
|
2021-07-17 17:49:07 +00:00
|
|
|
import {Routing} from '../../service/Routing'
|
|
|
|
import {redirect} from '../../http/response/RedirectResponseFactory'
|
2021-06-05 18:24:12 +00:00
|
|
|
|
2021-11-26 20:32:25 +00:00
|
|
|
// TODO handle JSON and non-web
|
|
|
|
|
2021-06-05 18:24:12 +00:00
|
|
|
@Injectable()
|
|
|
|
export class GuestRequiredMiddleware extends Middleware {
|
|
|
|
@Inject()
|
|
|
|
protected readonly security!: SecurityContext
|
|
|
|
|
2021-07-17 17:49:07 +00:00
|
|
|
@Inject()
|
|
|
|
protected readonly routing!: Routing
|
|
|
|
|
2021-06-05 18:24:12 +00:00
|
|
|
async apply(): Promise<ResponseObject> {
|
|
|
|
if ( this.security.hasUser() ) {
|
2021-07-17 17:49:07 +00:00
|
|
|
if ( this.routing.hasNamedRoute('@auth.redirectFromGuest') ) {
|
|
|
|
return redirect(this.routing.getNamedPath('@auth.redirectFromGuest').toRemote)
|
|
|
|
} else {
|
|
|
|
return error(new NotAuthorizedError(), HTTPStatus.FORBIDDEN)
|
|
|
|
}
|
2021-06-05 18:24:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|