sen*_*nty 16 php oauth laravel laravel-passport laravel-5.4
我设置了密码授权(它是应用程序的后端).现在,我可以发送一个帖子请求oauth/token,它适用于邮递员.但是,如果我也想从api注册用户怎么办?
我知道我可以使用当前/register路由,但是,我是否需要将用户重定向回登录页面并使用他的凭据再次登录?
或者在RegisterController中,在registered()函数中,我应该重定向到oauth/token路由吗?(为此,请注意我发送的所有5个数据都在'x-www-form-urlencoded'中,它似乎有效.但是,我是否需要在标题中分开一些?这对我来说很模糊,所以只是想要当我有机会时问.)
或者我应该oauth/token像这个人一样在方法中添加一些东西?实际上,我试图捕获库中方法的发布$request数据AccessTokenController@issueToken,但是我无法弄清楚如何操作parsedBody数组.如果我从实际库中触发我的寄存器功能,我怎么知道它是注册还是登录?
也许我错过了一些信息,但根据这个主题我找不到任何东西.在Passport中处理注册用户的正确方法是什么?
更新:接受的答案显示'注册'周期; 在它下面我添加了'login'和'refresh token'实现.希望能帮助到你 :)
Nil*_*hod 16
在您的API中创建路由为
Route::post('register','Api\UsersController@create');
Run Code Online (Sandbox Code Playgroud)
并在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);
}
Run Code Online (Sandbox Code Playgroud)
创建新用户后,返回访问令牌.
在一年之后,我想出了如何实现完整周期.
@Nileshsinh方法显示寄存器周期.
这里是登录和刷新令牌部分:
Route::post('auth/token', 'Api\AuthController@authenticate');
Route::post('auth/refresh', 'Api\AuthController@refreshToken');
Run Code Online (Sandbox Code Playgroud)
方法:
class AuthController extends Controller
{
private $client;
/**
* DefaultController constructor.
*/
public function __construct()
{
$this->client = DB::table('oauth_clients')->where('id', 1)->first();
}
/**
* @param Request $request
* @return mixed
*/
protected function authenticate(Request $request)
{
$request->request->add([
'grant_type' => 'password',
'username' => $request->email,
'password' => $request->password,
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'scope' => ''
]);
$proxy = Request::create(
'oauth/token',
'POST'
);
return \Route::dispatch($proxy);
}
/**
* @param Request $request
* @return mixed
*/
protected function refreshToken(Request $request)
{
$request->request->add([
'grant_type' => 'refresh_token',
'refresh_token' => $request->refresh_token,
'client_id' => $this->client->id,
'client_secret' => $this->client->secret,
'scope' => ''
]);
$proxy = Request::create(
'oauth/token',
'POST'
);
return \Route::dispatch($proxy);
}
}
Run Code Online (Sandbox Code Playgroud)