middleware conflicts with controller __construct middleware (request validation error not working) - laravel

I am using middleware for user roles.
But when I am using middleware in Controller __construct method, request validation does not work properly. It doesnt show any errors, session error bag returns null. I am not able to see any errors when form submit. But when I have disabled middleware in construct I can see request validation errors.
web.php middleware + controller _construct middleware = request validation doesnt works.
web.php middleware + without _construct middleware = works fine.
without web.php middleware + _construct middleware = works fine.
I showed the details in my codes.
I tried every method for a week but I couldn't solve it.
I look forward to your help sincerely.
web.php
Route::group(['middleware' => ['client.role:paying']], function () {
Route::get('/pay_section', 'HomepageController#showPaySection');
Route::get('/pay_success', 'HomepageController#showPaySuccess');
Route::get('/pay_error', 'HomepageController#showPayError');
Route::post('/pay_section', 'HomepageController#doPaySection');
});
HomepageController (like this my form request validation doesnt works because of middleware)
public function __construct()
{
$this->middleware(function ($request, $next) {
$client = auth()->guard('client');
if ($client->check()){
$request->session()->put('client_id', $client->user()->id);
}else{
$request->session()->put('client_id', -1);
}
$this->_cid = $request->session()->get('client_id'); // client
View::share(['cid' => $this->_cid]);
return $next($request);
});
}
HomepageController (like this my codes works perfect. I am able to see request validation errors there is no problem.)
public function __construct()
{
$this->_cid = 2; // client
View::share(['cid' => $this->_cid]);
}
Middleware ClientRole.php
public function handle($request, Closure $next, ...$roles)
{
$currentRole = array();
$client = auth()->guard('client');
if ($client->check()){
$currentRole[] = 'client';
}else{
$currentRole[] = 'guest';
}
if (session()->has('shop_cart')) {
$currentRole[] = 'shopping';
}
if (session()->has('order')) {
$currentRole[] = 'paying';
}
$currentRole[] = 'paying';
foreach($roles as $role) {
if(in_array($role, $currentRole))
return $next($request);
}
return redirect('/');
}
HomepageController form action
public function doPaySection(CreditcardRequest $request)
{
$validated = $request->validated();
// it doesnt show any errors when form empty. But it should be.
// without middleware it shows error on my view when form empty.
}
View
<div class="messages">
#if ($errors->any())
<div class="row mt-3">
<div class="col-md-12">
<div class="alert alert-warning alert-dismissable" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<h3 class="alert-heading font-size-h4 font-w400">Error!</h3>
#foreach ($errors->all() as $error)
<p class="mb-0">{{ $error }}</p>
#endforeach
</div>
</div>
</div>
#endif
</div>
<form action="{{ action('HomepageController#doPaySection') }}" method="post"
class="needs-validation" novalidate>
#csrf
<div class="row">
<div class="col-md-6 mb-3">
<label for="ccname">Name on card</label>
<input type="text" class="form-control" name="cc_name" id="ccname" placeholder="" value="" required>
<small class="text-muted">Full name as displayed on card</small>
<div class="invalid-feedback">
Name on card is required
</div>
</div>
<div class="col-md-6 mb-3">
<label for="ccnumber">Credit card number</label>
<input type="text" class="form-control" name="cc_number" id="ccnumber" placeholder="" value="" >
<div class="invalid-feedback">
Credit card number is required
</div>
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<label for="ccexp">Expiration</label>
<input type="text" class="form-control" name="cc_exp" id="ccexp" placeholder="" value="1209" required>
<div class="invalid-feedback">
Expiration date required
</div>
</div>
<div class="col-md-3 mb-3">
<label for="cccvv">CVV</label>
<input type="text" class="form-control" name="cc_cvv" id="cccvv" placeholder="" value="333" required>
<div class="invalid-feedback">
Security code required
</div>
</div>
</div>
<hr class="mb-4">
<hr class="mb-4">
<button class="btn btn-primary btn-lg btn-block" type="submit">
<i class="fa fa-check"></i> Submit
</button>
</form>

You may set SESSION_DRIVER=file in you .env file
Then run php artisan config:clear
Seems related

Related

Laravel 8 Form Request Validation Redirect to Index page instead same page and show error

On localhost all is good, but when I deploy the application to the server not working. If form request validation fails instead of bringing me back to the same page and showing an error, it redirects me to the index page.
config.blade.php
<form method="POST" action="{{ route('config.update', $config->id) }}">
#csrf
#method('PUT')
<div class="form-group row">
<div class="col">
<label class="col-form-label">Name</label>
<input id="name" type="text" class="form-control" name="name" value="{{ $config->name }}" required>
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">Address</label>
<input id="address" type="text" class="form-control" name="address" value="{{ $config->address }}">
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">Phone</label>
<input id="phone" type="tel" class="form-control" name="phone" value="{{ $config->phone }}" required>
</div>
</div>
<div class="form-group row mt-3">
<div class="col">
<label class="col-form-label text-md-right">E-mail</label>
<input id="email" type="email" class="form-control" name="email" value="{{ $config->email }}" required>
</div>
</div>
<div class="form-group row mt-4 mb-0">
<div class="col-md-12">
<button type="submit" class="btn btn-primary button-full-width">Save changes</button>
</div>
</div>
</form>
web.php
Route::resource('/admin/config', 'Admin\ConfigController');
ConfigController
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\ConfigServices;
use App\Http\Requests\ConfigRequest;
use App\Models\Config;
class ConfigController extends Controller
{
protected $configServices;
public function __construct(ConfigServices $configServices) {
$this->middleware('auth');
$this->configServices = $configServices;
}
...
public function update(ConfigRequest $request, $id)
{
$config = $this->configServices->updateConfigById($request, $id);
return redirect()->back();
}
...
}
ConfigRequest - here is the problem
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class ConfigRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'name' => 'required|string|max:255',
'address' => 'nullable|string|max:255',
'phone' => 'required|regex:/^([0-9\s\-\+\(\)]*)$/|min:9|max:15',
'email' => 'required|email:rfc',
];
}
}
Form Request return to index page instead same page. On localhost working everything, but when I deploy the app to server a problem arises.
When data on form request validated correct return me back on the same page and show success, but when form request failing redirect mine for some reason to the index page.
A problem arises in Laravel 8, this code worked well in previous Laravel versions.
Can someone help me, please?
In your custom request you need:
/**
* The URI that users should be redirected to if validation fails.
*
* #var string
*/
protected $redirect = '/dashboard';
or
/**
* The route that users should be redirected to if validation fails.
*
* #var string
*/
protected $redirectRoute = 'dashboard';
You can find more in the docs.
In the docs for older versions of Laravel these properties don't exist.
Do you have error parts in your blade?
#if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#if ($message = Session::get('unique'))
asdsad
#endif
#endforeach
</ul>
</div>
#endif

Axios returning HTML in response instead of data

I want to ask for help regarding Axios' response to my POST request in the login route via Laravel. It returns HTML of the homepage in data when it authenticated the user (or it finds the user in the database). Please see the response screenshot below:
May I just ask for the solution to my problem? Here are the necessary codes and the versions that I am using:
Tools
VueJS: v3.0.5
Axios: 0.19
Laravel: 5.8
Codes
Login.vue (script)
import axios from 'axios'
export default {
data() {
return {
csrfToken: '',
loginObj: {
username: '',
password: '',
remember: false,
},
message: '',
}
},
created() {
this.csrfToken = document.querySelector('meta[name="csrf-token"]').content;
},
methods: {
signIn() {
console.log(this.loginObj)
axios.post('login', this.loginObj).then(res => {
console.log(res)
})
.catch(error => {
console.log(error.response)
})
}
}
}
Login.vue (template)
<div class="card shadow-lg border-0 rounded-lg mt-5">
<div class="card-header bg-orange"><h3 class="text-center my-4">Hello!</h3></div>
<div class="card-body p-0">
<div class="row">
<div class="col-lg-6 d-none d-lg-block">
<div class="login-image-wrap text-center">
<img src="/img/ahrs_logo_trans.png" class="login-img" alt="AKB Logo">
</div>
</div>
<div class="col-lg-6">
<div class="p-5">
<div class="text-center">
<h1 class="h4 text-gray-900 mb-4">Sign In</h1>
</div>
<form name="loginForm" #submit.prevent="signIn">
<input type="hidden" name="_token" :value="csrfToken">
<div class="form-group">
<label class="small mb-1" for="inputUsername">Username</label>
<input class="form-control py-4" v-model="loginObj.username" id="inputUsername" type="text" placeholder="Enter username" required autofocus>
</div>
<div class="form-group">
<label class="small mb-1" for="inputPassword">Password</label>
<input class="form-control py-4" v-model="loginObj.password" id="inputPassword" type="password" placeholder="Enter password" required>
</div>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" v-model="loginObj.remember">
<label class="form-check-label text-small" for="remember">
Remember Me
</label>
</div>
</div>
<div class="form-group d-flex align-items-center justify-content-between mt-4 mb-0">
<a class="small" href="password.html">Forgot Password?</a>
</div>
<hr />
<div>
<button type="submit" class="btn bg-yellow btn-block font-weight-bold">LOGIN</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
LoginController.php
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 in the AuthenticatesUsers function username()
public function username()
{
return 'username';
}
Thank you for your help.
You will probably need to tell Laravel that you want to return the User as JSON if it's an ajax request.
You can do this by adding the following to your LoginController:
protected function authenticated(Request $request, $user)
{
if ($request->ajax()) {
return $user;
}
}
You may also need to set the X-Requested-With header that is usually set in the default bootstrap.js file that comes with Laravel so that Laravel knows it's an ajax request:
axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
Obviously, if this line is already there and you're still including the bootstrap file in your bundle you can ignore this.

Laravel 5.8 authentication

Hi i've a problem with laravel authentication system (5.8) The problem is that I need to login two times to enter my site
My routes file is
Route::get('/', 'RefundsController#index');
Route::get('/polizza', 'RefundsController#indexRefunds')->name('polizza');
Auth::routes();
--------------other routes------------------
RefundsController
public function __construct()
{
$this->middleware('auth');
}
public function index(){
return view('auth.login');
}
public function indexRefunds(Request $request){
DB::enableQueryLog();
$grafici = 1;
$getAverageLiquidati = DB::table('refunds')
->select(DB::raw("AVG(DATEDIFF(date_liq, date_ref)) AS avgliq"))
->where([
['disactive','=', 1],
['date_liq','<>','0000-00-00'],
['status_ref','>', 5]
])
->get();
$getAverageRifiutati = DB::table('refunds')
->select(DB::raw("AVG(DATEDIFF(date_status, date_ref)) AS avgrif"))
->where(function($q) {
$q->where('status_ref','=', 2)
->orWhere('status_ref','=', 3)
->orWhere('status_ref','=', 4);
})
->where([
['disactive','=', 1],
['date_liq','<>','0000-00-00'],
])
->get();
//dd(DB::getQueryLog());
//dd($getAverageRifiutati);
return view('pages.modify', compact('grafici','getAverageLiquidati','getAverageRifiutati'));
}
login blade
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">{{ __('Login') }}</div>
<div class="card-body">
<form method="POST" action="{{ route('login') }}">
#csrf
<div class="form-group row">
<label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
<div class="col-md-6">
<input id="email" type="email" class="form-control #error('email') is-invalid #enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus>
#error('email')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<div class="form-group row">
<label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
<div class="col-md-6">
<input id="password" type="password" class="form-control #error('password') is-invalid #enderror" name="password" required autocomplete="current-password">
#error('password')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
#enderror
</div>
</div>
<!--<div class="form-group row">
<div class="col-md-6 offset-md-4">
<div class="form-check">
<input class="form-check-input" type="checkbox" name="remember" id="remember" {{-- old('remember') ? 'checked' : '' --}}>
<label class="form-check-label" for="remember">
{{-- __('Remember Me') --}}
</label>
</div>
</div>
</div>-->
<div class="form-group row mb-0">
<div class="col-md-8 offset-md-4">
<button type="submit" class="btn btn-primary">
{{ __('Login') }}
</button>
{{--#if (Route::has('password.request'))--}}
<!--<a class="btn btn-link" href="{{-- route('password.request') --}}">
{{-- __('Forgot Your Password?') --}}
</a>-->
{{--#endif--}}
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
#endsection
my LoginController
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 = '/polizza';
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
}
In my Middleware
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)
{
if (Auth::guard($guard)->check()) {
return redirect('/polizza');
}
return $next($request);
}
}
When i connect to https://www.example.com/refunds/ because of definition in RefundsController it takes me to https://www.example.com/refunds/login but when i insert credentials it takes me to https://www.example.com/refunds/ with again login form then when i insert credentials it finally takes me to https://www.example.com/refunds/polizza
I dont understand why :(
The first redirect happens because in your Controller constructor, you are setting the middleware to auth. Hence, all unauthorized requests to any methods in that Controller will be redirected to default log in page. (https://www.example.com/refunds/ redirects to https://www.example.com/refunds/login)
When you enter your credentials there, Laravel takes you to your intended route (the route you tried to access without being authenticated, and that is https://www.example.com/refunds/).
This time you are authenticated, so in your controller, your index method is set to return log in view, so the view is being returned and rendered, and the form is being shown for the second time now. Now, that you log in for the second time, the Log In controller will redirect you to https://www.example.com/refunds/polizza, as there intended route does not exists, and it uses the default route which is correctly set to /polizza.
How to resolve this issue?
In your controller's constructor, change the line with:
$this->middleware('auth')->except(['index']);
This way, you will exclude the index function from the auth middleware and it can be accessible to the public. The request should no longer redirect you to the default log in page. Now, going to https://www.example.com/refunds/ will just render a log in form, as you specified in your controller. When you log in with that form, it will take you to /polizza route.

Edit function not working in CRUD form (Laravel)

I've created a CRUD form called 'Groups'. The Create and Delete functions work, but the 'Edit' throws up an error:
"Undefined variable: group (View:
C:\wamp64\www\sites\jointpromote2\resources\views\groups\edit.blade.php
The error points to this line of code:
<input type="text" class="form-control" name="group_name" value="<?php echo e($group->group_name); ?>" />
I've tried several things but nothing fixes the issue.
Here is my http/controllers/GroupController.php
{
$event = Group::find($id);
return view('groups.edit', compact('group'));
}
Here's my database/migrations/create_groups_table.php
public function down()
{
Schema::dropIfExists('groups');
}
And here's the edit.blade.php
#section('main')
<div class="row">
<div class="col-sm-8 offset-sm-2">
<h1 class="display-3">Update a Group</h1>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
<br />
#endif
<form method="post" action="{{ route('groups.update', '$group->id') }}">
#method('PATCH')
#csrf
<div class="form-group">
<label for="group_name">Group Name:</label>
<input type="text" class="form-control" name="group_name" value="{{ $group->group_name }}" />
</div>
<div class="form-group">
<label for="group_description">Group Description:</label>
<input type="text" class="form-control" name="group_description" value="{{ $group->group_description }}" />
</div>
<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
#endsection
You are using "compact" method in wrong way.
https://www.php.net/manual/en/function.compact.php
try with this
{
$event = Group::find($id);
return view('groups.edit', compact($event));
}
You're passing wrong variable from your GroupController to your edit.blade.php view. Change your GroupController to this:
{
$group= Group::find($id);
return view('groups.edit', compact('group'));
}
All should be working now.
Your variable name is event but you are compacting group and using group variable in blade. Change the variable name in controller and everything will be fine.
public function edit($id) {
$group = Group::find($id);
return view('groups.edit', compact('group'));
}
You write action is in the wrong way. Try copy and paste below code
<form method="post" action="{{ route('groups.update',
$group->id) }}">
You have to remove '.' from $group->id

Laravel Update Function Not Running Correct Response

When I attempt to update a record in my Laravel application, it is running the wrong URL causing an error 404. This function was working fine when I was developing locally however now it is hosted on a one.com server, it has stopped working.
edit.blade.php
<form method="POST" action="gins/{{ $gins->id }}">
#method('PATCH')
#csrf
<div class="field">
<label class="label" for="gin">Gin</label>
<div class="control">
<input type="text" class="input" name="gin"
placeholder="Gin" value="{{ $gins->gin }}">
</div>
</div>
<div class="field">
<label class="label" for="size">Bottle Size(ml)</label>
<div class="control">
<input type="text" class="input" name="size"
placeholder="Size (ml)" value="{{ $gins->size }}">
</div>
</div>
<div class="field">
<label class="label" for="price">Price(£)</label>
<div class="control">
<input type="text" class="input" name="price"
placeholder="Price of Gin" value="{{ $gins->price }}">
</div>
</div>
<div class="field">
<div class="control">
<button type="submit" class="button is-success">Update Record
</button>
</div>
</div>
</form>
Route
Route::patch('gins/{gin}', 'PostsController#update')->middleware('auth');
Auth::routes();
Controller
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
return redirect('gins');
}
The URL for the edit page is Laravel/gins/7/edit. When I click the submit button it's returning the URL Laravel/gins/7/gins/7 when it should be redirecting back to Laravel/gins/7.
The 7 in the Url is the record id from the particular record I'm attempting to update.
It's always a bad idea to hardcode urls like that. The following
<form method="POST" action="gins/{{ $gins->id }}">
in a route like laravel/gins/ would evaluate to laravel/gins/gins/7.
Also, routes change all the time in a dynamic web application. For this reason, I'd suggest you to use Named Routes.
For example:
Route::patch('gins/{gin}', 'PostsController#update')
->middleware('auth')
->name('posts.update');
and then change your form action to this:
<form method="POST" action="{{ route('posts.update', ['gin' => $gins->id]) }}">
I would also clean up your update() method a bit.
public function update(Request $request, $id)
{
$gins = \App\Gins::findOrFail($id);
$gins->gin = request('gin');
$gins->size = request('size');
$gins->price = request('price');
$gins->save();
// change this to a named route as well
return redirect('gins');
// or if you just want to return back to the previous page, you can do
// return back();
}

Resources