Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
I have a question regarding simpleAuth
  • Hello there,

    I have 2 questions.

    ・First question.

    Is it possible to add another input value to authenticate?

    For example, currently required values for authentication is ID and password only and I want to add one more input value to authenticate so that ID can be repeated for user management.

    ・Second question
    Is it possible to handle multiple sessions using simpleAuth like following?


    Best regards,
  • 1. Not out of the box.

    You will need create your own Auth class, by extending the Auth_Login_Simpleauth class, and replace the perform_check(), login(), logout() and validate_user() methods.

    2. Not out of the box.

    Auth supports multiple driver instances, so technically it is possible, but the included Auth options (Simple and ORM) don't support it. So you need to do the same as above, and make sure that you include the instance id. The instance 'id' is an optional field in the auth config, if not given it defaults to the value of 'driver'. In the Login class you can access it using $this->get_id();

    Once you have the id, you have to use it to make the session variables unique. So for example where it now uses

    \Session::set('username', ...) or \Session::get('username');

    it should use something like

    \Session::set($this->get_id().'-username', ...) or \Session::get($this->get_id().'-username');

    to make them unique.

    Once done, you can forge multiple auth instances.
  • Hello there,

    Thank you for your reply.

    Yes, that is what I expect actually.

    It seems I might have to make a lot of thing.

    For second question, do you mean that I might have to extend the Auth_Login_Simpleauth class?

    If so, it is possible that I might have to make method() for changing user.

    Am I correct?
  • HarroHarro
    Accepted Answer
    You have to do that for both.

    There are two options, either write your own driver (using the Auth_Login_Simpleauth class as a template), or extend Auth_Login_Simpleauth, and overload the methods you need to overload.

    Which one is best depends on how much you need to replace.

    To give an example, we have an app that uses "Customauth". It is in app/classes/auth/login/customauth.php, and defines:

    class Auth_Login_Customauth extends \Auth\Auth_Login_Simpleauth {}

    The app/bootloader.php contains:

    Autoloader::add_classes(array(
    // our custom auth classes
    'Auth_Login_Customauth' => APPPATH.'classes/auth/login/customauth.php',
    ));

    so that the framework knows it exists.

    The app/config/auth.php configuration file then contains

    'driver' => 'Customauth',

    which tells Auth to load it.

    So when you use Auth::forge(), you get a Customauth instance back.

    To create multiple instances, you can do:

    $auth1 = Auth::forge(array('id' => 'auth1');
    $auth2 = Auth::forge(array('id' => 'auth2');

    (obviously passing additional config data in the array if and when needed)

    In your driver, $this->get_id() would then return either 'auth1' or 'auth2', depending on the instance you used. You can then use that to make sure session values like username and stored per driver.
  • Hello, 

    Thank you very much for providing good example.

    I guess I do not have to modify entire class.

    So, I might just extend it.

    Now I have custom simpleauth class and ready for modify.

    Thanks :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion