Laravel 6-使用电话或电子邮件登录 [英] Laravel 6 - login with Phone or Email

查看:88
本文介绍了Laravel 6-使用电话或电子邮件登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 6,希望用户可以通过电子邮件或手机登录.

I'm using Laravel 6 and want user can alternatively login by email or mobile.

到目前为止我的尝试:

  • 迁移正常.
  • 注册工作正常

每次登录发送电子邮件移动时,登录状态都可以很好地运行,并且对loginController.php进行了以下更改.

Login works fine one at a time either email or mobile with below changes in loginController.php.

public function username()
{
    return 'mobile'; // or email
}

以及 login.blade.php ,用于从通过电子邮件发送到移动设备的HTML表单.

我试图通过 @error指令来验证电子邮件和电话,例如@error('email')|| @error('mobile')无效@enderror,但提交后无任何操作.下面是示例

I tried to validate both email and phone by @error directive, like @error('email') || @error('mobile') is-invalid @enderror but no action after submit. below is the example

<input id="user_login" type="text" class="form-control @error('email') || @error('mobile') is-invalid @enderror" name="user_login" value="{{ old('user_login') }}" required autocomplete="user_login" autofocus>

                            @error('user_login')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror

您的帮助将不胜感激.

推荐答案

提供的刀片模板包含不正确的刀片语法,这些语法已转换为无效的php(||转换为纯文本而不是OR操作).

The provided blade template contains incorrect blade syntax that gets transformed to invalid php(the || is converted to plain text instead of the OR operation).

在下面签出生成的php:

Checkout the resulting php below:

这可以通过两次使用@errorenderror来解决:

This can be fixed by using the @error and enderror twice:

<form action="{{ route('test.store') }}" method="post">
    <input id="user_login" type="text" class="form-control @error('email') is-invalid @enderror @error('mobile') is-invalid @enderror" name="user_login" value="{{ old('user_login') }}" required autocomplete="user_login" autofocus>

    @error('user_login')

可以使用附加字段来验证以下内容:

To add an additional field for authenticating the following can be used:

resources/auth/login.blade.php

resources/auth/login.blade.php

   <form method="POST" action="{{ route('login') }}">
                        @csrf

                        <div class="form-group row">
                            <label for="phone" class="col-md-4 col-form-label text-md-right">{{ __('Phone') }}</label>

                            <div class="col-md-6">
                                <input id="phone" type="text" class="form-control @error('phone') is-invalid @enderror" name="email" value="{{ old('phone') }}" required autocomplete="email" autofocus>

                                @error('phone')
                                <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

                        <div class="form-group row">
                            <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                            <div class="col-md-6">
                                <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" autocomplete="email" autofocus>

                                @error('email')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

app/Http/Auth/LoginController.php

app/Http/Auth/LoginController.php

覆盖username()方法以使用电话(如果提供)并使用电子邮件作为备用.

Overriding the username() method to use phone if provided and email as fallback.

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    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 request()->has('phone') ? 'phone' : 'email';
    }
}

或具有单个字段:

or with a single field:

  <div class="form-group row">
                            <label for="user_identifier" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address/Phone') }}</label>

                            <div class="col-md-6">
                                <input id="user_identifier" type="text" class="form-control @error('user_identifier') is-invalid @enderror" name="user_identifier" value="{{ old('user_identifier') }}" autofocus>

                                @error('user_identifier')
                                    <span class="invalid-feedback" role="alert">
                                        <strong>{{ $message }}</strong>
                                    </span>
                                @enderror
                            </div>
                        </div>

和相关的LoginController方法:

    public function username()
    {
        $identifier = request()->get('user_identifier');

        if(filter_var($identifier, FILTER_VALIDATE_EMAIL)){
            return 'email';
        }

        return 'phone';
    }

这篇关于Laravel 6-使用电话或电子邮件登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆