I'm using multi auth for Laravel ...
This is my lougout function for users
LoginController
public function logout()
{
Auth::guard('web')->logout();
return redirect('/');
}
AdminloginController
public function logout()
{
Auth::guard('web')->logout();
return redirect('/');
}
This is my route
Route::get('/enseignant/logout', 'Auth\LoginController#Elogout')->name('enseignant.logout');
Route::get('/administration/logout', 'Auth\AdminloginController#logout')->name('admin.logout');
All the methods in the view
<a href="{{ route('admin.logout') }}" class="btn btn-default btn-flat"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('admin.logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
The function works fine but when I click the button I get this error :
MethodNotAllowedHttpException in RouteCollection.php line 233
Your route accepts only GET method, but in the form, you specified using POST. I think this is the source of the problem. The error message also indicates that.
It's recommended to use POST for logout, as you did. So, just by changing the route in question to...
Route::post(...);
... should fix the error.
Related
I'm working with Laravel 8 and I wanted to build my own logout method like this:
public function logout(Request $request)
{
$this->guard()->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
if ($response = $this->loggedOut($request)) {
return $response;
}
return $request->wantsJson()
? new JsonResponse([], 204)
: redirect('/');
}
And I have created this link just like the Laravel's default auth system:
<div class="profile-user-loggedin">
<a href="{{ route('logout') }}">
<img src="img/icon-logout.png" style="width: 26px;" alt="" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</div>
And here is the route:
Route::post('/logout', [App\Http\Controllers\Auth\LoginController::class, 'logout'])->name('logout');
Now when I click on the link to logout the user, it properly redirects to the homepage of website but the problem is that the user is still logged in somehow!
I don't know really what's going wrong here since I the route and method looks fine.
So if you know, please help me out with this...
Thanks.
Use the below code in your custom logout function
Auth::logout();
I have this log-out code below working fine. But upon log-out I can't log-in anymore it keeps reloading in log-in just like refreshing the page. Is there a way to trace or track what is going on in the code of log-in?. Or put a breakpoint in VS-Code?.
Log-out
<li class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
<i class="fa fa-sign-out-alt nav-icon"></i>
<p>{{ __('Logout') }} </p>
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
#csrf
</form>
</li>
But when I register and successfully registered it redirects me to my homepage. And then click the log-out causes me again the problem.
Edit
Upon click the log-out here is my network tab.
Upon exploring I ended up here in RedirectIfAuthenticated. I have notice that upon log-in the $guards is empty that is why I can't log-in.
public function handle(Request $request, Closure $next, ...$guards)
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
But When I add this code in my Http > Controllers > Auth > LoginController > __construct()
if (Auth::attempt(array('username' => 'Apples', 'password' => '123456'))) {
return Redirect::intended('dashboard');
}
It redirects me to my homepage fine. But I don't want this approach because it's static.
Note : I am using Laravel 8
I have a problem with the laravel sessions or with crsf tokens of laravel, I dont know.
My laravel installation (v7.x) has a typical login (generated with 1php artisan make:auth1), and my problem is when I need close my session (post request to /logout route), I see in my navigator network a 302 code request, but in my form I have my csrf token and I can see it in the post request.
Sometimes I can "execute" the logout, laravel redirect me to /login route, but in my navigator i put manually the /home route (I need to be athenticated to enter here) and I see that my session is still open :|
Any ideas?
My web.php
Route::get('/', function () {
return redirect('/login');
});
Auth::routes();
Route::middleware(['auth', 'web'])->group(function () {
Route::get('/home', 'AppController#index')
->name('home');
// Orders
Route::get('/order/detail/{id}', 'AppController#show_order_detail')
->name('show_order_detail');
Route::get('/order/list', 'AppController#show_order_list')
->name('show_order_list');
Route::post('/order/update/status', 'AppController#update_order_status')
->name('update_order_status');
Route::post('/order/search_order', 'AppController#search_order')
->name('search_order');
Route::post('/order/filter_orders_by_status', 'AppController#filter_orders_by_status')
->name('filter_orders_by_status');
// Refund
Route::get('/order/refund/{id}', 'AppController#show_refund')
->name('show_refund');
Route::post('/order/send_refund', 'AppController#send_refund')
->name('send_refund');
// Users
Route::get("/administration/users", "AdministrationController#users_list")
->name("users_list");
Route::get("/administration/users/add", "AdministrationController#show_form_add_user")
->name("add_user");
Route::post("/administration/users/add/send", "AdministrationController#add_user_send")
->name("add_user_send");
Route::get("/administration/users/detail/{userId}", "AdministrationController#user_detail")
->name("user_detail");
Route::post("/administration/users/setpassword", "AdministrationController#user_setpassword")
->name("user_setpassword");
// Audit Log
Route::get("/administration/audit", "AdministrationController#show_audit")
->name("show_audit");
});
My logout form (its a link with a javascript event)
<div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">
{{ csrf_field() }}
</form>
</div>
I made a user management system with soft deletion and force deletion options. However, I'm having trouble getting the force deletion option to work.
The route:
Route::post('users/{user}/delete', 'UserController#forcedelete');
The relevant controller code:
public function forcedelete(User $user)
{
$user->forceDelete();
return redirect('users/trash');
}
The view code:
<a href="{{ url('users/'.$user->id.'/delete') }}"
onclick="event.preventDefault(); document.getElementById('delete').submit();">
<i class="fa fa-trash-o btn btn-danger btn-xs"></i>
</a>
<form id="delete" action="{{ url('users/'.$user->id.'/delete') }}"
method="POST" style="display: none;">
{{ csrf_field() }}
{{ method_field('DELETE') }}
</form>
The error that I'm getting is
MethodNotAllowedHttpException in RouteCollection.php line 233:
Why is it not working, and how can I fix it?
Try placing this route above your other user routes or user resource route. Also you're trying to use route model binding with a soft deleted model, which won't work. You need to use the id and delete it manually.
public function forcedelete($id)
{
User::where('id', $id)->forceDelete();
return redirect('users/trash');
}
Edit: Also delete {{ method_field('DELETE') }} from your form, since the route method defined is post.
Methods to remove/restore records from the table. Laravel 5.x, 6.x, 7.x
To enable soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes trait on the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
Soft delete:
This will move record to trash
$user= User::find($id);
$user->delete();
Force Delete: Permanently Deleting Models
$user= User::withTrashed()->find($id);
$user->forceDelete();
Restore: Restoring Soft Deleted Models
$user= User::withTrashed()->find($id);
$user->restore();
How to redirect index.blade to register.blade
index.blade
<a href="{{ url('register') }}">
Click to Channel
</a>
register.blade
{!!Form::text('name')!!}
In the example below you have to use route but the url does not change.
You just change the blade
Use this in index.blade:
<button type="button">Edit/Add Values</button>
Use this in route file
Route::get('/toregister', 'YourController#redirectToRegister');
And this to YourController:
public function redirectToRegister() {
return view('register');
}