Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use the method attempt() - Laravel 7

I'm trying to figure out how Auth::attempt() works. It doesn't return any error but it doesn't work either, just returns false.

I have tried to define the table name, I tried to add guards but I'm not sure how it works.

According to Laravel documentation, if I'm not mistaken, I should add a guard for each authentication (I need authentication with different tables).

https://laravel.com/docs/7.x/authentication#introduction

That's how I defined a new guard: (I'm not sure if it has something to do with my issue)

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'web' => [
        'driver' => 'session',
        'provider' => 'guest',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],

My question is how can I authenticate users retrieving data from the table I want using Auth::attempt() or should I use my own method instead?

I'm new to Laravel so it's a little bit confusing.

like image 293
NewUser2 Avatar asked Mar 03 '23 06:03

NewUser2


2 Answers

One definite error is, that you can't have two guards with the same name.

The next error might be, that you didn't add the provider 'guest', but that can only be guessed.

I would go about it like this:

  1. Give your second web provider another name.
  2. Make sure your first web auth guard is working as expected again
  3. Go about implementing the second web auth guard

As an example your auth.php might look something like this:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'guest' => [
        'driver' => 'session',
        'provider' => 'guest',
    ],
    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],
],
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],
    'guest' => [
        'driver' => 'eloquent',
        'model' => App\Models\Guest::class,
    ],
]

You would have to define the model Guest as well, of course.

Then you could do something like:

Auth::guard('web')->attempt($credentials)
// or
Auth::guard('guest')->attempt($credentials)

Something to think about

Sometimes you think you need different tables for authentication, but in reality it might be a much easier approach to work with one basic user table and auth guard and then use other tables to connect the user to further data and roles, for example.

like image 190
wschopohl Avatar answered Mar 05 '23 15:03

wschopohl


In Laravel, default web guard used for authentication.

config/auth.php


'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
 ],

 /** Define driver and respective provider, Supported: session, token **/ 

 'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users', 
    ],

    'admin' => [
        'driver' => 'session',  
        'provider' => 'admin',
    ],
    //..
 ],

 /** Defines how the users are retrieved out of your database **/

'providers' => [
   'users' => [
       'driver' => 'eloquent',
       'model' => App\User::class,
    ],

    'admin' => [  
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ],
    //...
]

By default, laravel use following model for authentication. No need to specify guard name here.

App\User

$credentials = $request->only('email', 'password');

Auth::attempt($credentials)

You can create custom guard as well in Laravel, e.g created admin guard and respective model for authentication

App\Admin

$credentials = $request->only('email', 'password');

// specify guard name for custom guard

Auth::guard('admin')->attempt($credentials)

https://laravel.com/docs/7.x/authentication#adding-custom-guards

like image 37
Aniket Muruskar Avatar answered Mar 05 '23 15:03

Aniket Muruskar