Registered users

0

I'm developing a system with Laravel and I have the following goal: To make only the administrator user able to register new users. My problem is not blocking the access of ordinary users to the user registry, but rather make the user registry accessible with the Administrator logged in, because in the standard way the user registration happens without the user being logged in and when I put a button inside the system directing to the user registry it blocks, redirects to the home. In case my system is all set, only the user registry is accessible. I hope I made my problem clear.

Registration Function:

public function salvar()
{
    $name = request()->input('name');
    $email = request()->input('email');
    $password = request()->input('password');

    $passwords = bcrypt($password);

    DB::insert('INSERT INTO users (name, email, password) VALUES (?, ?, ?)', array($name, $email, $passwords));

    return redirect() ->action('HomeController@index');
}
    
asked by anonymous 02.05.2018 / 15:45

1 answer

1

Just create a new route to register users, and do not use the default route. For example:

Route::post('admin/user/save', 'UserController@save')->name('admin.user.save');

UserController:

public function save(Request $request)
{
    try {
        $response = [
            'success' => false
        ];

        $data = $request->all();

        $params = [
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password'])
        ];

        $user = User::create($params);

        if ($user) {
            $response = [
                'success' => true
            ];
        }
        return redirect()->action('HomeController@index')->with($response);

    } catch (\Exception $e) {
        throw $e;
    }
}

And in your view , you can treat the return if success is true or false , to display a success or error message.

The save method is just an example, more treatments should be implemented according to your need, if you are going to create a password for it at the moment, you have to use the Hash::make($data['password']) method among other features you should have in your registration.

    
02.05.2018 / 17:28