import {
    Controller,
    view,
    Inject,
    Injectable,
    SecurityContext,
    redirect,
    Valid,
    BasicLoginAttempt,
    BasicLoginProvider,
} from '@extollo/lib'

/**
 * Login Controller
 * ------------------------------------
 * Put some description here.
 */
@Injectable()
export class Login extends Controller {
    @Inject()
    protected readonly security!: SecurityContext

    public login() {
        return view('auth:login')
    }

    public async attemptLogin(attempt: Valid<BasicLoginAttempt>) {
        return this.make<BasicLoginProvider>(BasicLoginProvider)
            .attemptLogin(attempt)
    }

    public async logout() {
        await this.security.flush()
        return redirect('/')
    }
}
