<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Auth;

class LoginController extends Controller
{
    // Tampilkan form login
    public function showLoginForm()
    {
        return view('login');
    }

    // Proses login
    public function login(Request $request)
    {
        // Validasi kredensial
        $credentials = $request->only('email', 'password');

        // Coba untuk melakukan login
        if (Auth::attempt($credentials)) {
            // Jika berhasil, redirect ke home atau route yang diinginkan
            return redirect()->intended('/dashboard');
        }

        // Jika gagal, kembali ke halaman login dengan pesan error
        return back()->withErrors(['email' => 'Email atau password salah']);
    }

    // Proses logout
    public function logout(Request $request)
    {
        Auth::logout();
        return redirect('/');
    }
}

