Axeia
473
4
Laravel

Registration working, authentication not work

Posted 2 years ago by Axeia

Hello, I'm new to laravel and I've been following the "Laravel 5 Fundementals" videos hoping to learn how to work with laravel. I made it to episode 15 but now I'm running into a problem. I can confirm that if I use /auth/registration I can sign up new users successfully as they show up in my database with all the fields that were filled in successfully inserted. However trying to sign in a user doesn't seem to work.

I've set the AuthController (App\Http\Controllers\Auth\AuthController) to redirect to my homepage (/) and set some custom validation rules to reflect stricter rules / reflect new fields I added.

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

class AuthController extends Controller
{    
    use AuthenticatesAndRegistersUsers, ThrottlesLogins;
    protected $redirectTo = '/';
    public function __construct()
    {
        $this->middleware($this->guestMiddleware(), ['except' => 'logout']);
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username'  => 'required|max:255|unique:users,username',
            'email'     => 'required|email|max:255|unique:users,email',
            'password'  => ['required', 'min:8', 'max:255', 'confirmed',
                'regex:~^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}|.{20,}$~'],
            'realName'  => 'max:255'
        ]);
    }
    protected function create(array $data)
    {
        return User::create([
            'username'  => $data['username'],
            'email'     => $data['email'],
            'password'  => bcrypt($data['password']),
            'realName'  => empty($data['realName']) ? null : $data['realName']
        ]);
    }
}

In my controller for the root domain (/) I've set it as following:

    public function index()
    { 
        if(\Auth::check())
            return \Auth::user();
        return view('home');
    }

And I always get the view instead of the user, var_dump'ing \Auth::user() gives me a null value. My app\Http\User.php is as follows:

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    public $primaryKey = 'userName';  
    public $incrementing = false;    
    public $redirectPath = '/auth/login';

    protected $fillable = ['username', 'email', 'realName','password']; // Allow mass assigning
    protected $hidden = [
        'password', 'remember_token',
    ];
}

And I have not modified any class it inherits from. Could someone please tell me how to further debug this, from what I've understood from the tutorial video if I get redirected I should have a successful login, otherwise I would have been redirected to the form itself. I seem to have the awkward situation where I'm not logged in successfully but I am redirected. Doing a var_dump($errors) in my root view isn't giving me any insight either.

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

Reply to

Use Markdown with GitHub-flavored code blocks.