简体   繁体   中英

Laravel: Add a custom query before login / Auth

I am new here and just started using laravel,

I want to add a new query before user click on the 'Login' button.

Ex.: After the user typed his username and password and clicked the Login button,

I want to do a query first if the username is existing on a different table.

if (exists) {
      // do laravel default auth.
} else {
     // add the username in the table
     // do laravel default auth.
}

hope somebody can help me,.

Thanks

If that's the case, then you have to customize the default login method provided by the laravel. see code below

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /**
     * Handle an authentication attempt.
     *
     * @param  \Illuminate\Http\Request $request
     *
     * @return Response
     */
    public function login(Request $request)
    {
        $credentials = $request->only('username', 'password');

        $username = Othertable::where('username', $request->username)->get();

        if($username->count() > 0 ) {
           if (Auth::attempt($credentials)) {
              // Authentication passed...
              return redirect()->intended('dashboard');
           } 
        } else {
            //add the username in the table
            //do laravel default auth.
        }
    }
}

read docs here authentication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM