Registration of users with Adm logged in

Asked

Viewed 264 times

0

I am developing a system with Laravel and I have the following objective: To make only the admin user can register new users. My problem is not to block the access of ordinary users to the user registration, but to make the user registration accessible with the logged in administrator, Because in the Standard the user registration happens without the user being logged in and when I put a button inside the system directing to the user registration it blocks, redirects to the home. In case my system is all ready, missing only the registration of users be accessible. I hope I have made clear my problem.

Function Register:

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');
}

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 his view, you can treat the return case success be it 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 that time, you have to use the method Hash::make($data['password']) among other particularities that you must have in your register.

  • I had started to do this, only that as I would have to create another controller pro Register I would have to use the same encryption method of Aravel q is bcrypt, only that I don’t know how to use this encryption method... But thanks for your answer, I will try to understand how bcrypt works.

  • it’s like I said there, you can use the Hash:make To encrypt the password, have a look at the documentation. https://laravel.com/docs/5.6/hashing

  • is that I made my registration structure different, I’ll put along with the question, but it worked legal.

  • works not kkk forget

  • Yes, try to do something similar to my answer, I will modify according to what I understand may be your need

  • now it worked, put $passwords = bcrypt($password); dps passed $passwords in array

  • Good @João.Mistura, I edited my answer, I think you can use that way, not to create a query inside your controller

  • blzz, @arllondias valeu

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.