Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bit1's avatar
Level 1

Laravel Passport Api Access Token used For Client Side

Hi! i have little knowledge of laravel passport api, i made serveral api for android devices but never used on web (client side), i'm little confused how to used it with web routes means i made a login api that login a user on the base of its role.

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]);
    }

}

it response me the full user login detail with access_token , now i'm using this api in my client side project here is that code

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);
        }

means when it logging it redirect the brokers route but if i used auth:web middleware in web route it redirects to me on login page, but if i remove auth middleware it redirect me to broker page but then user not be authenticated because Auth::user() sends me null. i searched alot after that i am posting that question. hope you understand my question.

0 likes
0 replies

Please or to participate in this conversation.