Pionas
Pionas

Reputation: 346

Laravel 5 Auth change table columns

I changed some fields in users database table.

table name: users
primaryKey: user_id
username: user_username
password: user_password
e-mail: user_mail

in Illuminate\Foundation\AuthAuthenticatesUsers I added protected $username = 'user_username';

When I try login to my account, I see a blank page after I give my username and password. Debug is on but not working. What happened?

Auth::attempt(array(
            'user_username' => 'pionas',
            'user_password'  => '12345',
        ));

In User model I added getAuthPassword and changed the column name to user_password. Log is clear.

Auth::loginUsingId(1); - not working

Probably all methods in Auth are not working.

My User model is:

<?php
namespace App\User;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';
protected $primaryKey = 'user_id';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
protected $hidden = array('user_password', 'remember_token');

/**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    public function comment()
    {
        return $this->hasOne('UserField', 'user_id');
    }
    /**

Upvotes: 1

Views: 2835

Answers (2)

Ricardo Vigatti
Ricardo Vigatti

Reputation: 525

You will need to do two things.

  1. On your model, add the method getAuthPassword() (you already do that)

    public function getAuthPassword()
    {
        return $this->user_password;
    }
    
  2. On your AuthController, when passing the $credentials to the $this->auth->validate() method (or the Auth::attemp() or anything else), you must set the array with a key to check for the password, and this key SHOULD be named as "password". All others keys can have any name, and you will use the name of your username column

    $credentials['user_username'] = $request->input('user_username');
    $credentials['password'] = $request->input('user_password');
    
    Auth::attempt($credentials);
    

Upvotes: 0

Jamesking56
Jamesking56

Reputation: 3901

You seem to have stripped some of the required traits that Laravel 5.1 uses. Here is an updated User model with those traits restored:

<?php

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract
{
    use Authenticatable, Authorizable, CanResetPassword;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';


    protected $primaryKey = 'user_id';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['user_username', 'user_mail', 'user_password', 'user_group_id', 'user_code', 'user_rang'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('user_password', 'remember_token');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->user_mail;
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->user_password;
    }

    public function comment()
    {
        return $this->hasOne('UserField', 'user_id');
    }

    /**
     * Scope a query to only include users of a given type.
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeOfStatus($query, $type)
    {
        return $query->where('user_status', $type);
    }

}

Upvotes: 2

Related Questions