doutong2132 2016-03-29 12:14
浏览 67
已采纳

没有Eloquent的流明自定义身份验证

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条回答 默认 最新

  • doufan9290 2016-03-29 13:27
    关注

    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,
        ]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码