v2board/app/Http/Middleware/User.php

31 lines
748 B
PHP
Raw Normal View History

2019-10-29 15:33:36 +08:00
<?php
namespace App\Http\Middleware;
use Closure;
class User
{
/**
* Handle an incoming request.
*
2020-01-11 13:36:52 +08:00
* @param \Illuminate\Http\Request $request
* @param \Closure $next
2019-10-29 15:33:36 +08:00
* @return mixed
*/
public function handle($request, Closure $next)
{
2020-02-11 21:04:47 +08:00
if ($request->input('access_token')) {
2020-02-11 21:06:15 +08:00
$user = \App\Models\User::where('token', $request->input('access_token'))->first();
2020-02-11 21:04:47 +08:00
if ($user) {
$request->session()->put('email', $user->email);
$request->session()->put('id', $user->id);
}
}
2019-10-29 15:33:36 +08:00
if (!$request->session()->get('id')) {
abort(403, '未登录或登陆已过期');
}
return $next($request);
}
}