php - Lumen 自定义身份验证,无需 Eloquent

标签 php authentication lumen lumen-5.2

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

在他的设置中,有一个使用 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

最佳答案

您可以阅读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,
    ]);
}

关于php - Lumen 自定义身份验证,无需 Eloquent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36283986/

相关文章:

authentication - keycloak 删除自动添加的默认角色

php - 在 Lumen 中安装 FFMpeg

php - Laravel Queue Worker 内存占用太大 :/

php - Laravel Lumen - 日志 channel

javascript - PHP 只读取动态创建的输入数组的第一个值

php - 如何提取 Google Analytics 事件中的页面名称

authentication - ASP .NET Core、SignalR (WebSocket) 未与 AuthorizeAttribute 连接

php - 从返回的函数(数据)中删除一个div

php - INSERT 和 UPDATE 语句将错误数据传递到数据库

c++ - Linux中getlogin函数的使用