流明的基本认证

时间:2017-06-01 07:33:06

标签: authentication lumen

我已经使用Laravel编写了一个用于检查身份验证的API调用。我需要将该控制器移动到Lumen,以便将其用作微服务。

这是我在Laravel的控制器。

public function byCredantial(Request $request)
{
    $user = [
        'email' => $request->input('email'),
        'password' => $request->input('password')
    ];

    if (Auth::attempt($user)) {
        $response = $this->getSuccess(Auth::user()->id);

        return response()->json($response, 200);
    } else {
        $response = $this->getError($user);

        return response()->json($response, 401);
    }
}

Lumen doc没有提供如何进行此类身份验证。他们没有检查creadential的功能是正确的。我怎么能在流明做到这一点。这可能吗?

1 个答案:

答案 0 :(得分:0)

你可以在流明做到这一点。默认情况下禁用外观(如果要启用它,您可以看到documentation中的说明),但我不建议启用外观作为应用程序的额外开销。相反,我会修改你的函数来调用app('auth')。这将返回Auth门面代理的类,而不加载所有其他外观。

public function byCredantial(Request $request)
{
    $user = [
        'email' => $request->input('email'),
        'password' => $request->input('password')
    ];

    $auth = app('auth');

    if ($auth->attempt($user)) {
        $response = $this->getSuccess($auth->user()->id);

        return response()->json($response, 200);
    } else {
        $response = $this->getError($user);

        return response()->json($response, 401);
    }
}

另外,我建议您阅读身份验证documentation并将大部分此代码放在AuthServiceProvider中。