流明自定义验证无锋 [英] Lumen custom authentication without Eloquent

查看:216
本文介绍了流明自定义验证无锋的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

张贴问题<一后href=\"http://stackoverflow.com/questions/36086846/lumen-dingo-jwt-is-not-instantiable-while-building\">Lumen +巴丁格+智威汤逊是不是实例化,而在SO建设约流明和巴丁格在这里我就如何建立这样一个系统,一个漂亮的详细的解答。

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.

我迄今所做的:


  • 删除 $ APP-GT&; withEloquent(); 从我们的引导\\ app.php

  • Removed $app->withEloquent(); from our bootstrap\app.php

其他编辑我觉得需要做的是编辑配置\\ auth.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.

最后,程序\\阿比\\ V1 \\控制器\\ AuthController @ postLogin 有打了一个电话到验证功能。此功能需要与我的框架,而不是通过雄辩沟通。这是如何流明整齐地做我也不能肯定。

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.

混帐回购: https://github.com/krisanalfa/lumen-dingo

推荐答案

您可以读取的这个。所以你的情况,在程序\\阿比\\ V1 \\控制器\\ AuthController @ postLogin

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,
    ]);
}

这篇关于流明自定义验证无锋的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆