简体   繁体   中英

How to redirect the user to the same page after login?

I have a login.blade.php, so when user accesses "project.test/login" he has an email and password fields to login.

I also have a header.blade.php that has the main menu, in this main menu there is a link "Create congress". If the user is not authenticated and click in "Create congress" he should not have acess to the page, he should be redirected to the login page. After login with success he should be redirected to the "Create congress" page.

This is working fine.

Doubt:

But then I also have a "registration.blade.php" when user accesses " http://project.test/congress/1/congresstest/registration ". When user accesses this page if is not authenticated it appears this this message:

@if(!\Auth::check())
    <div class="alert alert-info" role="alert">
        <span>
            Already have an account? 
            <a data-toggle="modal" 
            data-target=".bd-example-modal-lg" 
            class="font-weight-bold" href="">Login.</a>
        </span>
    </div>
@endif

If user clicks in Login, it appears a modal so the user can insert the email and password and if login with success the modal should close and he should be redirected to the same registration page.

The issue is that if the user logins with success in this page " http://project.test/congress/1/congresstest/registration " with the form in the modal he is redirected to the "Create congress" page instead of staying in the registration page. Do you know how to correct this issue?

Relevant code for the question:

Login.blade.php:

<form class="clearfix" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group col-12 px-0">
    <label for="inputEmail4">Email</label>
    <input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus>
</div>
<div class="form-group col-12 px-0">
    <label for="inputEmail4">Password</label>
    <input type="password" class="form-control" name="password" required>
</div>
<button type="submit">Login</button>
</form>

LoginController:

class LoginController extends Controller
{
    use AuthenticatesUsers;
    protected $redirectTo = '/home';

    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    protected function authenticated(Request $request, $user)
    {
        return redirect()->intended($this->redirectTo);
    }
}

header.blade.php with the main menu where there is a link to create a new congress:

<li>
    <a href="{!! route('congress.create') !!}"> Create Congress </a>
</li>

Modal with the login form:

<div class="modal fade bd-example-modal-lg" id="modal2" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-sm">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Login</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        <div class="container">
          <div class="row">
            <form class="clearfix" method="POST" action="{{ route('login') }}">
              {{ csrf_field() }}

              <div class="form-group col-12 px-0">
                <label for="inputEmail4">Email</label>
                <input type="email" class="form-control" value="{{ old('email') }}" name="email" required autofocus>
              </div>

              <div class="form-group col-12 px-0">
                <label for="inputEmail4"
                       class="text-heading font-weight-semi-bold">Password
                  <a href="{{ route('password.request') }}" class="text-gray ml-1" style="text-decoration: underline">
                    <small>Recover Password</small></a> </label>
                <input type="password" class="form-control" name="password" required>
              </div>


              <button type="submit" class="btn btn-primary btn d-block w-100">Login</button>

            </form>
          </div>
        </div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>

Routes:

Route::get('/', [
    'uses' => 'FrontController@index',
    'as'   =>'index'
]);

// Auth routes
Auth::routes();


Route::get('auth/{provider}/callback', [
    'uses' => 'OauthController@handleProviderCallback',
]);

// route to the registration page
Route::post('/congress/{id}/{slug?}/registration', [
    'uses' => 'RegistrationController@storeQuantity',
    'as'   =>'congresses.registration'
]);

 Route::group(['prefix' => '', 'middleware' => 'auth'], function(){

// route to the create congress page
  Route::get('/congress/create', [
        'uses' => 'CongressController@create',
        'as'   => 'congress.create'
    ]);
  }

CongressController:

class CongressController extends Controller
{

    public function create()
    {
        return view('congresses.create')->with('categories', Category::all());
    }
}

you need to use session for this, in your congresstest create add the following

$uri = $request->path();
$request->session()->put('last_uri', $uri);

then on successful login:

protected function authenticated(Request $request, $user)
{    
    $last_uri = $request->session()->get('last_uri');
    // or just simply return redirect()->route('NEEDED URI');
    return redirect()->route($last_uri);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM