2017-05-25 141 views
5

我设置了密码授权(它是应用程序的后端)。现在,我可以发送邮件请求到oauth/token,它适用于邮差。但是,如果我想从api注册用户呢?使用Laravel Passport注册用户

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

或者在RegisterController中,在registered()函数中,我该怎么做我重定向到oauth/token路由? (为此,请注意,我发送了'x-www-form-urlencoded'中的所有5个数据,并且它似乎能够工作。但是,我需要将头部中的某些部分分开吗?对我来说很模糊,所以只是想要询问我什么时候有机会)。

或者我应该在oauth/token方法中添加一些东西,如this guy?实际上,我试图在库中的[email protected]方法上发现$request的数据,但我无法弄清楚如何操作parsedBody数组。如果我从实际库中触发我的注册功能,我怎么知道它是注册还是登录?

也许我错过了一些信息,但我找不到任何基于此主题的内容。在Passport中处理注册用户的正确方法是什么?

回答

4

在您的API创建的路线为

Route::post('register','Api\[email protected]'); 

而且在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); 
} 

和创建新用户后,返回访问令牌。