0

这是我想在 laravel 5.6 中进行身份验证的表结构:

权限:permission_id,权限

登录: login_id , permission_id , Full_name , 密码

电子邮件: email_id , login_id , 电子邮件

电话: phone_id , login_id , phone_number

用户可以使用电子邮件和密码登录,也可以使用密码登录每个用户可以拥有多个电话和电子邮件,我有 3 种权限用户,管理员,经理。

我怎样才能做到这一点 ?这是我的代码,如果电子邮件和电话是登录表中的字段,但在我的情况下,电子邮件和电话与登录表是分开的,因为每个用户可以拥有多个电子邮件或电话。

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller {

use AuthenticatesUsers;

    public function showLoginForm() {
        return view('panel.login');
    }

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


    protected function attemptLogin(Request $request)
    {

        $username = $request->username;

        return $this->guard()->attempt(
            $this->credentials($request), $request->filled('remember')
        );

    }

    protected $redirectTo = '/panel';

    public function __construct() {
        $this->middleware('guest')->except('logout');
    }

}
?>
4

2 回答 2

1

感谢所有重播
我解决了它,这是我的代码 id 所做的是我使用 login_id 和密码登录,但我从电子邮件或电话表中获取 login_id,所以我得到用户输入的电子邮件的 login_id

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller {


use AuthenticatesUsers;

    public function showLoginForm() {
        return view('panel.login');
    }

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

    protected function attemptLogin(Request $request) {

        $id = '0';
        $username=trim( $request->username );
        if( filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ) {
           $id= Email::select('login_id')->where('email',$username)->first();

        }elseif (is_numeric($username)) {
            $id= Phone::select('login_id')->where('phone_number',$username)->first();
        }        
        $id=($id==null)?0:$id->login_id;        
        return $this->guard()->attempt(
                        ['Login_id' => $id, 'password' => $request->password], $request->filled('remember')
        );

    }


    protected $redirectTo = '/panel';

    public function __construct() {

        $this->middleware('guest')->except('logout');
    }

}
于 2018-09-25T19:13:44.137 回答
0

你可以写这个。希望这能解决您的问题。

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller {
    use AuthenticatesUsers;
    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/panel';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct() {
        $this->middleware('guest', ['except' => 'logout']);
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function login() {
        return view('panel.login');
    }
    protected function credentials(Request $request) {
        $field = filter_var($request->input($this->username()), FILTER_VALIDATE_EMAIL) ? 'email' : 'username';
        $request->merge([$field => $request->input($this->username())]);
        return array_merge($request->only($field, 'password'));
    }
    public function username() {
        return 'email';
    }
    public function authenticate(Request $request) {
        $credentials = $this->credentials($request);
        if (Auth::attempt($credentials)) {
            return redirect()->intended('/');
        } else {
            return redirect()->intended('login')->with('error', 'Invalid login credentials. Check your email address or username and password!');
        }
    }
}
于 2018-09-24T12:31:20.567 回答