I want make an API for used on both front-end and admin panel with using Laravel auth structure (with make:auth) and API Authentication (with Passport) for manage all operation of actions.
My "routes/api.php" file;
Route::group(['namespace' => 'Admin', 'prefix' => 'admin', 'as' => 'api.admin.'], function () {
Route::post('login', 'Auth\LoginController@login')->name('login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');
Route::group(['middleware' => 'auth:api'], function () {
//
});
});
My Auth\LoginController file;
<?php
namespace App\Http\Controllers\Admin\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
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 = '/admin';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
}
/**
* Show the application's login form.
*
* @return \Illuminate\Http\Response
*/
public function showLoginForm()
{
return view('admin.auth.login');
}
public function logout(Request $request)
{
auth()->logout();
session()->flash('message', 'Some goodbye message');
return redirect()->route('admin.login');
}
}
When i call "/api/admin/login" url with post method and "email", "password" data, it returns following error;
RuntimeException
Session store not set on request.
I think solved above error with following method in $middlewareGroup in Kernel.php api group; My app/Http/Kernel.php file;
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class
Session store not set on request. error is solved but, it returns MethodNotAllowedHttpException now.
I can solved these problems with other method but i want use auth standart of Laravel.
Is this possible?
Thank you all.
post
request to logout? If you just useRoute::get
for logout, then visiting the logout URL will log you out. – Jeff