某些更改后登录方法不起作用

时间:2019-09-30 10:43:47

标签: php laravel authentication

如何使用 Laravel 5.8 将登录逻辑更改为考虑“移动”字段而不是“电子邮件”?

我将 email 字段更改为 mobile ,但是登录方法(注册正常)始终返回错误响应。

用户模型:


namespace App;


use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;


class User extends Authenticatable
{
    use Notifiable;


    protected $fillable = array('first_name', 'last_name', 'profile_image', 'mobile', 'mobile_verified_at', 'email', 'email_verified_at', 'province_id', 'city_id', 'zone_id', 'status', 'wallet', 'offer_code', 'offer_percent', 'job', 'password');


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

    protected $username = 'mobile';


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    public function username()
    {
        return 'mobile';
    }

    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = \Hash::make($value);
    }



}

LoginController:


namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin(Request $request)
    {
        $request->validate([
            'mobile'    => 'required|exists:users,mobile',
            'password'  => 'required|min:3',
            'captcha'   => 'required|captcha'
        ]);

    }

}


登录视图:

{{--@extends('layouts.app')--}}
@extends('site::layout.template')

@section('content')

    <div class="row justify-content-center">

        <!-- Sidebar -->
        <div class="col-md-6">
            <div class="card">

                <!-- Card header-->
                <div class="card-header max">
                    <h3>Login</h3>
                </div>
                <div class="card-body">

                    @if ($errors->any())
                        <div class="alert alert-danger">
                            <ul>
                                @foreach ($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <div class="row justify-content-center">
                        <div class="col-md-8">

                            <form method="POST" action="{{ route('login') }}">
                                @csrf
                                <div class="form-group row">
                                    <div class="col-md-12">
                                        <label>Mobile</label>
                                        <input type="text" name="mobile" class="form-control form-shadow @error('mobile') is-invalid @enderror" value="{{ old('mobile') }}"  placeholder="09121234567" >
                                        @error('mobile')
                                        <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                        @enderror
                                    </div>
                                </div>

                                <div class="form-group row">
                                    <div class="col-md-12">
                                        <label>Password</label>
                                        <input type="password" name="password" class="form-control form-shadow @error('password') is-invalid @enderror" placeholder="*********" >
                                        @error('password')
                                            <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                        @enderror
                                    </div>
                                </div>


                                <div class="form-group row">
                                    <div class="col-md-12">
                                        <div class="form-check">
                                            <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
                                            <label class="form-check-label" for="remember">
                                                Remember me
                                            </label>
                                        </div>
                                    </div>
                                </div>




                                <div class="form-group row">
                                    <div class="col-md-8">
                                        <input type="text" class="form-control form-shadow @error('captcha') is-invalid @enderror" name="captcha" placeholder="enter front code" >
                                        @error('captcha')
                                            <span class="invalid-feedback" role="alert">
                                            <strong>{{ $message }}</strong>
                                        </span>
                                        @enderror
                                    </div>
                                    <div class="col-md-4">
                                        <img  id="refresh" class="captcha" src="{!! captcha_src('flat') !!}" alt="">
                                    </div>

                                </div>

                                <div class="form-group row">
                                    <button class="btn btn-primary btn-lg btn-block">ورود</button>
                                </div>
                                @if (Route::has('password.request'))
                                    <a class="btn btn-link" href="{{ route('password.request') }}">
                                      Reset password ?
                                    </a>
                                @endif

                            </form>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

@endsection


验证码很好,工作正常。现在,我需要将登录方法从“电子邮件”更改为“移动”。

2 个答案:

答案 0 :(得分:0)

只需在Auth / LoginController中添加

函数username()

 public function username()
{
    return 'email';
}

然后替换您想要的电子邮件。

答案 1 :(得分:0)

您应该看看official documentation

  

自定义用户名

     

默认情况下,Laravel使用电子邮件字段进行身份验证。如果要>自定义此设置,可以在LoginController上定义一个用户名方法:

     
public function username()
{
   return 'username';
}

因此,在您的情况下,您必须在LoginController中添加此功能,并将username更改为移动设备:

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class LoginController extends Controller
{


    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function username()
    {
        return 'mobile';
    }

    /**
     * Validate the user login request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function validateLogin(Request $request)
    {
        $request->validate([
            'mobile'    => 'required|exists:users,mobile',
            'password'  => 'required|min:3',
            'captcha'   => 'required|captcha'
        ]);

    }

}

如果您需要更具动态性的逻辑,则应查看AuthenticatesUsers特性的源代码,并覆盖登录功能。