Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bononlines's avatar

Use a different methode than database/eloquent for Auth the user

Hi,

First of all, what I am planning to reach is to write a custom auth class to auth my users without any database. Lets see I would like to load the users from a single file, or from another API calls. So do not need to use database, or eloquent for getting my users authorized.

I have seen the laravel documentation and found this page to write a customer user providers

https://laravel.com/docs/6.x/authentication#adding-custom-user-providers

But I really am stuck in this and can not get it working.

    Auth::provider('riak', function ($app, array $config) {
        // Return an instance of Illuminate\Contracts\Auth\UserProvider...

        return new RiakUserProvider($app->make('riak.connection'));
    });

What should be riak.connection ? I know riak is another service like database, but I need to use a custom one. What should I return in this function instead of return new RiakUserProvider($app->make('riak.connection')); ?

Thanks for your help

0 likes
7 replies
jlrdw's avatar

For an API you would still have tokens.

You should probably take a API tutorial.

And read the chapter in the docs.

bononlines's avatar

Hi,

do you mean the API chapter in the documentation https://laravel.com/docs/6.x/eloquent-resources?

It has nothing to do with that what I have requested and what I want to create.

If you have read my request I need to create custom auth without a database and or eloquent. So let's say I have my users all in one text file and I want to read the file once an user try to login. His username and password is saved into a text file and not a database.

I think I am clear enough about this.

jlrdw's avatar

Then just read the text file, verify that user is in the file, and get them logged in, I imagine you are just going to set something in session that they are logged in.

https://www.php.net/manual/en/function.fgetcsv.php is one resource, but there are other methods in php, I don't know how you are reading the text (csv) file.

Edit: Also see https://stackoverflow.com/questions/41855856/why-i-cant-find-the-app-extensions-riakuserprovider-namespace-in-my-laravel-p

bononlines's avatar

That is exactly what I need. To set a session that the current client is logged in. However, the main question is, where exactly should I read the text file? and where do I have to check the session if it is already set and the user is logged in?

Do I have to add a login() function to the loginController class and try to check the login information there? If the user exist in the file, I should then set a User and return?

jlrdw's avatar

You can put the text file in an array, and use in_array.

User enters name and password, then you call a custom method to verify the name (email, whatever field used) matches the password for that csv line then use session.

However, I'd suggest also looking at past answers on this. Copy and paste this in google and see the results.

site:laracasts.com custom authentication provider

or try

site:laracasts.com setting up a custom authentication provider

And just curious, why not use out of box regular authentication, it works so well.

jlrdw's avatar

Well sorry, searching was just a suggestion, I had no idea if any past answers would help.

And this is also just a suggestion:

I do not know if the passwords you have are hashed, but you may consider having a pin, like 15784 for example. And something like:

            $hashedPassword = $users->password;
            if (password_verify($password, $hashedPassword)) {
                Session::put('loggin', 'islogged');
                return redirect('place to redirect if login was good');
            } else {
                return redirect('place to redirect if login was bad');
            }

Or just pseudocode

           $name= //  get the entered name from request
           $pw = //  get the entered password from request or pin
           $password = // the password from text file or pin
           // somewhere here call a method to get it
           // to match with entered data.
           if ($password === $pw  )) {
                Session::put('loggin', 'islogged');
                return redirect('place to redirect if login was good');
            } else {
                return redirect('place to redirect if login was bad');
            }

But I have written custom auth in cakphp, but it still stored users in a database.

But the goal is making sure some stored credentials (text file in your case) matches what the user entered.

At least consider a pin, might be easier.

Also Riak in docs is just an example, Riak is a NoSQL database.

And just so you know, we here are not paid support, just forum members that try to help.

So it's up to you, but I strongly suggest (just a suggestion only) using the authentication in laravel the way it's meant to be used. It is easy and secure once you learn it.

Jeffrey has free video training on the basics.

Please or to participate in this conversation.