如何在我的自定义登录laravel5中使用哈希

时间:2015-05-26 09:26:22

标签: php laravel hash login

我正在开发自己的登录信息,我有以下代码

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";  
        }  
    }

和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'

        ];
    }

}

我的注册控制器

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('/');
    }

模型

<?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');
    } 
}

当我输入密码时,laravel会创建用户,数据库中的字段密码为白色。

1 个答案:

答案 0 :(得分:1)

请试试这个:

public function registro(CreateUserRequest $request){

            $file = $request->file('file');
            $fileName = $file->getClientOriginalName();
            $file->move(public_path().'/uploads/', $fileName);


            $user = User::create([
                'nif' => $request->input('nif'),
                'name' => $request->input('name'),
                'cognoms' => $request->input('cognoms'),
                'birthday' => $request->input('birthday'),
                'email' => $request->input('email'),
                'password' => \Hash::make($request->input('password')),
                'file' => 'uploads/'.$fileName.'',
            ]);


            /**Asignamos el rol a la tabla intermedia***/
            $user = User::find($user->id);
            $user->roles()->attach(1);
            return redirect('/');


   }