1

I have a Laravel 6 application.

In a typical application, the user would just specify their email, where the email is unique.

However, in my application, I have 2 columns in the User model that is used to authenticate users.

  • app_id
  • email
  • unique(app_id, email)

So in order to login, we need to pass both an app_id and an email, along with the password. The same email could be used across different app_ids.

How would I achieve this?

3
  • 1
    have you checked the authentication docs for manually authenticating the user? that combined with looking into the LoginController should be all you need Commented Jul 24, 2020 at 16:28
  • The issue is that I can only find customising a single column. Not querying across columns. Commented Jul 24, 2020 at 16:32
  • laravel.com/docs/6.x/authentication#authenticating-users it tells you how to do this .. once you know how attempt works you can adjust specific methods of the LoginController to adjust the credentials and the validation as needed ... if you are not using the LoginController then you have what you need for the most part Commented Jul 24, 2020 at 16:39

1 Answer 1

2

The default login actions provided by Auth::routes() are the following:

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');

This is the default login function, part of the AuthenticatesUsers trait used by the LoginController:

    /**
     * Handle a login request to the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function login(Request $request)
    {
        $this->validateLogin($request);

        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }

        if ($this->attemptLogin($request)) {
            return $this->sendLoginResponse($request);
        }

        // If the login attempt was unsuccessful we will increment the number of attempts
        // to login and redirect the user back to the login form. Of course, when this
        // user surpasses their maximum number of attempts they will get locked out.
        $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
    }

There are a few ways to approach this.

Option 1: Override login function in LoginController

# app/Http/Controllers/Auth/LoginController.php

    public function login(Request $request)
    {
        // add more stuff like validation, return the view you want, etc.
        // This is barebones
        auth()->attempt($request->only(['app_id', 'login', 'password']);
    }

Option 2: Override both the validateLogin and the credentials functions in LoginController

# app/Http/Controllers/Auth/LoginController.php

    protected function validateLogin(Request $request)
    {
        $request->validate([
            'app_id' => 'required|string',
            'email' => 'required|string',
            'password' => 'required|string',
        ]);
    }

    /**
     * Get the needed authorization credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only('app_id', 'email', 'password');
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.