2 回答
![?](http://img1.sycdn.imooc.com/545865620001c45402760276-100-100.jpg)
TA贡献1853条经验 获得超18个赞
如果要手动验证用户身份,可以轻松使用会话。有以下代码作为参考:
//this is where I would like to auth the user.
//using Auth::Attempt and Auth::Login will only run the default query
// typically you store it using the user ID, but you can modify the session using other values.
session()->put('user_id', user id from database here);
如果要检查用户是否经过身份验证,请将RedirectIfAuthenticated中间件修改为:
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (session()->has('user_id')) {
return redirect( custom path here );
}
return $next($request);
}
}
当您想注销用户时,只需销毁会话密钥
session()->forget('user_id');
**注意:** 许多广播和插件使用 Laravel 的身份验证系统(Guards),如果您想将它们与自定义身份验证系统一起使用,您可能需要挂钩他们的代码
![?](http://img1.sycdn.imooc.com/545847f50001126402200220-100-100.jpg)
TA贡献1812条经验 获得超5个赞
Laravel 提供了自定义会话驱动程序,您可以使用它们来创建或删除会话
<?php
namespace App\Extensions;
class MongoSessionHandler implements \SessionHandlerInterface
{
public function open($savePath, $sessionName) {}
public function close() {}
public function read($sessionId) {}
public function write($sessionId, $data) {}
public function destroy($sessionId) {}
public function gc($lifetime) {}
}
希望对您有所帮助,如果没有,请在下方评论。会帮你的。
###### 更新 #######
我认为你必须从 Laravel 进行自定义 HTTP 会话
第 1 步:在您的数据库中为会话创建另一个表,如下所示;
Schema::create('sessions', function ($table) {
$table->string('id')->unique();
$table->unsignedInteger('user_id')->nullable();
$table->string('ip_address', 45)->nullable();
$table->text('user_agent')->nullable();
$table->text('payload');
$table->integer('last_activity');
});
Step 2 : 在会话中存储数据,你通常会使用put方法或session助手;
// Via a request instance...
$request->session()->put('key', 'value');
// Via the global helper...
session(['key' => 'value']);
第 3 步:当您的函数返回 1 时获取特定用户的密钥
$value = $request->session()->get('key', function () {
return 'default';
});
第 4 步:删除会话,一段时间后,出于安全原因,您需要删除会话,然后您可以这样做。
$value = $request->session()->pull('key', 'default');
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报