Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sessions Model
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use App\Sessions;
use App\LastLogins;
class SessionController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Returns the view with some data.
*
* @return \Illuminate\Http\Response
*/
public function renderPage() {
$active_sessions = Sessions::where('user_id', '=', Auth::user()->id)->orderBy('last_activity', 'desc')->get();
return view('pages.sessions')->with([
'active_sessions' => $active_sessions
]);
}
/**
* Logs out a specific session of an user.
*
* @return $status_logout string Information text, if the logout was successful or not
*/
public function destroySpecificSession(Request $request)
{
$session = Sessions::find($request->session_id);
// Here, $session is always "null" as $request->session_id already returns an integer ID instead of an string unique ID
if(!$session->delete()) {
return redirect()->route('sessions')->with('status_logout', "The session could not been destroyed.");
}
return redirect()->route('sessions')->with('status_logout', "The session has been destroyed successfully.");
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sessions extends Model
{
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';
/**
* The "type" of the auto-incrementing ID.
*
* @var string
*/
protected $keyType = 'string';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id', 'user_id', 'ip_address', 'user_agent', 'payload', 'last_activity'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'payload'
];
}
<div class="row">
<div class="col-md-12">
@if($active_sessions->isEmpty())
<div class="alert alert-info col-md-12">
<p>You don't have any active sessions yet.</p>
</div>
@else
<div class="table-responsive">
<table class="table table-condensed table-hover">
<thead>
<tr>
<th>ID</th>
<th>User-Agent</th>
<th>IP Address</th>
<th>Last Activity</th>
<th>Destroy Session</th>
</tr>
</thead>
<tbody>
@foreach($active_sessions as $active_session)
<tr>
<td>{{ $active_session->id }}</td>
<td>{{ $active_session->user_agent }}</td>
<td>{{ $active_session->ip_address }}</td>
<td>{{ Carbon\Carbon::parse(Carbon\Carbon::createFromTimestamp($active_session->last_activity)->toDateTimeString())->format('d.m.Y, H:i') }}</td>
<td>
@if($active_session->id === Session::getId())
<a class="btn btn-block btn-warning" href="#">Current Session</a>
@else
<a class="btn btn-block btn-warning" href="{{ route('sessions.logout_specific', ['session_id' => $active_session->id]) }}">Log out</a>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@endif
</div>
</div>
<?php
Route::get('/sessions', 'SessionController@renderPage')->name('sessions');
Route::get('/sessions/logout/{session_id}', 'SessionController@destroySpecificSession')->name('sessions.logout_specific');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment