<?php

namespace App\Http\Controllers;

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

class AuthController extends Controller
{

  public function loginPage()
  {
    if (Auth::check()) {
      return redirect()->route('home');
    }
    return view('login');
  }

  public function auth(Request $request)
  {
    Auth::attempt($request->only('u_email', 'password'));

    if (Auth::check()) {
      $request->session()->put('role', Auth::user()->u_role);

      $request->session()->put('email', Auth::user()->u_email);

      $request->session()->put('nama', Auth::user()->u_nama);

      return redirect()->route('home');
    } else {
      return redirect()
        ->back()
        ->withErrors(["Email atau password salah!"]);
    }
  }


  public function logout()
  {
    Auth::logout();
    session()->flush();
    return redirect()->route('login');
  }
}
