0

This is my tables structure i want to make auth in laravel 5.6 :

permission: permission_id , permission

login: login_id , permission_id , Full_name , password

email: email_id , login_id , email

phone: phone_id , login_id , phone_number

users can login with email and password or phone with password each user can have many phone and email and I have 3 type of permission user , admin , manager.

how can I do that ? this is my code it work if email and phone was the field in login table but in my case email and phone are separated from login table because each user can have more than one email or phone.

<?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');
    }

}
?>
  • I don't think the built-in laravel authentication would work for you you probably need to implement [a custom guard](https://laravel.com/docs/5.7/authentication#adding-custom-guards) as well as a custom user provider. – apokryfos Sep 24 '18 at 12:29
  • so how can i do that ? – Safin Mohammed Sep 24 '18 at 13:13
  • The linked section in the manual has some detail, but I have to warn you, it's not easy. – apokryfos Sep 24 '18 at 13:14
  • i want some thing like that SELECT * FROM login JOIN email on email.login_id=login.login_id JOIN phone ON phone.login_id=login.login_id JOIN permission ON permission.permission_id=login.permission_id WHERE email='?' OR phone_number='?' – Safin Mohammed Sep 24 '18 at 13:27

2 Answers2

1

thanks for all for replays
i solve it and this is my code what id did is i make login with login_id and password but i get login_id from email or phone table so i get the login_id of email that user entered

<?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');
    }

}
0

You can write this. Hopefully this will solve your problem.

<?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!');
        }
    }
}
Jahid Mahmud
  • 1,136
  • 1
  • 12
  • 32
  • it is not working for me it is work if the email field was in the login table in my case it is in another table i need some thing like this : SELECT * FROM `login` JOIN email on email.login_id=login.login_id JOIN phone ON phone.login_id=login.login_id JOIN permission ON permission.permission_id=login.permission_id WHERE email='?' OR phone_number='?' – Safin Mohammed Sep 24 '18 at 12:56