openidyiilightopenidopenid-selector

How do I login the user with Yii and OpenID


I have added simpleopenidselector and lightopenid to my Yii web app and it does authenticate the user and it returns a url with openid data. The next step is to use the data from the OpenID provider to create a new identity in Yii to log in the user. How is this done with Yii?

Also, I think I need to create an openid table in order to store the openid's and also need to add the user to my user table. If the user has an account already then add the openid to their user account to prevent multiple accounts.

Has anyone achieved all of this with Yii? If so, I would be very interested in how it was accomplished.


Solution

  • To use the data from openID as Yii login, you could modify/overwrite the UserIdentity Class (protected/components).

    Overwrite the existing authenticate method. At this point you also can set the current Yii username, like:

    $this->username=$openId->username
    

    (where $openId->username should be replaced by the variable which contains the openID user name)

    By overwriting the side/login action, you can call your modified method, like this:

    $identity=new UserIdentity("m","m");//where m is dummy
    if($identity->authenticate()) {
               Yii::app()->user->login($identity);
    [...]
    }
    

    //Update (because of your comment): Not sure, if I understand your problem right. But what's about adding a new method in UserIdentity, like authenticateOID(). call this method at the beginning of the original authenticate() method, like so:

    if ($this->authenticateOID) {/*set username & return true to end the method here*/}
    else {/*original authenticate method from Yii*/}
    

    Inside authenticateOID() you check if OID authentication is done and/or if the user still in you local "OID - user table"