Laravel登录奇怪问题

时间:2017-09-12 09:59:23

标签: laravel laravel-5 login laravel-5.4

您好我是laravel的新手我使用Auth登录表单...当我尝试登录时,我被重定向到主页,但我使用Auth::check()它返回false例如

@if (Auth::check())
                <h1>Welcome you are loggeding</h1>
            @else
                <h1>No you are not loggedin</h1>
            @endif

因此,当我登录时,我看到不,您没有登录,当我打算使用错误的用户名或密码使登录失败时,它确实失败并返回错误消息这些凭据不匹配我的记录。但是当我登录并被重定向到主页而没有看到任何错误消息时这意味着登录正在运行,那么Auth::check()如何返回false并且我看到不,你没有登录

即使Auth::user()返回null ..我尝试了几乎所有内容,即使我重新启动了XAMPP并删除了我的浏览器缓存和Cookie并删除了storage\framework\sessions中的会话文件

PS:登录工作正常,直到我尝试php artisan session:table然后迁移了表并将SESSION_DRIVER更改为session.php内的数据库,甚至会话表实际上并没有将数据保存在其中所以我当我发现登录不再工作时我回滚了所有内容我删除了表并将驱动程序更改为文件

.env文件

APP_NAME=Eshows
APP_ENV=local
APP_KEY=base64:wG+6H+MFxNXa2mTw+UPhCkBIXmL5CMCE11sudm51kyE=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://eshows.dev/
APP_DOMAIN=eshows.dev

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=eshows
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

控制器\验证\ LoginController.php

    <?php

namespace App\Http\Controllers\Auth;

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

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.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /*public function authenticated($request, $user)
    {
        return redirect('/profile/' . $user->username);
    }*/
}

应用\ user.php的

    <?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'username', 'email', 'password', 'phone', 'mobile', 'city', 'country', 'about',
    ];

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

    public function user() 
    {
        return $this->hasMany('App\User');
    }
}

路线\ web.php

    //Authentication routes
Auth::routes();
Route::get('logout', ['as' => 'logout', 'uses' => 'Auth\LoginController@logout']);
Route::get('/','Frontend\HomeController@index');

HTTP \中间件\ RedirectIfAuthenticated.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        switch($guard) {
            case 'admin':
                if(Auth::guard($guard)->check()) {
                    return redirect()->route('admin.dashboard');
                }
            break;

            default:
                if (Auth::guard($guard)->check()) {
                    return redirect('/');
                }
            break;
        }

        return $next($request);
    }
}

查看\ AUTH \ login.blade.php

@extends('frontend.layouts.master')

@section('body-attrs')
    class="page-template-default page page-id-12 contact-us"
@endsection

@section('csrf-meta')
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
@endsection

@section('csrf-script')
    <script>
        window.Laravel = {!! json_encode([
            'csrfToken' => csrf_token(),
        ]) !!};
    </script>
@endsection

@section('title')
Login to your account
@endsection

@section('styles')
    <!-- BEGIN PAGE STYLE -->
    {!! Html::style('assets/common/css/parsley.css') !!}
    <!-- END PAGE STYLE -->
@endsection

@section('content')
    <div class="banner">
        <div class="heading-wrapper"><h1>Login to your account or create new one.</h1></div>
    </div>

    <section class="two-columns">
        <div class="container-fluid narrow">
            <div class="row">               
                <div class="col-sm-6 matchHeight column bg-image">
                    <h2 class="text-center">Login to your account</h2><hr>
                    <h4>Explore big community of talented people or begin branding for your own talent to everyone</h4>

                    <article>
                        {!! Form::open(['route' => 'login', 'role' => 'form', 'data-parsely-validate' => '', 'id' => 'wpcf7-f6-o1', 'class' => 'wpcf7 form-validation']) !!}
                            @if (count($errors))
                                <ul>
                                    @foreach($errors->all() as $error)
                                        <li style="color:red;">{{ $error }}</li>
                                    @endforeach
                                </ul>
                            @endif

                            <div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
                                <p>
                                    <span class="wpcf7-form-control-wrap your-name">
                                        {{ Form::label('username', 'Username', ['class' => 'control-label']) }}
                                        <div class="append-icon">
                                            {!! Form::text('username', old('username'), array('class' => 'form-control form-white', 'id' => 'username', 'placeholder' => 'Enter your username...', 'autofocus' => '', 'required' => '')) !!}

                                            <i class="icon-envelope"></i>

                                            @if ($errors->has('username'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('username') }}</strong>
                                                </span>
                                            @endif
                                        </div>
                                    </span>
                                </p>
                            </div>

                            <div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
                                <p>
                                    <span class="wpcf7-form-control-wrap your-name">
                                        {{ Form::label('password', 'Password', ['class' => 'control-label']) }}
                                        <div class="append-icon">
                                            {!! Form::password('password', array('class' => 'form-control form-white', 'id' => 'password', 'required' => '')) !!}

                                            <i class="icon-lock"></i>

                                            @if ($errors->has('password'))
                                                <span class="help-block">
                                                    <strong>{{ $errors->first('password') }}</strong>
                                                </span>
                                            @endif
                                        </div>

                                        <br>
                                        <input id="remember" type="checkbox" style="float:left;margin:8px 3px 0 0" name="remember" {{ old('remember') ? 'checked' : '' }}> <span style="display:block;padding-top:3px;">Remember me </span>
                                        <br><hr><a href="{{ route('password.request') }}" style="margin:0" class="forgot-password">Forgot your password?</a>
                                    </span>
                                </p>
                            </div>

                            <div class="form-group">
                                <p>
                                    <span class="wpcf7-form-control-wrap your-name">
                                        <button type="submit" style="color:#FFFFFF;margin-top:15px" class="btn btn-rounded btn-white btn-signup hover-effect">Login to my account</button>
                                    </span>
                                </p>
                            </div>
                        {!! Form::close() !!}
                    </article>
                </div>

                <div class="col-sm-6 matchHeight column bg-image">
                    <div class="create-account">
                        <h2 class="text-center">Create an Account</h2>
                        <h3>Don't have an account yet?</h3>
                        <p>You can create your acount quickly to save your wishlist, your address and other usefull info.</p>
                        <div class="text-center m-t-40">
                            <a href="{{ route('register') }}" class="btn btn-rounded btn-white btn-signup hover-effect">Create my Account <i class="fa fa-chevron-right"></i></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
@endsection

@section('scripts')
    <!-- BEGIN PAGE SCRIPTS -->
    {!! Html::script('assets/frontend/plugins/parallax/scripts/jquery.parallax-1.1.3.js') !!}
    {!! Html::script('assets/common/js/parsley.min.js') !!}
    <!-- END PAGE SCRIPTS -->
@endsection

1 个答案:

答案 0 :(得分:0)

我认为它有效

      class LoginController extends Controller{

protected $redirectTo = '/home';
 ......
 public function authenticate()
{
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
}