Why do I get this "These credentials do not match our records" message after authenticating user users in Laravel?

I am new to PHP and to Laravel .

I follow this guide to implement a custom user provider :

https://blog.georgebuckingham.com/laravel-52-auth-custom-user-providers-drivers/

I am using Laravel 5.3 version .

I’ll briefly explain what I need: my Laravel application is just a front-panel application, all the business logic that includes user authentication is done using Java back end application strong>, which provides REST web services .

Making a call:

http://localhost:8080/Extranet/login

and passing the username and password as basic authentication, I get a JSON response that represents the registered user:

{
  "userName": "Painkiller",
  "email": "painkiller@gmail.com",
  "enabled": true
}

So, in my Laravel application, I have to make this call and then parse the previous returned JSON object to generate an authenticated object in the foreground application session.

4 , , , -, :

, , , Laravel http://localhost:8000/login page, retrieveByCredentials ( $credentials), - REST .

, , , , ...

:

https://laravel.com/docs/5.3/authentication#adding-custom-user-providers

, :

retrieveByCredentials Auth:: . "" , . , "where" $ [ ' ']. Authenticatable. .

validateCredentials $user $ . , , Hash:: check $user- > getAuthPassword() $credentials ['password']. true false, , .

, retrieveByCredentials() , Authenticatable).

:

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
use GuzzleHttp\Client;
use function GuzzleHttp\json_encode;
use function GuzzleHttp\json_decode;
use Illuminate\Support\Facades\Log;

class UserProvider implements IlluminateUserProvider
{
    public function retrieveById($identifier)
    {
        // TODO: Implement retrieveById() method.
        \Log::info('retrieveById START');

    }

    public function retrieveByToken($identifier, $token)
    {
        // TODO: Implement retrieveByToken() method.
        \Log::info('retrieveByToken START');
    }

    public function updateRememberToken(Authenticatable $user, $token)
    {
        // TODO: Implement updateRememberToken() method.
        \Log::info('updateRememberToken START');
    }

    public function retrieveByCredentials(array $credentials) {

        // TODO: Implement retrieveByCredentials() method.

        \Log::info('retrieveByCredentials START');
        \Log::info('INSERTED USER CREDENTIAL: '.$credentials['email'] . ' ' .$credentials['password']);

        $client = new Client(); //GuzzleHttp\Client

        $response = $client->get('http://localhost:8080/Extranet/login',
            [
                'auth' => [
                    'nobili.andrea@gmail.com',
                    'pswd'
                ]
            ]);

        $dettagliLogin = json_decode($response->getBody());

        \Log::info('response: '.(json_encode($dettagliLogin)));

        $user = new User('Pippo', 'pippo@google.com', true);

        \Log::info('USER: '.(json_encode($user)));

        return $user;


    }

    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // TODO: Implement validateCredentials() method.
        \Log::info('validateCredentials START');
    }

}

a ( , , JSON) -:

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Support\Facades\Log;

class User implements Authenticatable {

    private $username;
    private $email;
    private $enabled;

    /**
     * User constructor.
     * @param $username
     * @param $email
     * @param $enabled
     */
    public function __construct($username, $email, $enabled)
    {
        $this->username = $username;
        $this->email = $email;
        $this->enabled = $enabled;
    }


    /**
     * @return string
     */
    public function getAuthIdentifierName() {
        // Return the name of unique identifier for the user (e.g. "id")

    }

    /**
     * @return mixed
     */
    public function getAuthIdentifier() {
        // Return the unique identifier for the user (e.g. their ID, 123)
    }

    /**
     * @return string
     */
    public function getAuthPassword() {
        // Returns the (hashed) password for the user
    }

    /**
     * @return string
     */
    public function getRememberToken() {
        // Return the token used for the "remember me" functionality
    }

    /**
     * @param  string $value
     * @return void
     */
    public function setRememberToken($value) {
        // Store a new token user for the "remember me" functionality
    }

    /**
     * @return string
     */
    public function getRememberTokenName() {
        // Return the name of the column / attribute used to store the "remember me" token
    }

    /**
     * @return mixed
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * @param mixed $username
     */
    public function setUsername($username)
    {
        $this->username = $username;
    }

    /**
     * @return mixed
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * @param mixed $email
     */
    public function setEmail($email)
    {
        $this->email = $email;
    }

    /**
     * @return mixed
     */
    public function getEnabled()
    {
        return $this->enabled;
    }

    /**
     * @param mixed $enabled
     */
    public function setEnabled($enabled)
    {
        $this->enabled = $enabled;
    }

}

, retrieveByCredentials() , , :

retrieveByCredentials() , , message: .

enter image description here

, , Laravel ( ), .

? , retrieveByCredentials() .

? ? ?

+4
1

. php- ajax. backend.

, Auth:: try. :

if(Auth::attempt([
 'username' => $request->username, 
 'password' => $request->password
])){

   # success -> redirect user to home page.
   return redirect()->route('home');

}else{
   # return error.
   $errors = new MessageBag(['password' => ['Email and/or password invalid.']]);

   return redirect()->back()->with(['errors' => $errors]); // For PHP.
   return response()->json(['errors' => $errors]); // For Ajax.
}

loginUsingId. .

$user = User::where('email', $request->email)->first();

if($user){
 # Check for password match.
 if($request->password == $user->getAuthPassword()){
   Auth::loginUsingId($user->id, true) /*You will have to get user id from database
                                       or to know somehow what user id is. 
                                       Second param (true) is for remembering user.*/
 }
}
else{
  # User does not exist. return error.
}

.

use App\Auth;
use Illuminate\Support\MessageBag;
0

Source: https://habr.com/ru/post/1668005/


All Articles