I have the same case with laravel custom authentication based on API response call need an answer please as this post has none
1 Answers
First install Laravel passport.
Once done create a custom controller and make an api route to it without any middleware.
public function login(Request $request)
{
$details = $request->only('email', 'password');
if (Auth::attempt($details)) {
$user = Auth::user();
if ($user->verified) {
$user->token = $user->createToken('App')->accessToken;
return response()->json([
'user' => $user
], $this->successful_response_code);
}
return response()->json([
'error' => 'not_verified'
], $this->failure_response_code);
}
return response()->json([
'error' => 'details_incorrect'
], $this->unauthorized_response_code);
}
Remove the user verified check from above (on mobile so harder to type code.)
So now the user posts to that route with username and password and in return will get their user object back with an access token which can be used in any routes you have which are secured with the auth:api
middleware.
Just make requests to the route you want using the users token as a header Authorization: Bearer ******(token)
Here is an example route.
Route::namespace('API\V1')->group(function () {
Route::namespace('Auth')->group(function () {
Route::post('login', 'LoginController@login');
Route::post('register', 'RegisterController@register');
Route::post('verify', 'VerificationController@verify');
});
Route::middleware('auth:api')->group(function () {
Route::namespace('User')->group(function () {
Route::get('users', 'UserController@index');
});
Route::namespace('Conversation')->group(function () {
Route::get('conversations', 'ConversationController@index');
Route::post('conversations', 'ConversationController@store');
Route::patch('conversations/{conversation}', 'ConversationController@update')->middleware('can:update,conversation');
});
});
});
Here is a repo you can browse through https://github.com/michaelmano/laravel-login/
As for the frontend once you have the users token sent back from a request it really depends on what language/framework you are using.

- 3,339
- 2
- 14
- 35
-
i dont have the database. I use guzzle to make the request and get the respone with bearer token – Bryan Lopez Feb 07 '19 at 08:33