登录页面空白

时间:2019-07-22 13:03:24

标签: php laravel

我使用Laravel 5.8.29, 我想创建多登录功能, 问题是,点击登录按钮后页面显示为空白,

我正在学习来自https://medium.com/@alfinchandra4/catatan-laravel-manual-login-logout-multi-auth-fa9c8ca12e54

的教程

我从web.php出发的路线

Route::get('/login', 'LoginController@getLogin')->middleware('guest');
Route::post('/login', 'LoginController@postLogin');
Route::get('/logout', 'LoginController@logout');

Route::get('/admin', function(){
    return view('admin.admin');
})->middleware('auth:admin');

Route::get('/user', function(){
    return "ini user";
})->middleware('auth:user');

Route::get('/superuser', function(){
    return "ini superuser";
})->middleware('auth:superuser');

控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Admin;
use App\User;
use App\superUser;
use Auth;

class LoginController extends Controller {

  public function getLogin(){
    $pagetitle = "Tambah User";
    return view('auth.login');
  }

  public function postLogin(Request $request){

      // Validate the form data
    $this->validate($request, [
      'email'       => 'required|email',
      'password'    => 'required'
    ]);

      // Attempt to log the user in
      // Passwordnya 
    if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password])) {
        // if successful, then redirect to their intended location 
        echo'ss';
        return redirect()->intended('/admin');
    } else if (Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password])) {
        return redirect()->intended('/user');
    } else if (Auth::guard('superuser')->attempt(['email' => $request->email, 'password' => $request->password])) {
        return redirect()->intended('/superuser');
    }

  }

  public function logout(){

    if (Auth::guard('admin')->check()) {
        Auth::guard('admin')->logout();
    } elseif (Auth::guard('user')->check()) {
        Auth::guard('user')->logout();
    }elseif (Auth::guard('superuser')->check()) {
        Auth::guard('superuser')->logout();
    }
    return redirect('/login');
    }

}

模型,实际上有3个模型(管理员,用户,超级用户),但相同,只是不同的表。

<?php


namespace App;

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

class Admin extends Authenticatable{
    use Notifiable;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = "admins";
    protected $primaryKey = "id";
    protected $fillable = [
        'name', 'email', 'password',
    ];
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

观看次数

<h1>hallo admin</h1>
<h2>Admin Page {{ Auth::guard('admin')->user()->name }}</h2>

<a href="/logout">Logout {{ Auth::guard('admin')->user()->name }} ??</a>

config / auth.php

<?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',
            'hash' => false,
        ],

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

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

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

        'user-api'      => [
            'driver'    => 'token',
            'provider'  => 'user',
        ],

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

        'superuser-api' => [
            'driver'    => 'token',
            'provider'  => 'superuser',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | 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' => [

        'user' => [
            'driver' => 'eloquent',
            'model'  => App\User::class,
        ],

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

        'superuser' => [
            'driver' => 'eloquent',
            'model'  => App\Superuser::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,
        ],
    ],

];

1 个答案:

答案 0 :(得分:1)

之后添加dd('finish')
if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password])) {
// if successful, then redirect to their intended location 
    echo'ss';
    return redirect()->intended('/admin');
} else if (Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password])) {
    return redirect()->intended('/user');
} else if (Auth::guard('superuser')->attempt(['email' => $request->email, 'password' => $request->password])) {
    return redirect()->intended('/superuser');
}

postLogin方法中。 如果看到“完成”,则说明您的条件失败。

所以像这样更新您的方法:

if (Auth::guard('admin')->attempt(['email' => $request->email, 'password' => $request->password])) {
// if successful, then redirect to their intended location 
    echo'ss';
    return redirect()->intended('/admin');
} else if (Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password])) {
    return redirect()->intended('/user');
} else if (Auth::guard('superuser')->attempt(['email' => $request->email, 'password' => $request->password])) {
    return redirect()->intended('/superuser');
}else{ //if failed login
    return back(); //or everything
}