Laravel:Auth :: login()没有启动会话

时间:2017-09-18 01:01:59

标签: php laravel authentication laravel-5.5

我正在跨两个数据库创建身份验证,这意味着用户可以对数据库A表用户或数据库B表user_account进行身份验证。所以现在,我已经陷入了这个问题。

如果我尝试使用数据库A表用户的凭据登录,它可以正常工作我能够登录。但是,当我尝试使用表user_account的数据库B的凭据时,我的查询正在运行,它会完全检查凭据是否正确,但我无法通过会话,只是按原样重定向。

我也使用Auth :: login()因为我的数据库B使用MD5来密码

这是我的LoginController.php

<?php

namespace App\Http\Controllers\Auth;

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

use App\UserAccount;
use App\User;
use Auth;


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.
    |
    */
    public function showLoginForm()
    {
        return view('auth.login');
    }

    public function login(Request $request){


        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required'
        ]);


        /*if(Auth::guard('master')->attempt(['email' => $request->email, 'password'=> $request->password], $request->remember)){

            return redirect()->intended(route('home'));
            //dd($request->all());
        }*/


        $user = UserAccount::where('Email', '=', $request->email)->where('password','=', md5($request->password) )->first(); //This is working
        if(!empty($user)){
            $userz = UserAccount::find($user->user_id);
            Auth::login($userz); //Not starting the session??

            //dd($request->all());
            return redirect()->intended(route('home'));
        }else if (Auth::guard('web')->attempt(['email' => $request->email, 'password'=>$request->password], $request->remember)){
            return redirect()->intended(route('home'));
            //dd($request->all());
        }
        return redirect()->back()
               ->withInput($request->only('email','remember'))
               ->withErrors(['email' => ['Invalid Credentials']]);

    }



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

UserAccount.php模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;


class UserAccount extends Authenticatable
{
    use Notifiable;
    protected $primaryKey = 'user_id';
    protected $table = "user_account";
    protected $guard = 'master';
    protected $connection = 'mysql2';
    //protected $connection = 'mysql2';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */


    public function changeConnection($conn)
    {
        $this->connection = $conn;
    }

    protected $fillable = [
        'Name', 'Email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;
    //protected $connection = 'mysql2';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

App \ Web.php路由

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

//Route::group()

Route::get('/', 'PagesController@index')->name('index');
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::prefix('admin')->group(function(){
    Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
    Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');
    Route::get('/', 'AdminController@index')->name('admin');
});

配置\ AUTH

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],




    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],

        'admin-api' => [
            'driver' => 'token',
            'provider' => 'admin',
        ],
        'master' => [
            'driver' => 'session',
            'provider' => 'master',
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        'master' => [
            'driver' => 'eloquent',
            'model' => App\UserAccount::class,
        ],

        'custom' => [
            'driver' => 'custom',
            'model' => App\UserAccount::class,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'master' => [
            'provider' => 'master',
            'table' => 'password',
            'expire' => 60,
        ],
    ],

];

这是我的第一个Laravel项目,我是全新的,感谢您的帮助

0 个答案:

没有答案