'jwt auth doesn't work for other controllers in Laravel?
Laravel web auth was working well. I'm trying to use jwt-auth module. Here's my code with jwt-auth module
---authcontroller.php
public function login(Request $request)
{
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required|string|min:6',
]);
if ($validator->fails()) {
// return response()->json($validator->errors(), 200);
session()->flash('error', json_encode($validator->errors()));
return back()->withInput();
}
if (!$token = auth()->attempt($validator->validated())) {
// return response()->json(['error' => 'Unauthorized'], 200);
session()->flash('error', 'These credentials do not match.');
return back()->withInput();
}
//////// this is test code
echo auth()->check();
//////////////////
$response_token = $this->createNewToken($token);
session()->flash('token', $response_token);
return Redirect::to('/');
}
---web.php
Route::group([
'middleware' => 'api',
'prefix' => 'auth'
],
function ($router) {
Route::post('/login', [AuthController::class, 'login']);
Route::post('/register', [AuthController::class, 'register']);
Route::post('/logout', [AuthController::class, 'logout']);
Route::post('/refresh', [AuthController::class, 'refresh']);
Route::get('/user-profile', [AuthController::class, 'userProfile']);
}
);
---auth.php
'defaults' => [
'guard' => 'api',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'jwt',
'provider' => 'users',
'hash' => false,
],
],
The problem is *test code returns true, but in the redirection controller, auth()->check() returns false. i.e. auth()->check() returns true only when login controller, but returns false for other controllers
What's the problem?
---updates (redirection page code)
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>@yield('title')</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">
<!-- Styles -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" integrity="sha512-1PKOgIY59xJ8Co8+NE6FZ+LOAZKjy+KY8iq0G4B3CyeY6wYHN3yt9PW0XpSriVlkMXe40PTKnXrLnZ9+fkDaog==" crossorigin="anonymous" />
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/gh/alpinejs/[email protected]/dist/alpine.js" defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
@yield('extra-css')
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
</head>
<body>
<header>
<div class="logo">
<a href="/">
<img src="{{ asset('images/blue-logo.png')}}"/>
</a>
</div>
<div class="navigation">
<a class="mr-3" href="{{ route('audition') }}">Audition</a>
<a class="mr-3" href="{{ route('about') }}">About</a>
<a class="mr-3" href="{{ route('playing') }}">Playing</a>
<a class="mr-3" href="{{ route('faq') }}">Faq</a>
@if(!auth('api')->check())
<a class="mr-3" href="{{ route('login') }}">Login</a>
<a class="mr-3" href="{{ route('userType') }}">Register</a>
@else
<a class="mr-3" href="{{ route('support') }}">Support</a>
<a class="mr-3" href="{{ route('dashboard') }}">{{ Auth::user()->name }}</a>
<form class="d-inline-block" method="POST" action="{{ route('logout') }}">
@csrf
<a class="mr-3" href="#" onclick="event.preventDefault(); this.closest('form').submit();">Logout</a>
</form>
@endif
</div>
</header>
@yield('content')
<footer id="footer">
<ul class="icons">
<li><a href="#" class="icon brands fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="#" class="icon brands fa-instagram"><span class="label">Instagram</span></a></li>
<li><a href="#" class="icon brands fa-github"><span class="label">GitHub</span></a></li>
<li><a href="#" class="icon fa-envelope"><span class="label">Email</span></a></li>
</ul>
<ul class="copyright">
<li>© Untitled.</li><li>Credits: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</footer>
@yield('extra-js')
</body>
</html>
Solution 1:[1]
Use auth('api') hence auth('api')->check()
auth() helper function by default returns the authenticated user and stores it in session and cookie for jwt or any other api authenitcation methods use auth('api') which is gonna check the Bearer token and if the user is authenticated or not.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Arman Momeni |
