问题
After posting a question Lumen + Dingo + JWT is not instantiable while building about Lumen and Dingo here in SO I got a nice detailed answer on how to set up such a system.
Within his setup there is a small authentication example, which uses Eloquent. Now we are loading an custom framework within Lumen, which has its own models etc, and has its own database connection etc.
What I can not seen to figure out is how to completely remove Eloquent, and do the authentication using our own framework.
What I have done so far:
- Removed
$app->withEloquent();
from ourbootstrap\app.php
Other edits I think that need to be done is editing config\auth.php
, or maybe even completely removing this file. I am not really sure.
Lastly, within App\Api\v1\Controllers\AuthController@postLogin
there is made a call to a validate
function. This function needs to communicate with my framework and not via Eloquent. How this is done neatly in Lumen I am also not sure.
Git repo: https://github.com/krisanalfa/lumen-dingo
回答1:
You may read this. So in your case, in App\Api\v1\Controllers\AuthController@postLogin
:
/**
* Handle a login request to the application.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\Response
*/
public function postLogin(Request $request)
{
try {
$this->validate($request, [
'email' => 'required|email|max:255',
'password' => 'required',
]);
} catch (HttpResponseException $e) {
return response()->json([
'message' => 'invalid_auth',
'status_code' => IlluminateResponse::HTTP_BAD_REQUEST,
], IlluminateResponse::HTTP_BAD_REQUEST);
}
$credentials = $this->getCredentials($request);
try {
// Attempt to verify the credentials and create a token for the user
// You may do anything you like here to get user information based on credentials given
if ($user = MyFramework::validate($credentials)) {
$payload = JWTFactory::make($user);
$token = JWTAuth::encode($payload);
} else {
return response()->json([
'message' => 'invalid_auth',
'status_code' => IlluminateResponse::HTTP_BAD_REQUEST,
], IlluminateResponse::HTTP_BAD_REQUEST);
}
} catch (JWTException $e) {
// Something went wrong whilst attempting to encode the token
return response()->json([
'message' => 'could_not_create_token',
], IlluminateResponse::HTTP_INTERNAL_SERVER_ERROR);
}
// All good so return the token
return response()->json([
'message' => 'token_generated',
'token' => $token,
]);
}
来源:https://stackoverflow.com/questions/36283986/lumen-custom-authentication-without-eloquent