在Laravel中无需电子邮件即可验证用户身份

时间:2017-01-03 21:44:06

标签: php laravel authentication jwt dingo-api

我尝试在用户注册时选择电子邮件。这是package。所以我在此函数中删除了email' => 'required|email|unique:users',

public function signup(Request $request)
{
    $credentials = $request->all();
    $validator = Validator::make($credentials, [
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required|min:6|confirmed',
        'password_confirmation' => 'required|min:3'
    ]);
    if ($validator->fails()) {
        throw new ValidationHttpException($validator->errors()->all());
    }
    try {
        $user = $this->users->create($request->except('roles', 'permissions'));
        if (!$user->id) {
            return $this->response->error('could_not_create_user', 500);
        }

        $hasToReleaseToken = Config::get('boilerplate.signup_token_release');
        if ($hasToReleaseToken) {
            return $this->login($request);

        }

        return $this->response->created();
    } catch (\Exception $e) {
        return $this->response->error($e->getMessage(), 500);
    }

}

然后在config-boilerplate.php中我也删除了电子邮件:

'signup_fields_rules' => [
    'name' => 'required',  
    'email' => 'required|email|unique:users',///// this
    'password' => 'required|min:6'
],

但是当我注册时,我收到了这个错误:

  

" message":"未定义索引:电子邮件",     " status_code":500,     " debug":{       " line":173,       " file":" /Users/MyMac/Desktop/Project/laravel-5.3-boilerplate-api-jwt-vue2/vendor/dingo/api/src/Http/Response/Factory.php&# 34 ;,       " class":" Symfony \ Component \ HttpKernel \ Exception \ HttpException",

路线:

$api = app('Dingo\Api\Routing\Router');
$api->version('v1', function ($api) {
$api->post('auth/signup', 'App\Api\V1\Controllers\AuthController@signup');

我是否需要禁用任何内容以允许用户使用或不使用电子邮件进行注册?

1 个答案:

答案 0 :(得分:1)

如果您想将电子邮件作为可选字段,请使用sometimes|email请参阅Docs。请尝试以下代码:

public function signup(Request $request)
{
    $credentials = $request->all();
    $validator = Validator::make($credentials, [
        'name' => 'required',
        'email' => 'sometimes|email',
        'password' => 'required|min:6|confirmed',
        'password_confirmation' => 'required|min:3'
    ]);
    if ($validator->fails()) {
        throw new ValidationHttpException($validator->errors()->all());
    }
    try {
        $user = $this->users->create($request->except('roles', 'permissions'));
        if (!$user->id) {
            return $this->response->error('could_not_create_user', 500);
        }

        $hasToReleaseToken = Config::get('boilerplate.signup_token_release');
        if ($hasToReleaseToken) {
            return $this->login($request);

        }

        return $this->response->created();
    } catch (\Exception $e) {
        return $this->response->error($e->getMessage(), 500);
    }

}

<强>配置-boilerplate.php

'signup_fields_rules' => [
    'name' => 'required',  
    'email' => 'sometimes|email',///// this
    'password' => 'required|min:6'
],