Romentigo
4 months ago
354
3
Laravel

Doesn't autologin after registration

Posted 4 months ago by Romentigo

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:

<body>
<nav class="navbar navbar-expand-lg navbar-light bg-white">
    <a class="navbar-brand" href="{{url('/')}}">
        <img src="{{asset('images/lis6.png')}}" width="30" alt="Logo">
        <span>ScheduleFox</span>
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
    </button>

    <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>
</nav>
    @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.

Please sign in or create an account to participate in this conversation.