我想要儲存登入資訊判斷用戶已登入
我在A頁面做登入並把user_token存在cookie內
在B頁面laravel就抓不到cookie了,想請教原因跟解決辦法
以下是我的程式碼:
儲存cookie的function
public function loginProcess(Request $request){
$email = $request->email;
$password = $request->password;
$user = User::where("email",$email)->first();
if(is_null($user)) return $this->error("帳號錯誤");
if($password != decrypt($user->password)) return $this->error("密碼錯誤");
$token = \Str::random(255);
User::where("id",$user->id)->update(["token"=>$token]);
return $this->ok()->cookie('test',123,60);
//return $this->ok(["user_token"=>$token]);
}
function ok($data=""){
return $this->result([
"success" => true,
"message" => "",
"data" => $data,
],200);
}
function result($data,$status){
return response()->json($data,$status);
}
應該要接到cookie的function
public function __construct(){
$this->middleware(function($request,$next){
dd($request->cookie());
/*$token = $request->cookie("user_token");
$user = User::where("token",$token)->first();
if(!is_null($user)){
$request->user = $user;
return $next($request);
}else{
return $this->error("尚未登入");
}*/
});
}
設定cookie是這樣寫的嗎?
我是有找到其他的資料 請求與輸入
$this->ok()->cookie('test',123,60)
至於
$request->cookie("user_token");
可以取得Cookie,
但是不知道為什麼被你註解掉了??
不過cookie是明碼儲存,
記得不要放敏感的資料在裡面喔.