O'Reilly logo

Yii Project Blueprints by Charles R. Portwood II

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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": "user@example.com",
   "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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required