I’m using Luracast restler and i’m trying to implement some authentication by implementing iAuthenticate interface.
The thing is, my authentication code needs to query my database to retrieve the user private key. This private key will always be provided in the url request (hashed).
I wanted to open just one database connection to each request, so i need to pass the db connection variable to my class that implements iAuthenticate and to the other classes that handle all the requests. But i can’t figure out how can i pass variables to my class that implements iAuthenticate.
Is it possible?
For reference, here are the luracast examples
thks in advance.
Using Single DB Connection for your API and Authentication Classes
Create a php file called
config.phpand place all your db information along with db connection and selection.For example
Include this function using
require_onceon both Authentication class and API class, something like (for simplicity I’m not encrypting the password here)Your API class can have a protected method that query the same db, it can be a different table that return the data using the same connection. For simplicity sake I use the same table here.
Using
require_oncemakes sure that the php file is included only once on the first encounter. Even if we stop using the auth class latter our api will keep functioningAssuming that following SQL is used to create our db table
And the index.php with the following
The Result
if you open
index.php/restrictedin the browser and key in the right username and password combination, you will see the following as the result 🙂