Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth login logout #1

Open
wants to merge 1 commit into
base: admin
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
public function loginView (){
$title = 'Login';
return view('auth.login', compact('title'));
}

public function login (Request $request){
$request->validate([
'email' => 'required',
'password' => 'required'
]);

$email = $request->input('email');
$password = $request->input('password');
$user = User::where('email', $email)->first();

if($user && $user->password === $password){
Auth::login($user);
return redirect()->intended(route('dashboard.index'));
}else{
return redirect(route('auth.login'))->withErrors('invalid email or password');
}
}

public function logout(Request $request)
{
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect(route('auth.login'));
}

}
11 changes: 10 additions & 1 deletion resources/views/auth/login.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@
</div>
</div>
</div>
<form method="POST">
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger" role="alert">
@foreach ($errors->all() as $error)
{{ $error }}
@endforeach
</div>
@endif
</div>
<form action="{{route('auth.postLogin')}}" method="POST">
@csrf
<div class="form-group">
<div class="form-label-group">
Expand Down
5 changes: 4 additions & 1 deletion resources/views/partials/header.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,13 @@ class="icon ni ni-moon"></em><span>Mode Gelap</span></a></li>
<div class="dropdown-inner">
<ul class="link-list">
<li>
<a href="">
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
<em class="icon ni ni-signout"></em>
<span>Logout</span>
</a>
<form id="logout-form" action="{{ route('logout')}}" method="POST">
@csrf
</form>
</li>
</ul>
</div>
Expand Down
10 changes: 7 additions & 3 deletions routes/web.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php


use Illuminate\Support\Facades\Route;
use App\Http\Controllers\AuthController;
use App\Http\Controllers\DashboardController;

/*
Expand All @@ -15,8 +17,10 @@
*/

Route::get('/', function () {
return redirect()->route('dashboard.index');
return redirect()->route('auth.login');
});

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index');

Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard.index')->middleware('auth');;
Route::get('/login', [AuthController::class, 'loginView'])->name('auth.login');
Route::post('/login', [AuthController::class, 'login'])->name('auth.postLogin');
Route::post('/logout', [AuthController::class, 'logout'])->name('logout');