1

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.

Abdul Rehman
  • 159
  • 1
  • 2
  • 18
  • 1
    Could you show us the code where you are trying to register the user? – Mozammil Jan 11 '19 at 21:54
  • 1
    Sorry, my bad your code works good now I was trying to do it with some my custom command for setting role_id. Please do post your code again I will mark it as accept but it still doesn't provide message and it refreshes the page and is not redirected to home page – Abdul Rehman Jan 11 '19 at 22:04
  • Edited my answer to cater for a redirect back home and added an example of how to display the message :) – Mozammil Jan 12 '19 at 07:06

1 Answers1

2

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
Mozammil
  • 8,520
  • 15
  • 29