0
votes

I have a laravel API project. I want to be able to send a login request and get back a token depending on some custom logic. I am not using a database so i cant use the default auth.

I have set up a provider called AuthCustomProvider.

namespace App\Providers;
use Auth;
use App\Authentication\UserProvider;
use Illuminate\Support\ServiceProvider;

class AuthCustomProvider extends ServiceProvider
{
    /**
     * Perform post-registration booting of services.
     *
     * @return  void
     */
    public function boot()
    {
        Auth::provider('custom_auth', function($app, array $config) {
            return new UserProvider();
        });
    }
    /**
     * Register bindings in the container.
     *
     * @return  void
     */
    public function register()
    {
        //
    }
}

I have then added this to the config/app.php file in the providers array:

'providers' => [

    App\Providers\AuthCustomProvider::class,

Then i added my custom providers driver to the config/auth.php file in the providers array:

'providers' => [
   'users' => [
       'driver' => 'custom_auth',
   ],
],

As im not using a database, I took out the model property

Lastly I created a folder called App/Authentication which i put my UserProvider.php file in which is this:

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;

class UserProvider implements IlluminateUserProvider
{
    /**
     * @param    mixed  $identifier
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        // Get and return a user by their unique identifier
    }

    /**
     * @param    mixed   $identifier
     * @param    string  $token
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        // Get and return a user by their unique identifier and "remember me" token
    }

    /**
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    string  $token
     * @return  void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
        // Save the given "remember me" token for the given user
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param    array  $credentials
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        // Get and return a user by looking up the given credentials
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    array  $credentials
     * @return  bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // Check that given credentials belong to the given user
    }

}

So lastly i make a function on the login controller. This is what the api call goes to:

public function Login(Request $request)
{
        $user  = Consultant::lookup('UserId', 1);
        //Returns collection of user details (user id, username etc)

        //Logic will go here in the future
        $logThemIn = true;

        if ($logThemIn)
        {
            auth()->login($user);
            //return oauth2 token
        }
}

So this is where im at now, if i run this, im getting the error:

'Declaration of App\Authentication\UserProvider::updateRememberToken(App\Authentication\Authenticatable $user, $token) must be compatible with Illuminate\Contracts\Auth\UserProvider::updateRememberToken(Illuminate\Contracts\Auth\Authenticatable $user, $token)'

Im new to laravel and there isnt alot of tutorials for what im trying to do that i can find. Any help is greatly appriciated

2

2 Answers

0
votes

Change your UserProvider to this which uses Illuminate\Contracts\Auth\Authenticatable instead of App\Authentication\Authenticatable, php will load a class from the current namespace if one isn't specified.

<?php

namespace App\Authentication;

use Illuminate\Contracts\Auth\UserProvider as IlluminateUserProvider;
use Illuminate\Contracts\Auth\Authenticatable;

class UserProvider implements IlluminateUserProvider
{
    /**
     * @param    mixed  $identifier
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        // Get and return a user by their unique identifier
    }

    /**
     * @param    mixed   $identifier
     * @param    string  $token
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
        // Get and return a user by their unique identifier and "remember me" token
    }

    /**
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    string  $token
     * @return  void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
        // Save the given "remember me" token for the given user
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param    array  $credentials
     * @return  \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        // Get and return a user by looking up the given credentials
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param    \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param    array  $credentials
     * @return  bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        // Check that given credentials belong to the given user
    }
}
0
votes

You forgot to import Authenticatable. Just add:

use Illuminate\Contracts\Auth\Authenticatable;