redirect user to specific page after he register its no working, the user is being redirected to the homepage

user9297268 Source

I have a home page that has a link "Create Post" to create a new post (<a href="{!! route('post.create') !!}">Create Post</a>).

When this link is clicked, if the user is authenticated he should be allowed to access this create post page.

But if the user is not authenticated the user should be redirected to the login/register page. After he login or register he should be redirected to the create post page. The login part is working the register part is not working.

I have the code below to do this but when the user dont have an account and click in "Create Post" he is redirected to the login page and in this login page there is a link to the register page. If the user click in this register link will be redirected to the register page. In this page he need to enter the name, email and password and submit the form, and because he previously cicked in the "Create Post" link he should be redirected to the create post page but its not working, he is being redirected to the homepage and not to the create post page.

Routes:

Route::group(['prefix' => 'users ', 'middleware' => 'auth'], function(){
    Route::post('/post/store', [
        'uses' => '[email protected]',
        'as'   => 'post.store'
    ]);

    Route::get('/createPost', [
        'uses' => '[email protected]',
        'as'   => 'post.create'
    ]);
});

Auth::routes();

Route::get('/home', '[email protected]')->name('home');

Register Controller:

 class RegisterController extends Controller
{


    use RegistersUsers;


    protected $redirectTo = '/home';


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


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),

        ]);
    }

    protected function authenticated(Request $request, $user)
    {
        return redirect()->intended('/createPost');
    }

}

Full Register Controller

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use App\Http\Controllers\Auth\Request as AuthRequest;

class RegisterController extends Controller
{


    use RegistersUsers;

    protected $redirectTo = '/home';


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


    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
        ]);
    }


    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),

        ]);
    }

    protected function registered(AuthRequest $request, $user)
    {
        return redirect('/createPost');
    }

}
phplaravel

Answers

answered 3 months ago Alexey Mezenin #1

After he login or register he should be redirected to the create post page. The login part is working the register part is not working

You can override the register() method in RegisterController:

protected function registered($request, $user)
{
    return redirect('some_page');
}

answered 3 months ago dilusha_dasanayaka #2

Override $redirect in registerController

protected $redirectTo = '/home';

to

protected $redirectTo = '/create_post';

this is a static way.

comments powered by Disqus