多验证会话不起作用

时间:2017-11-18 15:59:54

标签: php laravel authentication

我想问一下Laravel中的Multi-Auth。

我可以完美地注册和登录,但是当我使用Auth::guard('guests')->check()时,它不起作用。 (我认为会议不起作用,但不知道为什么。)

你们可以查看下面的图片和代码,知道我在说什么:

picture 1 picture 2

代码

auth.php使用配置保护

 <?php

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

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
         'guests' => [
            'driver' => 'session',
            'provider' => 'guests',
        ],
        'guests-api' => [
            'driver' => 'token',
            'provider' => 'guests',
        ],

    ],

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

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
         'guests' => [
            'provider' => 'guests',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

在auth.php中提到的Guest.php

 <?php

namespace App;

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

class Guests extends Authenticatable
{
    use Notifiable;
    protected $guard = 'guests'; 

     public function getAuthPassword()
    {
            return $this->guest_password;
    }

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'guest_email', 'guest_password',
    ];

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

Controller_guest.php使用注册和填充

    <?php

namespace App\Http\Controllers\frontend;

use App\Http\Controllers\Controller;
use Request;
use DB;
use App\Http\Requests\Request_Guest_Register;
use App\Http\Requests\Request_Guest_Register_Small;
use Auth;
use App\Guests;
use App\User;
use Illuminate\Auth\Authenticatable;

class controller_guest extends Controller
{
 use Authenticatable;

    protected $redirectTo = '/';

     public function __construct()
    {
        $this->middleware('guest');
    }
    public function guest_register_account()
    {
        return view('frontend.shop-cart.register');

    }

    public function post_guest_register_account(Request_Guest_Register $request)
    {
                    $title = Request::get("title");
                    $firstname = Request::get("firstname");
                    $lastname = Request::get("lastname");
                    $email = Request::get("g_email");
                    $password = Request::get("g_password");
                    $password=bcrypt($password);
                    $days = Request::get("days");
                    $months = Request::get("months");
                    $years = Request::get("years");
                    $birth = date_format(date_create($years."/".$months."/".$days),'y-m-d');

                    $guests = new Guests();
                    $guests->guest_title = $title;
                    $guests->guest_name = $firstname." ".$lastname;
                    $guests->guest_email = $email;
                    $guests->guest_password = $password;
                    $guests->guest_birth = $birth;
                    $guests->save();

                    return redirect(url('/'));

    }
     // trang dang ky khong day du (trong cart -> dang nhap)
    public function post_guest_register_account_small(Request_Guest_Register_Small $request)
    {
                    $email = Request::get("g_email");
                    $password = Request::get("g_password");
                    $password=bcrypt($password);

                    $guests = new Guests();
                    $guests->guest_email = $email;
                    $guests->guest_password = $password;
                    $guests->save();


                    return redirect(url('guest_login'))->with('thongbao','Tạo tài khoản thành công!');
    }
}

controller_guest_login.php使用登录

    <?php

namespace App\Http\Controllers\frontend;

use App\Http\Controllers\Controller;
use Request;
use DB;
use App\Http\Requests\Request_Guest_Login;
use Auth;
use App\Guests;
use App\User;
use Illuminate\Auth\Authenticatable;
use Session;


class controller_guest_login extends Controller
{
    use Authenticatable;

    protected $redirectTo = '/';


     public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
    public function guest_login()

    {
        return view('frontend.shop-cart.login');
    }

    public function post_guest_login(Request_Guest_Login $request){
        $email = Request::get("gl_email");
        $password = Request::get("gl_password");

        if(Auth::guard('guests')->attempt(array('guest_email'=>$email,'password'=>$password)))
            {
                //dang nhap thanh cong, di chuyen den trang user
                 $guests = Auth::user();

                 return redirect(url('/'));
            }
        else
            {
                  //dang nhap khong thanh cong, di chuyen den trang login va hien thi thong bao loi
                  return view("frontend.shop-cart.login",['err'=>'invalid']);

           }
        // $cr_email = DB::table("guest")->where("guest_email","=","$email");
        // $cr_password = DB::table("guest")->where("guest_password","=","$password");
        // if(!empty($cr_email)&&!empty($cr_password))
        // {
        //     return redirect(url('/'));
        // }
        // else
        // {
        //     return redirect(url('/'));
        // }

    }
}

Kernel.php用于配置Auth创建的中间件(其guest =&gt; ...)

  <?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
        \App\Http\Middleware\TrimStrings::class,
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
             \App\Http\Middleware\RedirectIfAuthenticated::class,

        ],

        'api' => [
            'throttle:60,1',
            'bindings',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
        'can' => \Illuminate\Auth\Middleware\Authorize::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
        // 'guests_not_logged' => \App\Http\Middleware\guests_not_logged::class,
        // 'guests_and_users_logged' => \App\Http\Middleware\guests_and_users_logged::class,
    ];
}

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(['middleware'=>'guests_not_logged'],function(){
    Route::get('/','frontend\controller_main@adv_and_products');
//});
Route::get('admin','backend\controller_user@list_user');

//Route::group(['middleware'=>'users_middleware'],function(){
    Route::get('login','backend\controller_login@get_login');
    Route::post("post_login",'backend\controller_login@post_login');
    Route::get('register_account','backend\controller_user@register_account');
    Route::post('post_register_account','backend\controller_user@post_register_account');
//});

Route::get('forgot_password','backend\controller_user@forgot_password');
Route::post('forgot_password','backend\controller_user@post_forgot_account');

//Route::group(['middleware'=>'guests_and_users_logged'],function(){
    Route::get('guest_register_account','frontend\controller_guest@guest_register_account');
    Route::post('guest_register_account','frontend\controller_guest@post_guest_register_account');
    Route::post('guest_register_account_small','frontend\controller_guest@post_guest_register_account_small');
    Route::get('guest_login','frontend\controller_guest_login@guest_login');
    Route::post('guest_login','frontend\controller_guest_login@post_guest_login');
//});

0 个答案:

没有答案