I want to make a authentication user login and register in laravel. But when I submit to save the register info..It's showing
Object not found!
The requested URL was not found on this server. The link on the
referring page seems to be wrong or outdated. Please inform the author
of that page about the error.
If you think this is a server error, please contact the webmaster.
Here is my authController:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Where to redirect users after login / registration.
*
* #var string
*/
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
protected function getLogin() {
return View('auth.login');
}
protected function postLogin(LoginRequest $request) {
if ($this->auth->attempt($request->only('email', 'password'))) {
return redirect()->route('/');
// return view('course.index');
}
return redirect('auth/login')->withErrors([
'email' => 'The email or the password is invalid. Please try again.',
]);
}
/* Register get post methods */
protected function getRegister() {
return View('auth.register');
}
protected function postRegister(RegisterRequest $request) {
$this->user->name = $request->name;
$this->user->email = $request->email;
$this->user->password = bcrypt($request->password);
$this->user->save();
return redirect('auth.login');
}
protected function getLogout()
{
$this->auth->logout();
return redirect('auth.login');
}
protected $redirectTo = '/course';
protected $loginPath = '/auth/login';
}
Here is my login.blade.php file:
<form method="POST" action="/auth/login">
{!! csrf_field() !!}
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password" id="password">
</div>
<div>
<input type="checkbox" name="remember"> Remember Me
</div>
<div>
<button type="submit">Login</button>
</div>
</form>
Here is my register.blade.php file
<form method="POST" action="/auth/register">
{!! csrf_field() !!}
<div>
Name
<input type="text" name="name" value="{{ old('name') }}">
</div>
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password">
</div>
<div>
Confirm Password
<input type="password" name="password_confirmation">
</div>
<div>
<button type="submit">Register</button>
</div>
</form>
And Here is the routes.php
<?php
/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('/all_user',function(){
return view('all_user');
});
Route::get('all_user/{id}/{name}',function($id,$name){ // Here we pass the peremeter in url all_user
return 'User '.$id." ".$name; // with the parameter id and name
});
Route::get('home','basicController#index'); // Here Home is the URL and it
//execute the basiccontroller index page
Route::get('about','basicController#about');
Route::resource('course','courseController');
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/
Route::group(['middleware' => ['web']], function () {
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
});
In your view
For login :
{{ !! Form::open(array('route'=>route('auth.login'))) !!}}
For Register :
{{ !! Form::open(array('route'=>route('auth.register'))) !! }}
In you Routes
For login
Route::post('auth/login', array('as'=>'auth.login','uses'=>'Auth\AuthController#postLogin'));
For Register
Route::post('auth/register', array('as'=>'auth.register','uses'=>'Auth\AuthController#postRegister'));
And don't forgot to close your form as :
{{ !! Form::close() !! }}
Related
I am trying to work on a Laravel PHP project and as I am new to this framework. First step I had to do is build a Registration Form. However, when I click on the Submit button no error is given, and nothing is registered in my users table.
Here is the code for my project so far :
My users migration table up and down functions
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->boolean('sexe');
$table->integer('age');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
I added to the original two fields which are : "sexe a boolean F/M" and age
My RegisterController important functions
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Mail;
class RegisterController extends Controller
{
use RegistersUsers;
protected $redirectTo = '/register';
public function __construct()
{
$this->middleware('guest');
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required', 'string', 'max:255',
'sexe'=> 'required|in:male,female',
'age' => 'required|integer|max:100',
'email' => 'required', 'string', 'email', 'max:255', 'unique:users',
'password' => 'required', 'string', 'min:5', 'confirmed',
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'sexe' => $data['sexe'],
'age' => $data['age'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
]);
}
/**
* Override default register method from RegistersUsers trait
*
* #param array $request
* #return redirect to $redirectTo
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
//add activation_key to the $request array
$activation_key = $this->getToken();
$request->request->add(['activation_key' => $activation_key]);
$user = $this->create($request->all());
//$this->guard()->login($user);
//write a code for send email to a user with activation link
$data = array('name' => $request['name'], 'email' => $request['email'], 'activation_link' => url('/activation/' . $activation_key));
Mail::send('emails.mail', $data, function($message) use ($data) {
$message->to($data['email'])
->subject('Activate Your Account');
$message->from('s.sajid#artisansweb.net');
});
return $this->registered($request, $user)
?: redirect($this->redirectPath())->with('success', 'We have sent an activation link on your email id. Please verify your account.');
print_r($request->input());
}
}
My Routes
Route::auth();
Route::get('/home', 'HomeController#index');
Auth::routes();
Route::get('/register', 'RegisterController#create');
Route::post('/register', 'RegisterController#register');
Route::get('/', function () {
return view('welcome');
});
My User.php Model fillable
protected $fillable = [
'name','sexe','age','email','password',
];
protected $hidden = [
'password', 'remember_token',
];
public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}
}
My blade file register part (register.blade.php)
<body>
<form method="POST" role="form" action="//IJJI/resources/views/chat.blade.php">
<meta name="csrf-token" content="{{ csrf_token() }}">
<input id="name" name="name"type="text" class="form-control" placeholder="Entrez ici votre Pseudo *" value="" />
<label class="radio inline">
<input id="homme" type="radio" name="sexe" value="homme" checked>
<span> Homme </span>
</label>
<label class="radio inline">
<input id="femme" type="radio" name="sexe" value="femme">
<span>Femme </span>
</label>
<input id="age" name="age" type="integer" class="form-control" placeholder="Saisissez votre age *" value="" />
<input id="Email" name="email" type="email" class="form-control" placeholder="Saisissez votre Email *" value="" />
<input id="password" name="password" type="password" class="form-control" placeholder="Entrez votre Mot de Passe *" value="" />
<input id="confirmpassword" name="confirmpassword" type="password" class="form-control" placeholder="Confrimez votre Mot de Passe *" value="" />
<button type="submit" class="btnRegister">
Je deviens membre Gratuitement
</button>
</form>
</body>
I have done PHP artisan make auth generated the files, made .env file adequate to my MySQL database with the username and password, even checked the PhpMyAdmin configuration, but all in vain.
After 4 days of search in Google websites I can't figure out where I am wrong.
P.S : Another thing that could be wrong is that code like this :
#section
#endsection
never gets accepted and just shows like normal text on my browser.
Thanks a lot for your help
Check your laravel logs location: storage/logs you will get errors.
i have notice you are using $table->boolean('sexe') and in validation you are giving string boolen should be 0/1
'sexe'=> 'required:in:true,false',
also change in your html form to 0,1 currently you are using male, female
Are you getting error?
Besides, can you please the following line at the top of your form to see if there is any validation error or not. After that try submitting the form and see if there is any error or not!
#if(count($errors) > 0)
<div style="color:red">
#foreach ($errors->all() as $message)
<ul>
<li>{{$message}}</li>
</ul>
#endforeach
</div>
#endif
And remove the action form the form tags.
Use:
#csrf
or
{{csrf_field()}}
instead of
<meta name="csrf-token" content="{{ csrf_token() }}">
I am newbie to laravel, I have several doubts here to ask.
It's like when I do basic login and registeration using Auth Class which is by default provided by Laravel 5.1, it gives me 404 Not found error.
Here is my directory structure:
resources/views/auth/register.blade.php
resources/views/auth/login.blade.php
I am following laravel 5.1 official doc, to get this.
When I press for submit button in register form it throws me 404 not found error.
register.blade.php
<!-- resources/views/auth/register.blade.php -->
<form method="POST" action="/auth/register">
{!! csrf_field() !!}
<div>
Name
<input type="text" name="name" value="{{ old('name') }}">
</div>
<div>
Email
<input type="email" name="email" value="{{ old('email') }}">
</div>
<div>
Password
<input type="password" name="password">
</div>
<div>
Confirm Password
<input type="password" name="password_confirmation">
</div>
<div>
<button type="submit">Register</button>
</div>
</form>
I have my auth class as a :
app/Http/Controllers/Auth/AuthController.php
app/Http/Controllers/Auth/PasswordController.php
My basic routes:
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController#getLogin');
Route::post('auth/login', 'Auth\AuthController#postLogin');
Route::get('auth/logout', 'Auth\AuthController#getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::controllers([
'password' => 'Auth\PasswordController',
]);
Still it throws me 404 not found error. Any Solution What I am missing?
Also the routes shows AuthController#getRegister, do I have to manually create the function of getRegister, as I couldn't find any.?
I am new to laravel
Also my AuthController.php looks like
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Validator;
use App\Http\Requests;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectPath = '/dashboard';
protected $loginPath = '/login';
/**
* Create a new authentication controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
I am not sure, but I guess the problem is coming from your action attribute in form. Never use plain url in form action in laravel.
Try to use
<form method="POST" action="{{ url('/auth/login') }}">
You can create also your forms in this way:(You have to add the dependency in your composer.json, search in google about laravel forms)
{!! Form::open(array('url' => 'http://other.domain.com')) !!}
<input type="text" name="test" value="something" />
<button type="submit">Submit</button>
{!! Form::close() !!}
Im trying to create login form and its controller but when i try to login it doesnt work can any one help me i'm very new in laravel.
here is my form
<form action="/login" method="post">
{{ csrf_field() }}
<div class="form-group has-feedback">
<input type="email" name="email" class="form-control" placeholder="Email">
<span class="glyphicon glyphicon-envelope form-control-feedback"></span>
</div>
<div class="form-group has-feedback">
<input type="password" name="password" class="form-control" placeholder="Password">
<span class="glyphicon glyphicon-lock form-control-feedback"></span>
</div>
<div class="row">
<div class="col-xs-7">
<div class="checkbox">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-xs-5">
<button type="submit" class="btn btn-primary btn-raised btn-block ">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
and here is my route
Route::get('/login', 'loginController#create');
Route::post('/login', 'loginController#store');
and my loginController is
class loginController extends Controller
{
public function __construct(){
$this->middleware('guest', ['except' => 'destroy']);
}
public function create(){
return view('pages.admin.login');
}
public function store(){
if(! auth()->attempt(request(['email', 'password']))){
return back()->withErrors([
'message' => 'Please check your credentials'
]);
}
return redirect('/home');
}
}
My user modal is
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'fname','oname','lname', 'email', 'phone','password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
Please where i'm i doing wrong on these, when i entered email and password as my credential it just refresh and back to login page
auth()->attempt() requires an array, you're sending the return value of request() as a single argument, may that be it?
Your function should be like this with Request facade.
public function store(Request $request){
if(! auth()->attempt($request->only(['email', 'password']))){
return back()->withErrors([
'message' => 'Please check your credentials'
]);
}
return redirect('/home');
}
Here is how your attempt function should be like:
if(Auth::attempt( ['email'=> $request['email'],'password' => $request['password'] ])
Add these on top of LoginController
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
and modify your store method
public function store(Request $request){
$email = $request->email;
$password = $request->password;
if (! Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication Failed...
return back()->withErrors([
'message' => 'Please check your credentials'
]);
}
return redirect('/home');
}
Also remove password from $hidden in your User Model.
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'remember_token',
];
Hope it's helpful.
You can use php artisan make:auth and laravel will produce everything needed for a login including the controller and then you can go into your resource and edit to make it look how you imagined.
This is what my login controller looks like after using php artisan make:auth
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
The reason I would like to get rid of bcrypt(for the moment,while developing) is because I'm creating an admin android app and I'm connecting it to the database .I have been having issues when I create a user from the app ,they cannot login into the webiste.The bcrypt came with the Auth installation.
[![enter image description here][1]][1]
I have figured out that the issued that is causing this problem is bcrypt .I can't seemed to find a way to remove the .
I want to remove the website requiring the user to have their password being bcrypted
I have tried in RegisterController 'password' => bcrypt($data['password']) the bcrypt but that has not worked
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
This 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 = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
This is my login.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="col-md-4 control-label">E-Mail Address</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control" name="email" value="{{ old('email') }}" required autofocus>
#if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="col-md-4 control-label">Password</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control" name="password" required>
#if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
#endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
Web.php
<?php
//use Illuminate\Http\Request;
/*
|--------------------------------------------------------------------------
| 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::get('/', function () {
return view('welcome');
});
Route::get('/test', function () {
return Auth::user()->test();
});
Auth::routes();
Route::any('/home', 'HomeController#index')->name('home');
Route::group(['as' => 'user.'], function () {
Route::get('/front', function () {
return view('front');
});
Route::get('/settings', ['as' => 'settings', 'uses' => 'ProfileController#viewSettings']);
Route::post('/settings', ['as' => 'settings', 'uses' => 'ProfileController#saveSettings']);
Route::any('/profile/{userId}', ['as' => 'profile', 'uses' => 'ProfileController#viewProfile']);
Route::get('/search/{query?}', ['as' => 'search', 'uses' => 'SearchController#search']);
Route::get('users', function () {
return User::find(1)->toJson();
});
Route::get('/chat', function () {
return view('chat');
});
Route::get('/calendar', function () {
return view('calendar');
});
Route::resource('events', 'EventsController', ['only' => ['index', 'store', 'update', 'destroy']]);
//Friends route
Route::post('/friends/request', ['as' => 'friends', 'uses' => 'FriendsController#sendRequest']);
Route::get('/friends/viewReq', ['as' => 'friends', 'uses' => 'FriendsController#viewFriendReq']);
Route::post('/friends/reqAction', ['as' => 'friends', 'uses' => 'FriendsController#requestAction']);
Route::get('/status-delete/{status_id}',['uses' => 'HomeController#getDeleteStatus', 'as'=> 'status.delete',
'middleware' =>'auth'
]);
Route::get('/edit/{status_id}', 'HomeController#edit');
});
[1]: https://i.stack.imgur.com/BFMap.jpg
You may hash a password by calling the make method on the Hash facade. In your RegisterController just encrypt your password as
'password' => Hash:make($data['password'])
but make sure that, you must include this to use Hash Facade
use Illuminate\Support\Facades\Hash;
After that you can use check method which allows you to verify that a given plain-text string corresponds to a given hash as
if (Hash::check($data['password'], Hash::make($data['password']))) {
// The passwords match...
}
I have created a new Laravel 5.2 installation. And I have run the following command to install the default Laravel authentication;
php artisan make:auth
The registration form works but the login just redirects home without logging in. And displays no errors when when I enter the wrong credentials.
This is my routes file:
Route::get('/', 'BaseController#index');
Route::get('/tutors', 'TutorsController#Index');
Route::get('/tutors/myrequest', 'TutorsController#RequestTutor');
Route::get('/tutors/{id}', 'TutorsController#show')->where(['id' => '[0-9]+']);
Route::get('/tutors/{GUID}', 'TutorsController#TutorByGUID')->where(['id' => '[A-Za-z]+[0-9]+']);
/********************Authentication routes**********************************/
Route::group(['middleware' => ['web']], function () {
Route::auth();
});
This is code from the AuthController:
class AuthController extends Controller
{
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
protected $redirectTo = '/';
public function __construct()
{
$this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}
/**
* Get a validator for an incoming registration request.
*
* #param array $data
* #return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'firstname' => 'required|max:255',
'lastname' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
]);
}
/**
* Create a new user instance after a valid registration.
*
* #param array $data
* #return User
*/
protected function create(array $data)
{
return User::create([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
This is the BaseController which contains the home method;
<?php namespace App\Http\Controllers;
use App\Services\InterfaceService;
use App\Repositories\TutorRepository;
use App\Http\Requests;
use Illuminate\Http\Request;
class BaseController extends Controller {
protected $TutorRepository;
protected $InterfaceService;
public function __construct(InterfaceService $InterfaceService, TutorRepository $TutorRepository)
{
//$this->middleware('guest');
$this->InterfaceService = $InterfaceService;
$this->TutorRepository = $TutorRepository;
}
public function index()
{
$tutors = $this->TutorRepository->all();
$page_info = \Config::get('constants.App.Pages.home');
$this->InterfaceService->SetPageInfo($page_info);
return view('home', ['TopTutors'=>$tutors]);
}
} ?>
This is code from the login view.
<form role="form" method="POST" action="{{ url('/login') }}" id="login_form">
{!! csrf_field() !!}
<div class="mj_login_form">
<div class="form-group">
<input type="text" placeholder="Email" id="email" name="email" class="form-control" value="{{ old('email') }}">
#if ($errors->has('email'))
<span class="help-block"><strong>{{ $errors->first('email') }}</strong></span>
#endif
</div>
<div class="form-group">
<input type="password" placeholder="Your Password" id="password" class="form-control" name="password">
#if ($errors->has('password'))
<span class="help-block"><strong>{{ $errors->first('password') }}</strong></span>
#endif
</div>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
<div class="form-group pull-left">
<div class="mj_checkbox">
<input type="checkbox" value="1" id="check2" name="remember">
<label for="check2"></label>
</div>
<span> remember me</span>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 mj_toppadder20">
<div class="form-group pull-right">
<span>forget password ?</span>
</div>
</div>
</div>
</div>
<div class="mj_pricing_footer">
login Now
</div>
</form>
That happens because your '/' route is not under the web middleware group and you're not using the auth middleware for the routes you want to be authenticated
This means that when accessing that route, session is not available, and the auth middleware doesn't fire, so authentication doesn't work.
Try to put all the routes in which you want to use session under the web middleware group, and for the routes in which you want authentication use the auth middleware:
Route::group( ['middleware' => ['web'] ], function () {
/* these routes use 'auth' middleware, so only an authenticated user will access*/
Route::group( ['middleware' => 'auth' ], function () {
Route::get('/', 'BaseController#index');
});
Route::auth();
});
A Note on Laravel version:
Keep in mind that if you're using Laravel version >= 5.2.27 the middleware web is used by default on every route defined in routes.php as you can see in app/Providers/RouteServiceProvider.php:
protected function mapWebRoutes(Router $router)
{
$router->group([
'namespace' => $this->namespace, 'middleware' => 'web'
], function ($router) {
require app_path('Http/routes.php');
});
}
In that case you can remove you statement for using the web middleware group, but you'll only need to use auth middleware for the authenticated routes