Laravel将数据从控制器发送到中间件

时间:2020-11-10 11:15:37

标签: php laravel

我正在尝试将token_id和user_id从控制器传递到中间件。 我有些困难。 这是我要运行的代码。

class UserController extends Controller
{
    protected $user_id;
    protected $token_id;

    public function __construct()
{
    $user_id = $this->user_id;
    $token_id = $this->token_id;
    $together = $user_id . ':' . $token_id;

    $this->middleware('check.auth:' . $together);
}

public function setLogin(Request $request)
    {
        $credentials = request(['email', 'password']);
        
        if (!Auth::attempt($credentials)){
                Secure::checkFailedAuth($request->email, $request->password);
                return response()->json(['response' => false, 'status' => 400, 'message' => 'Provided details are not correct!'], 400);
        }   

        $user = Auth::user();
        $tokenResult = $user->createToken($user->name);
        $token = $tokenResult->token;
        $token->save();

        $this->user_id = Auth::id();
        $this->token_id = $tokenResult->token->token_id;

        return response()->json(['result' => true, 'status' => 200, 'message' => 'Login Successful',  'data' => Auth::user(), 'token' => $tokenResult->accessToken ], 200);
    }

我运行时没有错误,但是user_id和token_id都为null。 有什么想法吗?

0 个答案:

没有答案