menu

Laravel - Authentification (logout)


Pada tutorial sebelumnya kita sudah berhasil membuat sistem login, selanjutnya tutorial kali ini kita akan membuat sistem logout pada laravel.

Algoritamnya mudah saja yaitu dengan menghapus session yang tersimpan pada project kita.

Oke, langsung saja kita mulai praktek tutorialnya.

Pertama, buat terlebih dahulu routenya dengan mengedit file web.php pada path protected/app/routes/ menjadi seperti ini,
.
.
.
//login
Route::get('login', 'Controller\Auth\AuthController@indexLogin');
Route::post('login', 'Controller\Auth\AuthController@login');
Route::get('logout', 'Controller\Auth\AuthController@logout');
.
.
.

Selanjutnya, buat function untuk logout dengan mengedit file AuthController.php
pada path protected/app/controllers/auth/ menjadi seperti ini,
<?php namespace Controller\Auth;

use Model\User;
use \Input;
use \View;
use \Validator;
use \Redirect;
use \Auth;
use \Session;

class AuthController extends \BaseController
{
 public function indexLogin()
 {
  return View::make('auth.login');
 }

 public function login() 
 {
  $user = NULL;
  // Check whether input is an email or username
  $field = Validator::make(
     array('email' => Input::get('username', '')),
     array('email' => 'email'))
     ->passes() ? 'email' : 'username';
  $credentials = array(
   $field   => Input::get('email'), 
   'password'  => Input::get('password', '')
  );

  $remember = Input::get('remember', '1') == '1' ? true : false;

  $status = INVALID_CREDENTIAL;

  if(Auth::attempt($credentials, $remember)) {
   $user = Auth::user();
   $status = OK;
  }

  // For Logging
  Session::put(SESSION_LOG_STATUS, $status);
  Session::put(SESSION_LOG_TYPE, LOGGING_UPDATE);
  Session::put(SESSION_LOG_MODEL, $user);
  // dd($user);
  return Redirect::to('');
 }

 public function logout() 
 {
  $status = INVALID_CREDENTIAL;
  $user = Auth::user();

  if(!$user) {
   return Redirect::to('login');
  } else {
   $status = OK;
   $user = Auth::logout();
  }

  // For Logging
  Session::put(SESSION_LOG_TYPE, LOGGING_UPDATE);
  Session::put(SESSION_LOG_STATUS, $status);
  Session::put(SESSION_LOG_MODEL, $user);

  return Redirect::to('');
 }
}

Kemudian, edit file header.blade.php pada path protected/app/views/layouts/web/ menjadi seperti ini,
<div class="header">
 <div class="banner">
  <div class="welcome">Welcome</div>
  {{-- code dibawah ini menggunakan constant variabel --}}
  <div class="gambar"><a href="#"><img src="{{ IMAGE_HEADER }}" style="width:"100px" height="129px";"></a></div>
 </div>
 <hr>
 <div class="menu">
  <div class="kiri">
   <li>
    <div class="dropdown">
      <button class="btn btn-default">Menu(css)</button>
      <div class="dropdown-content">
        <a href="{{ URL::to('crud/users')}}">User</a>
        <a href="{{ URL::to('book')}}">Index Book</a>
        <a href="{{ URL::to('javaScript/js1')}}">Belajar Script</a>
        <a href="{{ URL::to('javaScript/js2')}}">Belajar Script2</a>
        <a href="{{ URL::to('javaScript/js3')}}">Belajar Script3</a>
        <a href="{{ URL::to('javaScript/js4')}}">Belajar Script4</a>
        <a href="{{ URL::to('javaScript/js5')}}">Belajar Script5</a>
        <a href="{{ URL::to('javaScript/js6')}}">Belajar Script6</a>
        <a href="{{ URL::to('javaScript/js7')}}">Belajar Script7</a>
      </div>
    </div>
   </li>
   <li>
     <div class="dropdown-script">
      <button onclick="myFunction()" class="dropbtn btn btn-default">Translate</button>
      <div id="myDropdown" class="dropdown-content-script">
        <a href="{{ URL::to('lang/en') }}">English</a>
        <a href="{{ URL::to('lang/ina') }}">Indonesia</a>
      </div>
     </div>
   </li>
  </div>
  <div class="kanan">
   @if(Auth::user())
    <li onclick="if(confirm('Are you sure to logout?')) location.href='{{ URL::to('logout') }}';">Logout</li>
   @else
    <li onclick="location.href='{{ URL::to('login') }}';">Login</li>
   @endif
   <li onclick="location.href='{{ URL::to('/') }}';">Cari</li>
  </div>
 </div>
</div>

Terakhir, edit file hello.blade.php pada path protected/app/views/web/ menjadi seperti ini,
@extends('layouts/web/master')

<?php $title = "Welcome" ?>

@section('script')
@stop

@section('content')
<div class="isi">
 <h4>{{ trans('translate.welcome_to_codedoct'); }} @if(Auth::user()){{Auth::user()->name}}@else Guest @endif</h4>
 <br>
 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt facilis, dolore similique maiores? Doloribus, asperiores. Iusto debitis tempora velit repellat sed autem, quaerat commodi, necessitatibus soluta quos libero voluptatibus eum.</p>
 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt facilis, dolore similique maiores? Doloribus, asperiores. Iusto debitis tempora velit repellat sed autem, quaerat commodi, necessitatibus soluta quos libero voluptatibus eum.</p>
 <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Sunt facilis, dolore similique maiores? Doloribus, asperiores. Iusto debitis tempora velit repellat sed autem, quaerat commodi, necessitatibus soluta quos libero voluptatibus eum.</p>
</div>
@stop

Sehingga tampilannya saat akan logout akan tampak seperti ini,


Dan saat sudah logout akan tampak seperti ini,


===DONE!===