2 回答

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),如果您想將它們與自定義身份驗證系統一起使用,您可能需要掛鉤他們的代碼

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 關注
- 109 瀏覽
添加回答
舉報