Can't retrieve posts with auth scaffold - laravel

Wether I submit the login, register, or forgot password form I can't retrieve the POST data. While this is my form tag:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
It doesn't end up in /login so I'm guessing it gets redirected back to the current page. How do I retrieve this data anyway?
My goal is to know if a person has used the register/login/or forgot password form wether it failed or was successfull. (They are all on the same page in different modals)
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Blog;
use App\Account;
use Illuminate\Foundation\Auth;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$blogItems = Blog::all();
$onlinePlayers = Account::getOnlinePlayers()->count();
$onlineStaff = Account::getOnlineStaff()->count();
$input = Request('username');
return Auth::routes();
}
}
The login form:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
#if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</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="{{ url('/password/reset') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
My Routes:
<?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!
|
*/
//Auth
Auth::routes();
//Homepage
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController#logout');
LoginController:
<?php
namespace App\Http\Controllers\Auth;
use \Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Auth\Authenticatable;
use Illuminate\Http\Request;
use App\Account;
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']);
}
/**
* Override the username method used to validate login
*
* #return string
*/
public function username()
{
return 'username';
}
}

Related

Why can't I redirect to home after logging in in laravel 8?

I'm having trouble with laravel 8, I'm using the default laravel authentication, but it seems that after I log in I can't redirect to the Home page. I just added this to the LoginController public function username() { return 'username'; }
Here is my table protected $table = 'TO_USER_LOGIN';, I also added the fillables.
my HomeController is fine it just that it won't let me redirect to the Homepage. The protected $redirectTo = RouteServiceProvider::HOME; also has a default value '/home'
LoginView
<div class="container">
<!-- Outer Row -->
<div class="row justify-content-center">
<div class="col-xl-10 col-lg-12 col-md-9 col-sm-12">
<div class="card o-hidden border-0 shadow-lg my-5">
<div class="card-body p-0">
<!-- Nested Row within Card Body -->
<div class="row">
<div class="col-lg-6 d-none d-lg-block bg-login-image"></div>
<div class="col-lg-6">
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">TRAVEL ORDER SYSTEM</h1>
</div>
<form method="POST" class="user" action="{{ route('login') }}">
#csrf
<div class="form-group">
<input id="username" type="text" placeholder="Username" class="form-control form-control-user #error('username') is-invalid #enderror" name="username" value="{{ old('username') }}" required autocomplete="off" autofocus>
#error('username')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<div class="form-group">
<input id="password" type="password" placeholder="Password" class="form-control form-control-user #error('password') is-invalid #enderror" name="password" required autocomplete="off">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>
<hr>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
LoginController
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
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 = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* #return void
*/
public function redirectTo(){
Redirect::route('home');
}
public function username()
{
return 'username';
}
public function __construct()
{
$this->middleware('guest',['except' => ['logout', 'userLogout']]);
}
public function userLogout(){
Auth::guard('web')->logout();
return redirect('/');
}
}
I finally found the problem! It was with my table all along, laravel was looking for a column id which my table doesn't have instead it has an incremented UserID so I changed it to id and it worked! There was no error so basically, I tried thinking as much as I could even tried making a new project just to see if I have a problem with my current one.

Laravel login not working properly after changing the default username and password

I have a custom table for users named 'iw_users' and inside, i have also a custom column for email and password named 'iu_email' and 'iu_password'. What i want to do here is to set my iu_email as the email and iu_password as a password on login, register, and forgot password (basically all functionalities that laravel Auth provides).
I have followed the answer on this thread: Laravel: How can i change the default Auth Password field name
But when i try to login (with correct credentials) the page is just refreshing and redirecting me again to log in page (I can confirm that laravel did not authorize me using Auth::check()).
Also, the color of the error and input are not red whenever i try to login with wrong credentials.
This is my App\User code:
class User extends Authenticatable
{
use Notifiable;
protected $table = 'iw_users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'iu_user_id', 'iu_last_name', 'iu_first_name', 'iu_email', 'iu_mobile_no', 'iu_password', 'iu_gender', 'iu_country', 'iu_role', 'iu_photo', 'iu_status', 'iu_ipaddress'
];
public function getAuthPassword()
{
return $this->iu_password;
}
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
// Commented. i don't have 'password' and 'remember_token' in iw_users
// protected $hidden = [
// 'password', 'remember_token',
// ];
}
This is my LoginController.php
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');
}
public function username()
{
return 'iu_email';
}
}
This is my login.blade.php form
<form class="form-horizontal" 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="iu_email" value="{{ old('iu_email') }}" required autofocus>
#if ($errors->has('iu_email'))
<span class="help-block">
<strong>{{ $errors->first('iu_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>
I would suggest you to check https://scotch.io/#sukelali/how-to-create-multi-table-authentication-in-laravel
Have you got any error while debugging this? I doubt, it's unable to override their default fields.

Prevent login if user isnt approved laravel 5.4

I checked all around for information about how to check on login if user is approved or not and then redirect to logged in or give an error. Now i am little confused because in internet there are a lots of posts and every one is different. So can anyone can help me to deal with this? Also it would be really nice to explain how it works (sintaxes and all other stuff)
User.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 = [
'companyname', 'email', 'password', 'VAT', 'companyphone',
'companystreet', 'companycity', 'companycountry', 'companypostcode'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
LoginController :
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');
}
}
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">
#if($status = Session::get('status'))
<div class ="alert alert-info">
{{$status}}
</div>
#endif
<form class="form-horizontal" 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>
#endsection
Also in my DB is boolean field "activated" default 0
#Karlis Pokkers
Middleware is one option but, I would like to go for little hack provided by Laravel documentation.
You can override Laravel's attemptLogin method.
Add this code to your app > Http > Controllers > Auth > LoginController:
/**
* Attempt to log the user into the application.
*
* #param \Illuminate\Http\Request $request
* #return bool
*/
protected function attemptLogin(Request $request)
{
return Auth::attempt(['username' => $request->username, 'password' => $request->password, 'activated' => 1 ]);
}
No need to write you own LoginController. Use Laravel's default authentication Controllers.
You can check out different sites for that. Answer on Laracast
With laravel its very simple. You have to create a new middleware or extend the app/Http/Middleware/RedirectIfAuthenticated.php middleware.
A good documentaion you can find here: https://laravel.com/docs/5.5/middleware
For example:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::user()->activated) {
return redirect('/home');
}
return $next($request);
}

How do I retrieve POST data after Auth?

Wether I submit the login, register, or forgot password form I can't retrieve the POST data. While this is my form tag:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
It doesn't end up in /login so I'm guessing it gets redirected back to the current page. How do I retrieve this data anyway?
My goal is to know if a person has used the register/login/or forgot password form wether it failed or was successfull. (They are all on the same page in different modals)
EDIT:
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Blog;
use App\Account;
use Illuminate\Foundation\Auth;
use Illuminate\Support\Facades\Input;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
//$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* #return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$blogItems = Blog::all();
$onlinePlayers = Account::getOnlinePlayers()->count();
$onlineStaff = Account::getOnlineStaff()->count();
$input = Request('username');
return Auth::routes();
}
}
The login form:
<form class="form-horizontal" role="form" method="POST" action="{{ url('/login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="text" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
#if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</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="{{ url('/password/reset') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
My Routes:
<?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!
|
*/
//Auth
Auth::routes();
//Homepage
Route::get('/', 'HomeController#index');
Route::get('/home', 'HomeController#index');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController#logout');
LoginController:
<?php
namespace App\Http\Controllers\Auth;
use \Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Auth\Authenticatable;
use Illuminate\Http\Request;
use App\Account;
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']);
}
/**
* Override the username method used to validate login
*
* #return string
*/
public function username()
{
return 'username';
}
}
Definitely you can. You can retrieve the login data via the following command:
If your html is:
<input type="text" name="username"/><br/>
<input type="password" name="password"/><br/>
Then inside the controller action, use the following command:
string username = Request["username"];
string password = Request["password"];

Redirecting after login not working but works fine after registration

I used php artisan make:auth. I am changing the email to username for login.
In login.blade.php i just replaced email with username.
<div class="form-group{{ $errors->has('username') ? ' has-error' : '' }}">
<label for="username" class="col-md-4 control-label">Username</label>
<div class="col-md-6">
<input id="username" type="name" class="form-control" name="username" value="{{ old('username') }}" required autofocus>
#if ($errors->has('username'))
<span class="help-block">
<strong>{{ $errors->first('username') }}</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>
Now, i am not getting error message for incorrect credidentals and not redirected to /home for correct credidentals, the login page just reloads.
I also added this 'username' => $data['username'], in create function in RegisterController and It sucessfully redirects to /home after registration but not while login.
I have also override this property in logincontroller: protected $username = 'username';
Have you override following function?
public function username()
Located in: vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php.
change:
public function username()
{
return 'email';
}
To:
public function username()
{
return 'username';
}
Also make sure you added the username field in the DB.
Your LoginController should look something like this:
<?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;
public function username()
{
return 'username';
}
/**
* Where to redirect users after login / registration.
*
* #var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest', ['except' => 'logout']);
}
}
In Laravel 5.3 you can override the sendLoginResponse() method in LoginController.php, for example:
protected function sendLoginResponse(Request $request)
{
$request->session()->regenerate();
$this->clearLoginAttempts($request);
return redirect('/home');
}

Resources