Packy
Packy

Reputation: 3583

laravel 5.2 dont log in user after registration

I am building a system where the Admin can add users. That being said my Auth controller looks like this, which works great, but using the standard registration the user gets logged in after creation. Obviously if the Admin adds a users (another admin or just a general user) the admin wants to remain logged in and not log in the user just created. How can I do this in a manner where I dont have to edit any of the library? If I update Laravel that would overwrite those changes.

AuthController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;
use Mail;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image as Image;


class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

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

    /**
     * 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, [
            'first-name' => 'required|max:255',
            'last-name' => 'required|max:255',
            'phone' => 'required|max:255',
            'form' => 'max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create( array $data ) 
    {
        //Create the user
        $user = User::create([
            'first_name' => $data['first-name'],
            'last_name' => $data['last-name'],
            'phone' => $data['phone'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        //Is it a User? Then give them that role
        if ($data['user-role'] == 'user')
        {
            $role = Role::where('name', '=', 'user')->firstOrFail();
            $user = User::find($user->id);
            $user->roles()->attach($role->id);
        }

        //Is it an Admin? Then give them that role
        if ($data['user-role'] == 'admin')
        {
            $role = Role::where('name', '=', 'owner')->firstOrFail();
            $user = User::find($user->id);
            $user->roles()->attach($role->id);
        }

        Mail::send('auth.emails.registered', ['user' => $user], function ($m) use ($user) 
        {
            $m->to($user->email, $user->first_name)->subject('You Have Been Added');
        });

        return $user;
    }

}

Upvotes: 1

Views: 598

Answers (1)

thefallen
thefallen

Reputation: 9749

The RegisterUsers trait, used on registration, automatically logs users in the register() method, but you can safely overwrite it in the AuthController like this:

public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $this->create($request->all());

    return redirect($this->redirectPath());
}

Upvotes: 1

Related Questions