Answer the question
In order to leave comments, you need to log in
How to authorize a user from another site?
there are two sites. one receives all data from site 2 (api) because DB one with which the site2 is connected.
On site 1, the user enters a login password in the form to enter site1 - site1 sends a request to site 2 with the data entered by the user (login, password) to the corresponding api route - which, in turn, calls the controller in which the user is searched in the database with such data and if it is, then sends a response to site 1 (true falls or whatever is required). (on Laravel 5...)
site1:
web.php
Route::post('/login','[email protected]')->name('login');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client as GuzzleClient;
class HomeController extends Controller
{
public function login(Request $request)
{
$email = $request->email;
$pass = $request->password;
$client = new GuzzleClient([
'base_uri' => 'http://site1/api/',
]);
$responce = $client->request('POST', 'login', [
'headers' => [
???? - и обязательно ли ?
],
'form_params' => [
'data_form' => [
'email' => $email,
'password'=>$pass
]
]);
$body = $responce->getBody();
dd($body->getContents());
}
Route::post('login', '[email protected]');
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ApiController extends Controller
{
public function login(Request $request)
{
$emailUser = trim($request->data_form['email']);
$passUser =trim($request->data_form['password']);
$userFrom = User::where('email',$emailUser)->get();
dd($userFrom->id);
}
Answer the question
In order to leave comments, you need to log in
The error can be in different places, here we do not have all the information, it turns out that we can only make assumptions.
- Error in configuration files. Check that your .env files or config/database.php match all the options.
- Check that the database (which you rely on) exists and is available in any other way (via the terminal) according to the parameters that you specify in the config files.
About the code
I don’t know which version of Laravel you are using, but trim has been available since version 5.4 of the Guzzle documentation
, you can first get it from the container in the login method and configure it in the service provider.
Variables can not be created, but immediately transferred where necessary, since there is no need for this.
$email = $request->email;
$pass = $request->password;
$emailUser = trim($request->data_form['email']);
$passUser =trim($request->data_form['password']);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question