用于客户端的Laravel Passport Api访问令牌

时间:2018-10-27 08:39:03

标签: laravel api eloquent laravel-passport laravel-middleware

我对laravel护照api知之甚少,我为android设备制作了serveral api,但从未在网络(客户端)上使用过,我有点困惑如何将其与网络路由一起使用,这意味着我做了一个登录api,用于登录用户的角色。

public function login(Request $request)
{

    $rules = array(
        'email' => 'required',
        'password' => 'required',

    );
    $messages = array();
    $validator = Validator::make($request->all(), $rules, $messages);

    if ($validator->fails()) {
        return [
            'status' => 204,
            'message' => $validator->errors()->first()
        ];
    }

    $user = User::where('email', $request->email)->first();
    if (!$user) {
        return response(['status' => 204, 'message' => 'User Not Found']);
    }

    if (Hash::check($request->password, $user->password)) {
        $http = new Client;
        $response = $http->post(url('oauth/token'), [
            'form_params' => [
                'grant_type' => 'password',
                'client_id' => '2',
                'client_secret' => 'oWXBChJA8TUItWnvZ3J52KNhGMqsPkEqpDyhjnNf',
                'username' => $request->email,
                'password' => $request->password,
                'scope' => '',
            ],
        ]);
        $getVerifyStatus = AppStatus::where('id', $user->profile_verify_status_id)->value('name');


        if ($getVerifyStatus == 'verified') {
            return response(['status' => 200, 'message' => 'Sign In Successfully','auth' => json_decode((string)$response->getBody(), true), 'user' => $user]);

        }
        else {
            return response(['message' => 'Profile Not Verified', 'status' => 204]);
        }
    }
    else {
        return response(['message' => 'Password Not Match', 'status' => 204]);
    }

}

它使用access_token响应我的完整用户登录详细信息,现在我在客户端项目中使用此api,这是该代码

public function loginShipper()
{
    return view('login');
}

public function login()
{

    $validator = Validator::make(
        array(
            'email' => $_POST['email'],
            'password' => $_POST['password']
        ),

        array(
            'email' => 'required',
            'password' => 'required'
        )
    );

    if ($validator->fails()) {
        $messages = $validator->messages();
        return redirect('/')->with('message', $messages);
    }
    else
    {
        //  $u_password = (Hash::check();

        $url = 'http://localhost:81/mvp/api/shipper/login';
        $myvars = 'email=' . $_POST['email'] . '&password=' . $_POST['password']  . '&created_at=' . '';


        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        curl_close($ch);

        $json_response = json_decode($response);

        // print_r($json_response);
        // exit();

        $user = User::where(array('email' => $_POST['email']))->first();



        if ($user) {

            Session::put('user_id', $user->id);

            Session::put('email', $user->email);

            Session::put('access_token', $json_response->auth->access_token);

            Session::save();
            return Redirect::route('brokers');
            //$messages = $validator->messages();
            // return redirect('/')->with('message', $messages);
        }

表示在登录时将其重定向到代理路由,但是如果我在Web路由中使用auth:web中间件,它将在登录页面上重定向到我,但是如果我删除auth中间件,它将将我重定向到代理页面,但是由于用户未通过身份验证, Auth :: user()向我发送空值。在发布此问题后,我进行了大量搜索。 希望你理解我的问题。

0 个答案:

没有答案