Laravel Fortify logout - laravel

I have a Laravel 8 Fortify system in place. I have a logout link that looks like this:
<a class="dropdown-item" href="#" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>
So when I click this logout link, it does not delete logged in session cookies. When I click the back button it just redirects me back to homepage like it is still logged in. Any ideas?

Use this logout button, I think it will solve your problem:
<a href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
{{ __('Logout') }}
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST" style="display: none;">
#csrf
</form>

Related

Larave 8 jetstream domain/logout not working

When I try to http://domain.test/logout then showing "The GET method is not supported for this route. Supported methods: POST"
But normal logout with post method working perfectly. How can I change the /logout route Post to get.
in Jetstream, fortify
if you list your routes using php artisan route:list you'll see that logout route is defined for POST, so you just need to submit a form to this route in order to fire logout.
POST | logout | Laravel\Fortify\Http\Controllers\AuthenticatedSessionController#destroy | web
Now if you wish to convert that function to respond to GET method, you'll need to change that route in /vendor/laravel/fortify/routes/route.php
I have not tested it in any way, but the method and routes are there.
If still anyone is looking for it you can try this.
In Laravel 8 get method doesn't support logout route you can try post method like below.
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
You can also try this by check in if it's login then it will show you logout otherwise it will show you login .
#if (auth()->id())
<form method="POST" action="{{ route('logout') }}">
#csrf
<div class="nav-item">
<a class="nav-link" href="{{ route('logout') }}" onclick="event.preventDefault();
this.closest('form').submit(); " role="button">
<i class="fas fa-sign-out-alt"></i>
{{ __('Log Out') }}
</a>
</div>
</form>
#else
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}" role="button">
<i class="fas fa-sign-in-alt"></i>
Login
</a>
</li>
#endif
For Anyone Facing This Problem in Laravel Jetstream use this Code
<form method="POST" action="{{ route('logout') }}">
#csrf
<x-jet-dropdown-link href="{{ route('logout') }}" onclick="event.preventDefault(); this.closest('form').submit();">
<i class="fa fa-sign-out"></i>{{ __('Logout') }}
</x-jet-dropdown-link>
</form>
You should put them in the base.blade.php file or the file where the logout form is supposed to be

How are the "Login" and "Register" links from Laravel's built-in authentication system added to existing pages?

I have created several pieces of my Laravel 7 application (I'm a newbie trying to learn). I then wanted to add authentication on top of that, so I did:
php artisan ui bootstrap --auth
npm install && npm run dev
After doing so, I have a "login" and a "register" link on the top left-corner of every page of my app. These links are being added by some automated method and are not changing the pages (views - *.blade.php files) that I had created before adding the authentication system.
I'd like to prevent these links from being added automatically. Then I can add my own links to my navigation bar. The problem is I can't figure out how they're being created or added to my views. Can someone help me?
NEVERMIND - I FIGURED IT OUT.
Appearently during the authentication system scaffolding my "layout.blade.php" was modified to include both the "login" and "register" links by the addition of this code just under the <body> tag:
<div class="flex-center position-ref full-height">
#if (Route::has('login'))
<div class="top-right links">
#auth
Home
#else
Login
#if (Route::has('register'))
Register
#endif
#endauth
</div>
#endif
</div>
All I needed to do is remove that code block. Hope this helps someone in the future.
The login links are created on a separate template file (views/layouts/app.blade.php). To remove them you need to customize this file.
This is the part where the header links are created:
<!-- Right Side Of Navbar -->
<ul class="navbar-nav ml-auto">
<!-- Authentication Links -->
#guest
<li class="nav-item">
<a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>
</li>
#if (Route::has('register'))
<li class="nav-item">
<a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
</li>
#endif
#else
<li class="nav-item dropdown">
<a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<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" style="display: none;">
#csrf
</form>
</div>
</li>
#endguest
</ul>

Dropdown button in nav expand the height of the nav when dropdown button clicked. How to solve?

I am using the Laravel framework and want to use Bootstrap to create a dropdown inside a navbar. I would like the dropdown not to expand the height of the navbar when clicked.
image: https://i.imgur.com/2tryjXl.png
I have seen the dropdown and navbar Bootstrap4 documentation and have searched a lot of posts but still can't solve this problem.
<div id="app">
<nav class="navbar navbar-default navbar-dark bg-dark">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="{{ url('/') }}">
{{ config('app.name', 'Laravel') }}
</a>
</div>
<div>
<ul class="nav navbar-nav justify-content-end">
#if (Auth::guest())
<li class="nav-item">Login</li>
#else
<li>
<div class="dropdown show">
<a href="#" class="btn btn-secondary dropdown-toggle" role="button"
id="dropdownMenuLink" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">
{{ Auth::user()->name }}
</a>
<div class="dropdown-menu" aria-labelledby="dropdownMenuLink">
<a href="{{ route('logout') }}" class="dropdown-item"
onclick="event.preventDefault();document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ route('logout') }}" method="POST"
style="display: none;">
{{ csrf_field() }}
</form>
</div>
</div>
</li>
#endif
</ul>
</div>
</div>
</nav>
#yield('content')
</div>
I would expect that the navbar won't expand due to the open of dropdown in it.
Replace the navbar-default class with navbar-expand
You may also want to add dropdown-menu-right after dropdown-menu to align the dropdown

trying to get property of non-object multi-auth

I use Hesto/multi-auth package. The username when I login success default redirect in customer.layout.auth, how can I to redirect in my blade, Example: welcome.blade.php, I can't use {{ Auth::user()->name }} in another blade, it error Trying to get property of non-object . How to fix it, please help me !
AppServiceProvider.php
public function register()
{
//
if ($this->app->environment() == 'local') {
$this->app->register('Hesto\MultiAuth\MultiAuthServiceProvider');
}
}
auth.blade.php
#if (Auth::guest())
<li>Login</li>
<li>Register</li>
#else
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="{{ url('/customer/logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
<form id="logout-form" action="{{ url('/customer/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>
</ul>
</li>
#endif
I had the same problem and I think I found the solution.
You are creating your routes on the web route file.
You need to create your routes inside user route file created by Hesto/multi-auth package.
I don't know if this is the right way of doing it but it worked for me.

Laravel logging out automatically

I am using Laravel 5.3 and Laravel's default authentication system. After login the page redirect to the welcome page. But if I click on any other link the page redirect to the login page.
I tried using database for session driver, the result is same.
Finally I have found the problem. The application is logging out because I used Auth::logout() in blade.
<li>
<a role="menuitem" tabindex="-1" href="{{ Auth::logout() }}">
<i class="fa fa-power-off"></i> Logout
</a>
</li>
In Laravel 5.3 url(/logout) is no longer working. So, I used Auth::logout() but the problem is logout() method on Auth facade is clearing the authentication information from user's session whenever the page load.
Extra:
To get a logout link in application I am using this method:
<li>
<a href="{{ url('/logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();">
<i class="fa fa-power-off"></i>
Logout
</a>
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
</li>

Resources