I am new in laravel 5 and trying to create a simple authentication system where an user can simply registers and logs in, if the user is logged in it would show a welcome message, otherwise the registration and login buttons. But I'm trying to check whether the user is logged in using Auth::check() it's giving me false both in my controller and view though I've successfully logged in using Auth::attempt(). I've searched in SO and other sites, but found no viable solution, somewhere it said it's a bug in laravel 5, somewhere the problem was just something wrong in their code. I don't know what am I doing wrong here.
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'));
CreateUserTable
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();
});
So I was having the same issue until I visited http://localhost/home
, after logging in.
It seems I had run php artisan app:name NameOfApp
and it was throwing a namespace error. I changed namespace App\\Http\\Controllers;
to namespace NameOfApp\\Http\\Controllers;
in HomeController.php
and it worked fine.
Hope this helps.
Put your route's in the middleware group to have access to session. like so:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
if(Auth::check) return "Welcome" . Auth::user()->email;
});
});
I hope this helps... (Forgive me for not using view() for when user log's in)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.