如何在邮递员中解决这个问题?

时间:2019-05-15 03:26:26

标签: postman jwt-auth

我附上了邮递员的输出。如何在邮递员中获得适当的输出? 在邮递员中,它显示错误:true,用户不存在。代码或jwt文件中是否存在任何错误?当我测试api时,它没有显示预期的输出以测试身份验证。 enter image description here

  class AuthController
    {
       use \App\CommonFunctions;
protected $container;

public function __construct(ContainerInterface $container) {
    $this->container = $container;
}


function auth($request,$response)
{
    $input = $request->getParsedBody();
    $user = Users::select('id','pword')->where('email','=',$input['email'])->first();

    // verify email address.
    if(!$user) {
        $response->withStatus(404);
        return $response->withJson(['error' => true, 'message' => 'User does not exist.'],404);
    }
    // verify password.
    $salt = getenv('TMS_SALT');
    if (!(sha1($salt.$input['password']) == $user->pword)) {
        $response->withStatus(401);
        return $response->withJson(['error' => true, 'message' => 'Password is incorrect.'],401);
    }
    $now = new \DateTime();
    $future = new \DateTime("+120 minutes");
    $server = $request->getServerParams();
    $jti = (new Base62)->encode(random_bytes(16));
    $payload = [
        "iat" => $now->getTimeStamp(),
        // "exp" => $future->getTimeStamp(),
        "jti" => $jti,
        "sub" => $server["PHP_AUTH_USER"]
    ];
    $token = JWT::encode($payload, getenv('JWT_SECRET'), "HS256");
    $data = array(
        'token' => $token,
        'user_id'=>$user->id,
        // appod'expires' => $future->getTimestamp()
    );
    $response->withStatus(200);
    return $response->withJson($data);
}

}

0 个答案:

没有答案