如何在流明api中进行身份验证?

时间:2017-06-20 10:56:41

标签: php laravel authentication

我已经使用Auth::attempt(['email' => $request->email_id, 'password' => $request->password])进行了检查,它在网络方面起作用,但在api方面给出了错误。

if (Auth::attempt(['email' => $request->email_id, 'password' => $request->password])) 
            dd("Successfully Authenticated ");
                 else dd("false");

1 个答案:

答案 0 :(得分:0)

使用JWT身份验证来验证您的流明api

composer require tymon/jwt-auth

安装后按照配置

https://github.com/tymondesigns/jwt-auth/wiki

以下身份验证功能有助于验证并返回API令牌

use JWTAuth;
use Tymon\JWTAuth\Exceptions\JWTException;

class AuthenticateController extends Controller
{
    public function authenticate(Request $request)
    {
        // grab credentials from the request
        $credentials = $request->only('email', 'password');

        try {
            // attempt to verify the credentials and create a token for the user
            if (! $token = JWTAuth::attempt($credentials)) {
                return response()->json(['error' => 'invalid_credentials'], 401);
            }
        } catch (JWTException $e) {
            // something went wrong whilst attempting to encode the token
            return response()->json(['error' => 'could_not_create_token'], 500);
        }

        // all good so return the token
        return response()->json(compact('token'));
    }
}