【问题标题】:auth::check in laravel 5 not workingauth::check in laravel 5 不工作
【发布时间】:2015-09-06 05:08:14
【问题描述】:

我是 laravel 5 的新手,正在尝试创建一个简单的身份验证系统,用户可以在其中简单地注册和登录,如果用户已登录,它将显示欢迎消息,否则显示注册和登录按钮。但是我正在尝试检查用户是否使用 Auth::check() 登录,尽管我已经使用 Auth::attempt() 成功登录,但它在我的控制器和视图中都给了我错误。我在 SO 和其他网站上进行了搜索,但没有找到可行的解决方案,在某处它说这是 laravel 5 中的一个错误,某处问题只是他们的代码中有问题。我不知道我在这里做错了什么。

login.blade.php

@extends('master')

    @section('content')
        <h1>বাংলার পথে</h1>
        @if(Auth::check())
            <p>স্বাগতম {{ $username }}</p>
        @else
            <a class="tb-button all-around-spacing push-left" href="/login">লগ ইন</a>
            <a class="tb-button all-around-spacing push-left" href="/register">রেজিস্টার</a>
        @endif
    @stop

AuthenticationController.php

public function login() {
    $validator = Validator::make(Input::all(), array(
        'user_email' => 'required|email',
        'user_password' => 'required|alphaNum'
    ));

    $niceNames = array(
        'user_email' => 'ই-মেইল',
        'user_password' => 'পাসওয়ার্ড'
    );

    $validator->setAttributeNames($niceNames);

    if($validator->fails()) {
        return Redirect::to('login')->withErrors($validator);
    } else {
        $remember = Input::get('remember');
        if(Auth::attempt(array('user_email' => Input::get('user_email'), 'password' => Input::get('user_password')), $remember)) {
            return Redirect::to('/');
        } else {
            return Redirect::to('login')->withMessage('আপনার ই-মেইল অথবা পাসওয়ার্ড টি ভূল');
        }
    }
}

public function register() {
    $validator = Validator::make(Input::all(), array(
            'user_name' => 'required|between:6,16',
            'user_email' => 'required|email|unique:users',
            'user_password' => 'required|alphaNum|between:6,16|confirmed',
            'user_password_confirmation' => 'same:user_password'
        )
    );

    $niceNames = array(
        'user_name' => 'ট্রাভেলার নাম',
        'user_email' => 'ই-মেইল',
        'user_password' => 'পাসওয়ার্ড',
        'user_password_confirmation' => 'পাসওয়ার্ড নিশ্চীতকরণ'
    );

    $validator->setAttributeNames($niceNames);

    if($validator->fails()) {
        Input::flash();
        return Redirect::to('register')->withErrors($validator);
    } else {
        DB::transaction(function(){
            $user = new User();
            $user->user_name = Input::get('user_name');
            $user->user_email = Input::get('user_email');
            $user->password = Hash::make(Input::get('user_password'));
            $user->save();
        });

        return Redirect::to('login');
    }
}

HomeController.php

public function index()
{
    if(Auth::check()) {
        $username = Auth::user()->user_name;
        return View::make('home/index')->withUsername($username);
    } else {
        return View::make('home/index');
    }

}

User.php

<?php
/**
 * Created by PhpStorm.
 * User: Shuvro
 * Date: 6/20/15
 * Time: 12:30 AM
 */

namespace App\models;


use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;


class User extends Model implements AuthenticatableContract {
    use Authenticatable;
    protected $table = 'users';
    protected $fillable = ['user_name','user_email','user_password','user_password_confirmation'];
    protected $hidden = ['user_password','remember_token'];
} 

Routes.php

Route::get('/', array('as' => 'index', 'uses' => 'HomeController@index'));

Route::get('/register', array('as' => 'register', 'uses' => 'AuthenticationController@registerView'));
Route::post('/register', array('as' => 'register', 'uses' => 'AuthenticationController@register'));

Route::get('/login', array('as' => 'login', 'uses' => 'AuthenticationController@loginView'));
Route::post('/login', array('as' => 'login', 'uses' => 'AuthenticationController@login'));

创建用户表

   Schema::create('users', function(Blueprint $table) {
        $table->increments('user_id');
        $table->string('user_name');
        $table->string('user_email')->unique('user_email');
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

【问题讨论】:

  • 注册/播种机是什么样子的?
  • 您是通过迁移创建用户表还是手动创建?
  • 在您的刀片文件中,您应该检查身份验证,如下所示:@if(\Illuminate\Support\Facades\Auth::check())
  • @Jazerix 向上迁移功能添加
  • @Salar 它对我不起作用:(

标签: php authentication laravel-5


【解决方案1】:

所以我在登录后访问 http://localhost/home 之前遇到了同样的问题。

似乎我运行了php artisan app:name NameOfApp,它抛出了一个命名空间错误。我将HomeController.php 中的namespace App\Http\Controllers; 更改为namespace NameOfApp\Http\Controllers;,效果很好。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    将您的路由放在中间件组中以访问会话。像这样:

       Route::group(['middleware' => ['web']], function () {
           Route::get('/', function () {
              if(Auth::check) return "Welcome" . Auth::user()->email;
    
          });
    
       });
    

    我希望这会有所帮助...(请原谅我在用户登录时没有使用 view())

    【讨论】:

      猜你喜欢
      • 2020-04-20
      • 1970-01-01
      • 2020-09-05
      • 2019-10-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多