PHP laravel auth与我的自定义字段

时间:2016-03-14 16:04:27

标签: php laravel

我已经看过如何使用laravel的默认认证机制的教程。 我想让我的身份验证适用于不同的参数。 我有以下内容存储在控制器中,用于注册用户:

 public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'username' => 'required|max:255',
        'email' => 'required',
        'password' => 'required|alphaNum|min:6',

    ]);

    if ($validator->fails()) {
        return redirect('/register')
            ->withInput()
            ->withErrors($validator);
    }
    $confirmation=$this->createConfirmation_code();
    $user = new User;
    $user->first_name = $request->first_name;
    $user->last_name = $request->last_name;
    $user->username = $request->username;
    $user->email = $request->email;
    $user->password = -$request->password;
    $user->country = $request->country;
    $user->street = $request->street;
    $user->zip = $request->zip;
    $user->state = $request->state;
    $user->city = $request->city;
    $user->state = $request->city_state;
    $user->institute = $request->institute;
    $user->confirmation_code=$confirmation;
    $user->confirmed='no';
    $this->sendEmailReminder($request,$user);
    User::create([
        'first_name' => $user->first_name,
        'last_name' => $user->last_name,
        'username' =>$user->username,
        'email' =>$user->email,
        'password' => bcrypt($user->password),
        'country' =>$user->country,
        'street' =>$user->street,
        'zip' => $user->zip,
        'state' =>$user->state,
        'institute' =>$user->institute,
        'confirmation_code' =>$user->confirmation_code,
        'confirmed' => $user->confirmed
    ]);
    return redirect('/confirm')->with('user',$user);
}

然后登录我使用以下内容:

public function  login(Request $request)
{
    $rules = array(
        'username' => 'required', 
        'password' => 'required'
    );
    $validator = Validator::make($request->all(), $rules);

    if ($validator->fails()) {
        return Redirect::to('/login')
            ->withErrors($validator);
    } else {

        $userdata = array(
            'username' => $request->username,
            'password' => $request->password
        );

        // attempt to do the login
        if (Auth::attempt($userdata)) {
            return redirect('/{username}');

        } else {
            return Redirect::to('/login');
        }

    }
}

但是我的登录失败了。 这是我的Authenticate类中的句柄:

    public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->guest()) {
        if ($request->ajax() || $request->wantsJson()) {
            return response('Unauthorized.', 401);
        } else {
        }
    }

    return $next($request);
}

这是我的用户模型:

class User extends Authenticatable
{
protected $fillable = [
    'first_name', 'last_name','username','email','password','country','street','zip','state','institute','confirmation_code','confirmed'
];

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

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

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

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

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

public function setRememberToken($value)
{
    $this->remember_token = $value;
}

public function getRememberTokenName()
{
    return 'remember_token';
}
}

我没有对默认的auth.php文件进行任何更改。任何人都可以帮助我,我怎样才能让它发挥作用?

0 个答案:

没有答案