Lumen custom authentication without Eloquent

最后都变了- 提交于 2019-12-11 10:14:21

问题


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 our bootstrap\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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!