I have a home page that has a link "Create Post" to create a new post (Create Post).
When this link is clicked, if the user is authenticated he should be allowed to access this create post page.
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 to create an account, and because he previously cicked in the "Create Post" link he should be redirected to the create post page but its not working properly:
Error:
The user just should be redirected to the create post page after register if he previously clicked in the create new post link, otherwise he should be redirected to the homepage after register. But the user is being always redirected to the create post page after he registers.
Do you know where is the error?
RegisterController
<?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($request, $user)
{
return redirect('(/users/createPost');
}
}
Routes:
Route::group(['prefix' => 'users', 'middleware' => 'auth'], function(){
Route::post('/post/store', [
'uses' => 'PostController@store',
'as' => 'post.store'
]);
Route::get('/createPost', [
'uses' => 'PostController@create',
'as' => 'post.create'
]);
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');