How do I log into an application from another application in Laravel using an API?

Viewed 17

I have a POS Application and Inventory Application hosted differently but sharing the same database. I want to log into the Inventory from the POS System in laravel. I have worked something out but isn't working. Please I need assistance. It redirects me to the invenotry system login page. it's unable to authenticate and login me into the inventory system. Please any assistance?

This is the login function in my controller from the POS SYSTEM

public function inventoryLogin(){

$response = Http::asForm()->post('http://inventory.sys/api/login/from-pos', [
            'email' => auth()->user()->email,
            'role' => auth()->user()->role,
        ]);
        return $response;
}

Also, my API.php in my INVENTORY SYSTEM

Route::post('login/from-pos', function(Request $request){
    $rules = array(
        'email' => 'email|required|max:300',
        'role' => 'required|max:300',
    );
    
    $validator = Validator::make($request->all(), $rules);
    if ($validator->fails()) {
        return $validator->errors();
    }

    if (request('role') == "Administrator") {
        $user = User::where('email', request('email'))->firstOrFail();
        $token = $user->createToken('auth_token')->plainTextToken;
        auth()->login($user);
        return redirect('/dashboard');
    }else{
        return response([
            'status' => 419,
            'Message' => "Sorry! unauthorized user"
        ]);
    }
    
});
0 Answers
Related