Search code examples
phplaravellaravel-authorizationlaravel-authenticationlaravel-session

How to make auto Logged In off on creation of new user using auth() in laravel 5.7?


I'm making a project in which I want to create user through Laravel auth() but don't want to get it auto logged in on creation of account. I have copied the default register page into studentRegister and I want to register it through admin when he gets logged in, my code is as follow :

@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ __('Register') }}</div>

                    <div class="card-body">
                        <form method="POST" action="{{ route('registerStudent') }}">


                            <div class="form-group row">
                                <label for="name" class="col-md-4 col-form-label text-md-right">{{ __('Name') }}</label>

                                <div class="col-md-6">
                                    <input id="name" type="text" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" name="name" value="{{ old('name') }}" required autofocus>

                                    @if ($errors->has('name'))
                                        <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('name') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>

                                <div class="col-md-6">
                                    <input id="email" type="email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="{{ old('email') }}" required>

                                    @if ($errors->has('email'))
                                        <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('email') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

                                <div class="col-md-6">
                                    <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" required>

                                    @if ($errors->has('password'))
                                        <span class="invalid-feedback" role="alert">
                                        <strong>{{ $errors->first('password') }}</strong>
                                    </span>
                                    @endif
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>

                                <div class="col-md-6">
                                    <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required>
                                </div>
                            </div>

                            <input id="role_id" type="hidden" name="role_id" value="1">


                            <div class="form-group row mb-0">
                                <div class="col-md-6 offset-md-4">
                                    <button type="submit" class="btn btn-primary">
                                        {{ __('Register') }}
                                    </button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

I have removed the guard function in the controller as well so I can use this when one user is already logged in.

Kindly tell me how to do this that I just create users through it but users session don't get changed from old one to new created one.


Solution

  • You could just override the register() method found in Illuminate\Foundation\Auth\RegistersUsers.

    Since App\Http\Controllers\Auth\RegisterController uses that trait, you would override the method there. The original method found in the trait is as follows.

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
    
        event(new Registered($user = $this->create($request->all())));
    
        $this->guard()->login($user);
    
        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }
    

    You would replace it with:

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
    
        event(new Registered($user = $this->create($request->all())));
    
        return back()
            ->with('success', 'An account has been created successfully');
    }
    

    As you can see, instead of logging in the user and redirecting him after an account was created for them, we are just going to create their account and return back to the form.

    You could also check if session has a success key and display the message accordingly.

    Edit: If you want to redirect back to homepage, you would do something like this:

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();
    
        event(new Registered($user = $this->create($request->all())));
    
        return redirect('/')
            ->with('success', 'An account has been created successfully');
    }
    

    and to display the message, you would need to have this in your blade file.

    @if(Session::has('success'))
        {{ Session::get('success') }}
    @endif