Lumen: How to make Auth login for user using middleware?

2.9k views Asked by At

I'm trying to pass a username and password from a form into a controller, check if that user exists (users database table), and then redirect to an authenticated page where only users that are logged in can see their username.

In my routes.php:

$app->post('/', ['uses' => 'AuthenticationController@login']);

$app->group(['middleware' => 'auth'], function ($app) {
    $app->post('/tickets', ['uses' => 'TicketsController@getTickets']);
});

Then my AuthenticationController.php login function:

public function login(Request $request) {
    $credentials = $request->only(['username','password']);

    $user = App\User::find($credentials);

    if ($user) {
        // I'm assuming you do a redirect() here but am unsure how with Auth
    } else {
        return 'not logged in';
    }
}

At this point, I don't know what I need to put into my getTickets function in my TicketsController.php.

Anyone that knows how to go about doing this, please throw me a bone as to how to proceed.

Ultimately, it'll be great to have the tickets.blade.php view to have {{ user->username }} and to display the username of who's logged in. If the user doesn't log in and visits this view, nothing displays because they're not authenticated.

EDIT: Currently, I'm getting this error:

FatalErrorException in AuthenticationController.php: Class 'App\Http\Controllers\App\User' not found

I do have both of these lines included in my AuthenticationController.php before all functions begin:

use App\User;
use Auth;

Maybe it's the way I'm passing my $credentials into the find function, but that doesn't solve the Authentication side of things.

0

There are 0 answers