Laravel: Inject Auth Intensity

How can I add to Authentication in Laravel?

Like this:

public function __construct(Auth $auth) { $this->auth = $auth; } 

If I do this, this will not work:

 $user_type = Auth::user()->user_type; 
+5
source share
2 answers

You should enter the prompt Illuminate\Auth\AuthManager :

 public function __construct(Illuminate\Auth\AuthManager $auth) { $this->auth = $auth; } 
+6
source

If you want to add Auth, you really need to introduce this class:

 use Illuminate\Contracts\Auth\Guard; 

This will solve everything that you defined inside:

 config/auth.php 

If you want to extend Auth, you can do this, but only for:

  • The protection driver, which is a protection class - it needs to implement the Guard or StatefulGuard interface.

  • The provider, which is the UserProvider class, needs to implement the UserProvider interface.

Standard Guard Guard drivers in Laravel / Lumen:

  • Session session
  • Tokenguard

Standard Auth UserProviders users are in Laravel / Lumen:

  • EloquentUserProvider
  • DatabaseUserProvider

You can read more about the Auth extension in the official Laravel documentation. See the link below:

https://laravel.com/docs/5.0/extending#authentication

This is the code I have in my controller and it works like a charm:

  public function createToken(Request $request, Guard $guard) { // return 'in progress...'; } 

Best practice for extending the Auth class is the ServiceProvider () load method.

Hope this helps!

Greetings.

+3
source

Source: https://habr.com/ru/post/1206251/


All Articles