php - 使用 Laravel Passport 注册用户

标签 php laravel oauth laravel-5.4 laravel-passport

我设置了密码授予(它是应用程序的后端)。现在,我可以向 oauth/token 发送发布请求,它可以在 Postman 上运行。但是,如果我也想从 api 注册用户怎么办?

我知道我可以使用当前的 /register 路由,但是,我是否需要将用户重定向回登录页面并让他使用他的凭据再次登录?

或者在 RegisterController 中,在 registered() 函数中,我应该重定向到 oauth/token 路由吗? (为此,请注意我正在发送“x-www-form-urlencoded”中的所有 5 个数据,它似乎有效。但是,我需要在标题中分隔一些吗?这对我来说很模糊,所以只是想要问我什么时候有机会)。

或者我应该在 oauth/token 方法中添加一些东西,比如 this guy ?实际上,我试图在库内的 AccessTokenController@issueToken 方法上捕获发布的 $request 数据,但是我不知道如何操作 parsedBody数组。如果我从实际库触发我的注册功能,我怎么知道它是注册还是登录?

也许我遗漏了一些信息,但我找不到与此主题相关的任何信息。在 Passport 中处理注册用户的正确方法是什么?


更新:接受的答案显示“注册”周期;在它下面我添加了“登录”和“刷新 token ”实现。希望对您有所帮助:)

最佳答案

在您的 API 中创建路由为

Route::post('register','Api\UsersController@create');

然后在 UsersController 中创建方法 create()

function create(Request $request)
{
    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $request
     * @return \Illuminate\Contracts\Validation\Validator
     */
    $valid = validator($request->only('email', 'name', 'password','mobile'), [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'password' => 'required|string|min:6',
        'mobile' => 'required',
    ]);

    if ($valid->fails()) {
        $jsonError=response()->json($valid->errors()->all(), 400);
        return \Response::json($jsonError);
    }

    $data = request()->only('email','name','password','mobile');

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'mobile' => $data['mobile']
    ]);

    // And created user until here.

    $client = Client::where('password_client', 1)->first();

    // Is this $request the same request? I mean Request $request? Then wouldn't it mess the other $request stuff? Also how did you pass it on the $request in $proxy? Wouldn't Request::create() just create a new thing?

    $request->request->add([
        'grant_type'    => 'password',
        'client_id'     => $client->id,
        'client_secret' => $client->secret,
        'username'      => $data['email'],
        'password'      => $data['password'],
        'scope'         => null,
    ]);

    // Fire off the internal request. 
    $token = Request::create(
        'oauth/token',
        'POST'
    );
    return \Route::dispatch($token);
}

创建新用户后,返回访问 token 。

关于php - 使用 Laravel Passport 注册用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44172818/

相关文章:

php - 使用PHPExcel导出到xlsx

php - 如何使用php mysqli搜索数据库的所有表并显示答案

Laravel 就像查询生成器一样

mysql - 错误号 : 150 "Foreign key constraint is incorrectly formed" when trying to create a foreing key

mysql - 如何将 SQL 查询转换为 Eloquent Laravel

python - 零足迹 Python-Social-Auth 身份验证

json - 将 oAuth token 与 Azure MobileServiceClient.login() 结合使用

javascript - 仅当监听器存在时才发布消息?

php - Parse.com PHP SDK 获取对象的用户关系

c# - 在 .NET 中使用 OAuth2.0 两条腿的方法访问 Jira API