dimanche 31 juillet 2016

Modifing the Auth registration system in Laravel 5.2 to include hidden form fields

every one. I'm currently having an issue modifying the Auth registration system in Laravel 5.2. What I am trying to do, is have two separate loging system for regular users and admins. I added a new field into the database for the users called admin, and set the default to 0, or false. Using the registration page that Laravel's auth system uses, I added a hidden input and set the value to 1. This page will obviously be used to log in the admins so that they can enter the admin area.

When I submit the form, the admin field is simply set to the default value, which is not what I want for this portion of the site. Once the user clicks submit on this page, I want his information to get sent to the database the the admin value of 1. Is there any way of doing this?

Here is what I have done so far to customize the auth registration system.

I removed the name field and added a firstname and lastname field so that the users can enter both of these value in separate text inputs. I updated the User model to include these fields in the $fillable array and updated the AuthControllers validate method so that these fields could be validated.

Here is the code for each

Here is the User.php model file

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'firstname', 'lastname', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token', 'admin',
];
}

and here os the AuthContoller file

class AuthController extends Controller
{

use AuthenticatesAndRegistersUsers, ThrottlesLogins;

/**
 * Where to redirect users after login / registration.
 *
 * @var string
 */
protected $redirectTo = '/';

/**
 * Create a new authentication controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'firstname' => 'required|max:255',
        'lastname' => 'required|max:255',
        'email' => 'required|email|max:255|unique:users',
        'password' => 'required|min:6|confirmed',
        'admin' => 'required',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return User
 */
protected function create(array $data)
{
    return User::create([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'admin' => $data['admin'],
    ]);
}
}

As you can see, I also added the admin field in the User.php model file hoping that that would auto fill automatically.

If anyone can tell me how this could be done, that would be great. But as you can see this isn't really a practical solution for an actual website, but its more about learning the ins-and-outs of Laravel and learning how to customize the heck out of it. As I began to write this question out, it seemed to me, that I could easily just use the registration for any user, then allow an upper level admin to set the value of the admin field to true so that the user could venture into the admin sections of the application. Of course, will a system like this, there would be no need to have two separate log in screens, but like I said, I'm just learning.

I also plan on adding roles, similar to word presses for the various level's of admins, that would control things such as creating, editing, and publishing content such as posts. but for now, I'm just working on this system as it is.

Thanks for any help and comments about this topic.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2anxpRN
via IFTTT

Aucun commentaire:

Enregistrer un commentaire