<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class SesiLoginController extends Controller
{
    public function login(){
        return view('admin.login',['title'=> 'PINTU2 Admin Login Page']);
    }
    
    public function ceklogin(Request $request){
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ],[
            'email.required' => 'Email Tidak Boleh kosong',
            'password.required' => 'Email Tidak Boleh kosong',
            'email.email' => 'Email harus berupa Email, Contoh abc@mail.com'
        ]
        );
        $infologin = [
            'email' => $request->email,
            'password' => $request->password,
        ];
        if(Auth::attempt($infologin)){
            return redirect()->route('admin.dashboard');
        }
        else{
            return redirect()->route('login')->withErrors('Email atau password Salah, Cek kembali')->withInput();
        }
        
    }
    
    public function logout(){
        Auth::logout();
        return redirect()->route('login');
    }
}
