使用Laravel护照注册用户

我设置了密码授予(它是应用的后端)。现在,我可以向Postman发送帖子请求,并且它可以在Postman上工作。但是,如果我也想从API注册用户怎么办?oauth/token

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

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

或者我应该像这个家伙一样在方法中添加一些东西?实际上,我试图在库中捕获方法上发布的数据,但是我无法弄清楚如何操作数组。如果我从实际库中触发我的注册函数,我如何知道它是注册还是登录?oauth/token$requestAccessTokenController@issueTokenparsedBody

也许我错过了一些信息,但我找不到基于这个主题的任何东西。处理护照注册用户的正确方法是什么?


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


答案 1

在 API 中,将路由创建为

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

在用户控制器中创建方法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);
}

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


答案 2

一年后,我想出了如何实施整个周期。

@Nileshsinh方法显示寄存器周期。

以下是登录和刷新令牌部分:

Route::post('auth/token', 'Api\AuthController@authenticate');
Route::post('auth/refresh', 'Api\AuthController@refreshToken');

方法:

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

推荐