Laravel register and log in via phone number and SMS confirmation

人走茶凉 提交于 2020-04-16 08:20:13

问题


I want to make a test-project where User can register just by setting a phone number and a password. On next step, I want a package or smth like that, that will send an SMS to this number with a confirmation code. So, after User enters this code, it became registered, authenticated and redirected to homepage.

Any suggestions on what components/libraries/packages I have to use and how to modify standard Laravel registration/authentication? Thanks!


回答1:


You can follow the below steps to login with either username, email or phone. I have also included slug and avatar for extended usage. However you can edit and change them as per your needs.

Migration

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username')->unique();
            $table->string('phone')->unique();
            $table->string('email')->unique();
            $table->string('password');
            $table->string('avatar');
            $table->string('slug');
            $table->boolean('gender');
            $table->rememberToken();
            $table->timestamps();
        });

In your Auth/RegisterController modify as follows

protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'username' => 'required|string|max:20|unique:users',
            'phone' => 'required|string|max:15|unique:users', //you can also use required|regex:/[0-9]{10}/|digits:10 as per your needs
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|dumbpwd|confirmed',
            'gender' => 'required|bool',
        ]);
    }

protected function create(array $data)
    {
        if($data['gender'])
          {
            $avatar = 'male.png';
          }
        else
          {
            $avatar = 'female.png';
          }

        return User::create([
            'name' => $data['name'],
            'gender' => $data['gender'],
            'username' => $data['username'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'slug' => str_slug($data['username']),
            'avatar' => $avatar,
        ]);
    }

Now in your logincontroller add this

protected function credentials(Request $request)
      {
        if(is_numeric($request->get('email'))){
          return ['phone'=>$request->get('email'),'password'=>$request->get('password')];
        }
        elseif (filter_var($request->get('email'), FILTER_VALIDATE_EMAIL)) {
          return ['email' => $request->get('email'), 'password'=>$request->get('password')];
        }
        return ['username' => $request->get('email'), 'password'=>$request->get('password')];
      }

I haven't included the model and views as they are easy. All you need is to add the new column fields to the model array and add extra fields on the registration form for username, phone, gender and avatar.

On your login view form, make the input type as text where you can either enter email, username or phone and password field remains the same.

You can further easily extend this to make user activation via code or link using any one of the below packages. I use email activation using https://github.com/bestmomo/laravel-email-confirmation which works wonderful.

I haven't tried phone verification and activation using a code, but found few packages that could help you https://github.com/phonedotcom/sms-verification-laravel

There are number of packages like twilio, google 2 step verification and many other that you can search on github or packagist. Hope this helps. Happy Coding!



来源:https://stackoverflow.com/questions/48883060/laravel-register-and-log-in-via-phone-number-and-sms-confirmation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!