如何在数据库

时间:2017-07-20 05:16:44

标签: php api jwt lumen lumen-5.3

我知道如果我在我的数据库中存储jwt令牌,我将失去目的但由于某种原因,我想存储它,我该怎么做?

控制器

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Tymon\JWTAuth\JWTAuth;

class AuthController extends Controller
{
/**
 * @var \Tymon\JWTAuth\JWTAuth
 */
protected $jwt;

public function __construct(JWTAuth $jwt)
{
    $this->jwt = $jwt;
}

public function postLogin(Request $request)
{
    $this->validate($request, [
        'email'    => 'required|email|max:255',
        'password' => 'required',
    ]);

    try {

        if (! $token = $this->jwt->attempt($request->only('email', 'password'))) {
        $create_token = User::where('user_id', $login->user_id)->update(['token' => $token]);
            return response()->json(['user_not_found'], 404);
        }
        else {
        $create_token = User::where('user_id', auth()->user()->user_id)->update(['token' => $token]);
        }

    } catch (\Tymon\JWTAuth\Exceptions\TokenExpiredException $e) {

        return response()->json(['token_expired'], 500);

    } catch (\Tymon\JWTAuth\Exceptions\TokenInvalidException $e) {

        return response()->json(['token_invalid'], 500);

    } catch (\Tymon\JWTAuth\Exceptions\JWTException $e) {

        return response()->json(['token_absent' => $e->getMessage()], 500);

    }

    return response()->json(compact('token'));
}

    public function logout(Request $request) 
{
    $this->jwt->invalidate($this->jwt->getToken());
    return response()->json([
        'message' => 'User logged off successfully!'
    ], 200);
}
}

我尝试了上面的方法但是我收到错误说调用未定义的函数App \ Http \ Controllers \ auth() 任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

不建议看到json网络令牌是时间敏感的,但我确实写了类似的东西,但是使用redis。查看代码,我建议您为Select * FROM table WHERE flag = 'n' AND id > 510245 创建行并将其添加到tokens。创建用户后,您可以创建令牌并将其保存到数据库中。