Book Image

Yii Project Blueprints

By : Charles R. Portwood ll
Book Image

Yii Project Blueprints

By: Charles R. Portwood ll

Overview of this book

Table of Contents (15 chapters)
Yii Project Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Authenticating users


Now that our API is functional, let's add the ability for users to be authenticated against our API. For this, we're going to create an endpoint that accepts the following JSON request body:

{
   "email": "[email protected]",
   "password": "<example_password>"
}

With this information, the API will be authenticated using LoginForm, which we worked on in previous chapters. If the user is valid, we'll generate a new API token that will be stored in the user_metadata table. This token will be returned to the client who is making the request and will be used to authenticate for all future requests:

  1. To get started, create a new controller in protected/modules/api/controllers/ called UserController.php with the following definition:

    <?php class UserController extends ApiController {}
  2. Next, we'll need to define a default set of access rules so as to allow our authentication method to be used without authentication:

    public function accessRules()
    {
        return array(
          ...