How to auth with Laravel 5.4

Hey there. I am unable to get anything to work because of the thousands of micro PHP classes scattered across another thousand folders created by Laravel. I keep getting all sorts of funny errors just by trying to see an application can actually work in Laravel and that it’s not a myth. First, I had to wrestle with all the suggestion on this issue. Painfully, the answer that helped me was the penultimate on such a long long thread of framework contributors living in denial. I’ve encountered dozens more, scoured tutorial sites, read this official doc on Laravel auth and stackoverflow questions with outdated answers to the cryptic errors I’ve been riddled with. This has been on for the past 7 days.

Some of the errors would have been avoidable if my auth table name was simply “users” and even now I’ve changed to “users”, I’m still stuck.

I eventually logged in at some point by removing this line

$this->middleware('auth');

from the HomeController constructor then manually setting up my own auth like I would have done in 30mins if I was developing from scratch. But it felt contrived. The tutorials say everything should work out of the box without touching any native PHP functions so I uncommented the line and continued writhing.

The present situation is that the login form just lies there even though I’ve manually seeded my database. From the web.php file (which I understand is neo routes.php), I have

Route::post('/login', 'HomeController@loginPost');

Then in its controller, the latest snippet I’ve tried is this

public function loginPost() {
        var_dump(Input::all());
        if (Auth::attempt(Input::all())) {
            var_dump('expression');
        User::create([
        'username' => strtolower(Input::get('username')),
        'password' => Hash::make(Input::get('password')),
        ]);

$user = User::where('username', '=', strtolower(Input::get('username')));

Auth::login($user);
        }
        else var_dump('expression2');
    }

It’s probably supposed to be email there instead of username but I did a replace all in the view, swapping email for username since the application’s authentication ID should be their usernames and not their emails. When I hit enter now, the page just reloads with empty input fields. It’s even tired of throwing errors at me. Is there a way out or is Laravel just not for me?

steps :
1 : hope you have fresh copy of laravel installtion
2 : now you have to add your database configuration in .env file (this is hidden file)
DB_CONNECTION=mysql
DB_HOST= your databse host
DB_PORT= your db port
DB_DATABASE= name_of_db
DB_USERNAME= user_of_db
DB_PASSWORD= password_of_db
3 : run ‘php artisan make:auth’ in your terminal
4 : now ‘php artisan migrate’ in your terminal this will craete your user table and now you can access authentication

Already done all that. How do I include email confirmation in the register process??Secondly, when I tried logging out, I got this error

'ErrorException in GenericUser.php line 46: Undefined index: usernamerException in GenericUser.php line 46: Undefined index: username

The file is located at

C:\wamp64\www\laravel\vendor\laravel\framework\src\Illuminate\Auth\GenericUser.php

and contains these

1 Like

ErrorException in GenericUser.php line 46: Undefined index: usernamerException in GenericUser.php line 46: Undefined index: username

for this error, Had you changed the function getAuthIdentifierName()??
If yes, then make it like this:
public function getAuthIdentifierName()
{
return ‘id’;
}
Or maybe your table does not contain a field called username.

How do I include email confirmation in the register process??

You can make the mail notification after user is registered, for that you can refer this:https://laravel.com/docs/5.4/mail#generating-mailables

At the time of register, you can take an active flag for making your email confirmation.
Initially, your active flag is 0 when a user register, After the user, confirm the mail, you can active the user by an update that particular user’s active field to 1.
You can also use the token based email confirmation if you want.
At the time of login, you can check the user is active or not. If not you have to show user to confirm their mail.

Please let me know if any question.

I’ve done that. The new error is “TokenMismatchException in VerifyCsrfToken.php line 68:”

As for the email validation, where am I to place the code since everything is supposedly running out of the box? I have no control over the controllers for auth. Unless I remove that line[quote=“nmeri17, post:1, topic:263845”]
$this->middleware(‘auth’);
[/quote]

and handle everything myself like I would in a normal application.

TokenMismatchException in VerifyCsrfToken.php line 68:" For this error Have you placed {{csrf_field}} in the form???

I only removed it before following your instructions. After running make:auth, the views were regenerated with their defaults (including the csrf placeholder). So it’s there. I think the token in the form is different from the one in the head tag. I tried synchronizing them manually back then before opening this thread and it worked. Remember I hadn’t logged in then at all. Now I’ve logged in but can’t log out. Do I go the manual way again i.e copy the one in the form and paste in the head.

Please send your code which you are using for this so that we can resolve not sure what mistake you are doing

I don’t understand. You mean the entire project folder? I already told you–after running make:auth everything about authentication went back to the way it was when I installed the project. The web.php, views and controllers (home and login).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.