Start routing and pipeline rewrite
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -14,8 +14,4 @@ export * from './event/UserFlushedEvent'
|
||||
export * from './repository/orm/ORMUser'
|
||||
export * from './repository/orm/ORMUserRepository'
|
||||
|
||||
export * from './ui/basic/BasicRegisterFormRequest'
|
||||
export * from './ui/basic/BasicLoginFormRequest'
|
||||
export * from './ui/basic/BasicLoginController'
|
||||
|
||||
export * from './config'
|
||||
|
||||
@@ -1,143 +0,0 @@
|
||||
import {Controller} from '../../../http/Controller'
|
||||
import {Inject, Injectable} from '../../../di'
|
||||
import {SecurityContext} from '../../context/SecurityContext'
|
||||
import {Session} from '../../../http/session/Session'
|
||||
import {ResponseFactory} from '../../../http/response/ResponseFactory'
|
||||
import {redirectToGet, view} from '../../../http/response/ViewResponseFactory'
|
||||
import {Routing} from '../../../service/Routing'
|
||||
import {ResponseObject, Route} from '../../../http/routing/Route'
|
||||
import {BasicLoginFormData, BasicLoginFormRequest} from './BasicLoginFormRequest'
|
||||
import {Valid, ValidationError} from '../../../forms'
|
||||
import {BasicRegisterFormData, BasicRegisterFormRequest} from './BasicRegisterFormRequest'
|
||||
import {AuthenticatableAlreadyExistsError} from '../../AuthenticatableAlreadyExistsError'
|
||||
import {Request} from '../../../http/lifecycle/Request'
|
||||
|
||||
@Injectable()
|
||||
export class BasicLoginController extends Controller {
|
||||
public static routes({ enableRegistration = true } = {}): void {
|
||||
const controller = (request: Request) => {
|
||||
return <BasicLoginController> request.make(BasicLoginController)
|
||||
}
|
||||
|
||||
Route.group('auth', () => {
|
||||
Route.get('login', (request: Request) =>
|
||||
controller(request).getLogin())
|
||||
.pre('@auth:guest')
|
||||
.alias('@auth.login')
|
||||
|
||||
Route.post('login', (request: Request) =>
|
||||
controller(request).attemptLogin())
|
||||
.pre('@auth:guest')
|
||||
.alias('@auth.login.attempt')
|
||||
|
||||
Route.any('logout', (request: Request) =>
|
||||
controller(request).attemptLogout())
|
||||
.pre('@auth:required')
|
||||
.alias('@auth.logout')
|
||||
|
||||
if ( enableRegistration ) {
|
||||
Route.get('register', (request: Request) =>
|
||||
controller(request).getRegistration())
|
||||
.pre('@auth:guest')
|
||||
.alias('@auth.register')
|
||||
|
||||
Route.post('register', (request: Request) =>
|
||||
controller(request).attemptRegister())
|
||||
.pre('@auth:guest')
|
||||
.alias('@auth.register.attempt')
|
||||
}
|
||||
}).pre('@auth:web')
|
||||
}
|
||||
|
||||
@Inject()
|
||||
protected readonly security!: SecurityContext
|
||||
|
||||
@Inject()
|
||||
protected readonly session!: Session
|
||||
|
||||
@Inject()
|
||||
protected readonly routing!: Routing
|
||||
|
||||
public getLogin(): ResponseFactory {
|
||||
return this.getLoginView()
|
||||
}
|
||||
|
||||
public getRegistration(): ResponseFactory {
|
||||
if ( !this.security.repository.supportsRegistration() ) {
|
||||
return redirectToGet('/')
|
||||
}
|
||||
|
||||
return this.getRegistrationView()
|
||||
}
|
||||
|
||||
public async attemptLogin(): Promise<ResponseObject> {
|
||||
const form = <BasicLoginFormRequest> this.request.make(BasicLoginFormRequest)
|
||||
|
||||
try {
|
||||
const data: Valid<BasicLoginFormData> = await form.get()
|
||||
const user = await this.security.repository.getByIdentifier(data.username)
|
||||
if ( user && (await user.validateCredential(data.password)) ) {
|
||||
await this.security.authenticate(user)
|
||||
|
||||
const intention = this.session.get('@extollo:auth.intention', '/')
|
||||
this.session.forget('@extollo:auth.intention')
|
||||
return redirectToGet(intention)
|
||||
}
|
||||
|
||||
return this.getLoginView(['Invalid username/password.'])
|
||||
} catch (e: unknown) {
|
||||
if ( e instanceof ValidationError ) {
|
||||
return this.getLoginView(e.errors.all())
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
public async attemptRegister(): Promise<ResponseObject> {
|
||||
const form = <BasicRegisterFormRequest> this.request.make(BasicRegisterFormRequest)
|
||||
|
||||
try {
|
||||
const data: Valid<BasicRegisterFormData> = await form.get()
|
||||
const user = await this.security.repository.createFromCredentials(data.username, data.password)
|
||||
await this.security.authenticate(user)
|
||||
|
||||
const intention = this.session.get('@extollo:auth.intention', '/')
|
||||
this.session.forget('@extollo:auth.intention')
|
||||
return redirectToGet(intention)
|
||||
} catch (e: unknown) {
|
||||
if ( e instanceof ValidationError ) {
|
||||
return this.getRegistrationView(e.errors.all())
|
||||
} else if ( e instanceof AuthenticatableAlreadyExistsError ) {
|
||||
return this.getRegistrationView(['A user with that username already exists.'])
|
||||
}
|
||||
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
public async attemptLogout(): Promise<ResponseObject> {
|
||||
await this.security.flush()
|
||||
return this.getMessageView('You have been logged out.')
|
||||
}
|
||||
|
||||
protected getRegistrationView(errors?: string[]): ResponseFactory {
|
||||
return view('@extollo:auth:register', {
|
||||
formAction: this.routing.getNamedPath('@auth.register.attempt').toRemote,
|
||||
errors,
|
||||
})
|
||||
}
|
||||
|
||||
protected getLoginView(errors?: string[]): ResponseFactory {
|
||||
return view('@extollo:auth:login', {
|
||||
formAction: this.routing.getNamedPath('@auth.login.attempt').toRemote,
|
||||
errors,
|
||||
})
|
||||
}
|
||||
|
||||
protected getMessageView(message: string): ResponseFactory {
|
||||
return view('@extollo:auth:message', {
|
||||
message,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -1,24 +0,0 @@
|
||||
import {FormRequest, ValidationRules} from '../../../forms'
|
||||
import {Is, Str} from '../../../forms/rules/rules'
|
||||
import {Singleton} from '../../../di'
|
||||
|
||||
export interface BasicLoginFormData {
|
||||
username: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
@Singleton()
|
||||
export class BasicLoginFormRequest extends FormRequest<BasicLoginFormData> {
|
||||
protected getRules(): ValidationRules {
|
||||
return {
|
||||
username: [
|
||||
Is.required,
|
||||
Str.lengthMin(1),
|
||||
],
|
||||
password: [
|
||||
Is.required,
|
||||
Str.lengthMin(1),
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
import {FormRequest, ValidationRules} from '../../../forms'
|
||||
import {Is, Str} from '../../../forms/rules/rules'
|
||||
import {Singleton} from '../../../di'
|
||||
|
||||
export interface BasicRegisterFormData {
|
||||
username: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
@Singleton()
|
||||
export class BasicRegisterFormRequest extends FormRequest<BasicRegisterFormData> {
|
||||
protected getRules(): ValidationRules {
|
||||
return {
|
||||
username: [
|
||||
Is.required,
|
||||
Str.lengthMin(1),
|
||||
],
|
||||
password: [
|
||||
Is.required,
|
||||
Str.lengthMin(1),
|
||||
Str.confirmed,
|
||||
],
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user