mercredi 29 août 2018

Laravel 5.5 Auth attempt method only returns false

This might a recurring question on why auth:attempt is returning always false, but despite looking for answers I am not able to solve my issue any further.

I have a students table with id as std_id, email as std_email and password as std_password. to overcome the default password field, I went through some answers and recommend to use this function to override the field name

public function getAuthPassword() {
        return $this->std_password;
    }

and the attempt function is

public function authenticate(Request $request)
    {
        if (Auth::attempt(['std_email' => $request['std_email'], 'password' => $request['std_password']])){
            return response()->json([
                'validated' => true
            ]);
        }else{
            return response()->json([
                'validated' => false
            ]);
        }
    }

So, I've updated two files, auth/LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use \Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function authenticate(Request $request)
    {
        if (Auth::attempt(['std_email' => $request['std_email'], 'password' => $request['std_password']])){
            return response()->json([
                'validated' => true
            ]);
        }else{
            return response()->json([
                'validated' => false
            ]);
        }
    }

}

and the User model as

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $primaryKey = "std_id";
    protected $table = "students";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'std_email', 'std_password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        //'adv_password', 'remember_token',
    ];

    public function getAuthPassword() {
        return $this->std_password;
    }

}

I made sure that in User mode, I've set the primary key as std_id and table as students.

Also, when I register a student, I use bcrypt to has the password and save it in the DB.

public function create(Request $request){

        $account_exists = $this->check_account_exists($request['email']);

        if($account_exists){

            return response()->json(['account'=>true]);

        }else{

            DB::table('students')->insert([
                'std_fname'     => $request['first_name'],
                'std_lname'     => $request['last_name'],
                'std_email'     => $request['email'],
                'std_password'  => bcrypt($request['std_password']),
                'group_id'      => $request['group'],
                'std_isActive'  => 1
            ]);

            return response()->json(['created'=>true]);

        }

    }

At this point i am not getting any answers why the attempt method fails as I've made sure the login information is correct? Can you see if i have missed anything else?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire