簡體   English   中英

在Laravel 5.2中通過電子郵件或電話登錄

[英]Login with either email or phone in Laravel 5.2

我是Laravel的新手,我正在開發一個應用程序,它將要求用戶使用電子郵件(少數)或電話號碼(多數)登錄。 目前,我正在使用默認的Laravel身份驗證和經過修改的注冊,以允許用戶提供其電話號碼。 我發現的解決方案顯示了如何使用用戶名而不是電子郵件登錄。 我該如何實現? 我正在使用PHP 7.0運行Laravel 5.2

這是我的用戶模型:

protected $fillable = ['firstname', 'lastname', 'email','phone','account_type','county','sub_county', 'password',];

protected $hidden = ['password', 'remember_token',];

這是我的AuthenticateUsers.php:

 /**
 * Get the needed authorization credentials from the request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return array
 */
protected function getCredentials(Request $request)
{
    return $request->only($this->loginUsername(), 'password');
}

/**
 * Validate the user login request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return void
 */
protected function validateLogin(Request $request)
{
    $this->validate($request, [
        $this->loginUsername() => 'required', 'password' => 'required',
    ]);
}

/**
 * Get the login username to be used by the controller.
 *
 * @return string
 */
public function loginUsername()
{
    return property_exists($this, 'username') ? $this->username : 'email';
}

我的登錄表單中的電子郵件輸入字段:

<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input type="text" class="form-control" name="email" value="{{ old('email')    }}">

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>

在您的loginUsername函數中,進行以下更改以在使用電子郵件默認值之前檢查請求是否具有電話屬性。

public function loginUsername($request) { return $request->has('phone') ? 'phone' : 'email'; }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM