I am developing my own login and I have the following code
public function login(Request $request ) {
$email = \Request::input('email');
$password = \Request::input('password');
if (Auth::attempt(['email' => $email, 'password' => $password]))
{
//echo "success";
return redirect('home');
}
else {
return "fail";
}
}
And the CreateUserRequest
<?php namespace App\Http\Requests;
use App\Http\Requests\Request;
class CreateUserRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'nif' => 'required | max:9 ',
'name' => 'required | max:255',
'email' => 'required',
'cognoms' => 'required | max:255',
'birthday' => 'required',
'password' => 'required | confirmed',
'password_confirmation' => 'required',
'municipios' => 'required | Integer|Min:1',
'presentacion' => 'required',
'file' => 'required'
];
}
}
My register controller
public function registro(CreateUserRequest $request){
$usuario = new User();
$usuario->nif = \Request::input('nif');
$usuario->name = \Request::input('name');
$usuario->cognoms = \Request::input('cognoms');
$usuario->birthday = \Request::input('birthday');
$usuario->email = \Request::input('email');
$usuario->password= \Request::input('password');
/**Foto del usuario**/
$file = \Request::file('file');
$fileName = $file->getClientOriginalName();
$file->move(public_path().'/uploads/', $fileName);
$usuario->file = 'uploads/'.$fileName.'';
$usuario->save();
/**Asignamos el rol a la tabla intermedia***/
$user = User::find($usuario->id);
$user->roles()->attach(1);
return redirect('/');
}
The model
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['nif','name','cognoms','email', 'password','idempresa','id_poblacion','id_online',];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
public function empresa()
{
return $this->belongsTo('Empresa');
}
public function municipio()
{
return $this->belongsTo('App\Ciudad','id_poblacion');
}
public function roles()
{
return $this->belongsToMany('App\Rol')->withPivot('user_id','rol_id');
}
public function mensajes()
{
return $this->belongsToMany('App\User')->withPivot('id_emisor','id_receptor');
}
public function subastas(){
return $this->hasMany('App\Subasta','id_creador','id');
}
public function pujas(){
return $this->hasMany('App\Puja','id_subasta','id');
}
}
When I put the password , the laravel create the user andthe field password in database is white.
if (Auth::attempt(array('email' => $email, 'password' => $password)))?$usuario->password = \Hash::make('password');to$usuario->password = \Request::input('password');and register a new user and try to login once more. This is becausepublic function setPasswordAttribute($password) { $this->password = \Hash::make($password); }in the model will automatically hash the password before storing it in db