【问题标题】:Lumen custom authentication without Eloquent没有 Eloquent 的 Lumen 自定义身份验证
【发布时间】:2016-07-16 23:45:36
【问题描述】:

在 SO 中发布了一个关于 Lumen 和 Dingo 的问题 Lumen + Dingo + JWT is not instantiable while building 后,我得到了一个关于如何设置这样一个系统的很好的详细答案。

在他的设置中有一个使用 Eloquent 的小型身份验证示例。现在我们在 Lumen 中加载一个自定义框架,它有自己的模型等,有自己的数据库连接等。

我无法弄清楚的是如何完全删除 Eloquent,并使用我们自己的框架进行身份验证。

到目前为止我做了什么:

  • 从我们的bootstrap\app.php 中删除了$app->withEloquent();

我认为需要进行的其他编辑是编辑config\auth.php,或者甚至可能完全删除此文件。我不太确定。

最后,在App\Api\v1\Controllers\AuthController@postLogin 中调用了validate 函数。此功能需要与我的框架进行通信,而不是通过 Eloquent。这在 Lumen 中是如何巧妙地完成的,我也不确定。

Git 仓库:https://github.com/krisanalfa/lumen-dingo

【问题讨论】:

    标签: php authentication lumen lumen-5.2


    【解决方案1】:

    您可以阅读this。所以在你的情况下,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,
        ]);
    }
    

    【讨论】:

    • 啊,原来代码中的 JWTAuth::attempt($credentials) 实际上是检查数据库中的用户,我认为它在上面的 try/catch 块中。谢谢,我会尝试一下。
    • 我仍然收到无法连接到数据库的错误。我试图弄清楚这个连接是在哪里与我们框架中定义的数据库不同的数据库建立的。
    • 由于我无权挖掘你的框架,我无法提供任何答案。
    • 你好。我在 repo 上还有一张未公开的票,如果你有时间,也许你可以看看。这是关于使用自定义user provider
    猜你喜欢
    • 1970-01-01
    • 2017-10-18
    • 2020-04-01
    • 2022-12-10
    • 1970-01-01
    • 2018-09-26
    • 2010-12-17
    • 1970-01-01
    • 2012-09-22
    相关资源
    最近更新 更多