Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jacobjones's avatar

Doesn't autologin after registration

I'm using default Laravel Auth with some changes. I noticed that it doesn't log a user in after a registration. It works weird. In my app.blade it looks like this:

Logo ScheduleFox
<div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
        <li class="nav-item">
            <a class="nav-link {{request()->is('instruments') ? 'active' : ''}}" href="{{url('/instruments')}}">Інструменти</a>
        </li>
    </ul>
    <div class="form-inline nav-pills my-2 my-lg-0">
        @auth
            <ul class="navbar-nav">
                <li class="nav-item mr-3 {{request()->is('profile') ? 'active' : ''}}">
                    <a class="nav-link font-weight-bold" href="{{url('/profile')}}">{{\Illuminate\Support\Facades\Auth::user()->name}}</a>
                </li>
            </ul>
            <a class="nav-link" href="{{url('/logout')}}">Вихід</a>
        @endauth
        @guest
            <a class="nav-link" href="{{url('/login')}}">Вхід</a>
        @endguest
    </div>
</div>
@yield('content') My navbar shows Login for guests and other elements for logged in users. The problem is next: after register my navbar changes like I'm logged in, but it's not. Nothing works, nothing is shown except navbar. When I refresh my app, it redirects me to welcome.blade and shows me a navbar as for guest. What I'm doing wrong? My Register and Login controllers:

Register

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller; use App\Providers\RouteServiceProvider; use App\User; use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller { /* |-------------------------------------------------------------------------- | Register Controller |-------------------------------------------------------------------------- | | This controller handles the registration of new users as well as their | validation and creation. By default this controller uses a trait to | provide this functionality without requiring any additional code. | */

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/profile';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => ['required', 'string', 'max:255'],
        'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
        'role_id' => ['required'],
        'password' => ['required', 'string', 'min:8', 'confirmed'],
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'role_id' => $data['role_id'],
        'password' => Hash::make($data['password']),
    ]);
}

} Login

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use App\Providers\RouteServiceProvider; use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller {

use AuthenticatesUsers;

public function authenticated(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (Auth::attempt($credentials))
    {
        return redirect('/profile');
    }
}

public function logout(Request $request) {
    Auth::logout();
    return redirect('/');
}

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

} UPD: It works fine when I log in manually. It just trying to log me in automatically after register and it fails.

...

https://shareit.onl/ https://appvn.onl/

0 likes
1 reply
jlrdw's avatar

What version, that does not look like version 7.* or 6.* code.

I strongly suggest until you learn laravel authentication and authorization well, you just use "out of box" auth as it works really well.

I'm not grasping your authenticated method above, it is there for "after" someone is already authenticated.

I use it for example to redirect to either an admin area or a bookkeeping area depending on role of authenticated logged in user.

Please or to participate in this conversation.