2

I am having a challenge with registering new users. I am accessing the BackEnd from an Android app (API).

I am using this Laravel API Boilerplate

Here is my validation rule;

 'signup_fields_rules' => [
        'sname' => 'required',
        'fname' => 'required',
        'gender' => 'required',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:8',
    ],

and my new user registration code

public function signup(Request $request)
    {
        $signupFields = Config::get('boilerplate.signup_fields');
        $hasToReleaseToken = Config::get('boilerplate.signup_token_release');

        $userData = $request->only($signupFields);

        $validator = Validator::make($userData, Config::get('boilerplate.signup_fields_rules'));

        if($validator->fails()) {
            Log::info($validator->errors()->all());
            throw new StoreResourceFailedException('Could not create account.',$validator->errors()->all());
        }
        User::unguard();

        $dept = DB::table('depts')->select('dept_id')
                                ->where('dept_name', $request->input('dept'))->first();

        $userData['user_id'] = str_random(60);
        $userData['dept_id'] = $dept->dept_id;
        $user = User::create($userData);
        User::reguard();

        if(!$user->id) {
            return $this->response->error('could_not_create_user', 500);
        }

        if($hasToReleaseToken) {
            return $this->login($request);
        }

        return $this->response->created();
    }

The problem:

When a new user try to register, it shows Email taken already. I found out that the new User data will be stored in the database before showing the error. Is it that the Registration function is being called twice.

I need help with this, please.

BlackPearl
  • 2,532
  • 3
  • 33
  • 49

0 Answers0