Login

\Modules\Api\User\Login

Introduction

For third-party Oauth2 providers, you must install the corresponding sdk within the application that will allow you to retrieve the access_token, then send as parameter to Login:post()

Do not directly use the access_token on the other views of your application: it is not the same as the token generated by the Login:post() method, you must necessarily go through this method, in this way each provider is housed in the same brand and is treated similarly by our platform.

πŸ“˜

Note

Regarding native accounts, before using Login:post() the user must have an account having used a first time Login:register(), otherwise the API will return the error user_not_found

Methods

post

<?php public static function \Modules\Api\User\Login::post($login = null, $password = null, $access_token = null, $source = 'native')

Description
Login the user.

Parameters

  • login
    User's login
  • password (optional)
    User's password (if it's an native account)
  • access_token (optional)
    User access token from a third-party provider
    (To recover by the sdk installed application side corresponding to the provider)
  • source (optional)
    Type of authentification (default : native)

Return

{
  "id": 1,
  "first_name": "my firstname",
  "last_name": "my lastname",
  "username": "username",
  "picture": null,
  "company": "Apps Panel",
  "phone": "0600000000",
  "last_updated_at": null,
  "status": 1,
  "bo_access": 1,
  "api_access": 1,
  "created_at": "2018-05-22 11:02:39",
  "token": "e359ec9f4c674f1aafdc66b2483fe5ad"
}

🚧

important

A token has returned to you as the last field of the object. This token has been generated for the user, and it is this one that you should check on every view of your application that is protected by user authentication. (see Token page)

Example

Here is a simple example of what your login webservice might look like.

Here, we try to recover a password and access_token, so it assumes that at least 2 types of authentication are enabled (the native and at least one Oauth2)

<?php

namespace App\Modules\Users;

use \Lib\Controller\WSController;
use \Modules\Api\User\Login as LoginController;

/**
 * Class Login
 * @package App\Modules\Users
 */
class Login extends \Modules\Elements
{

    /**
     * @description Log in the user.
     * @param WSController $controller
     * @param array $params
     * @doc string $login user's login
     * @doc string $password user's password(optional)
     * @doc string $access_token user access token from a third-party provider (optional)
     * @doc string $source type of authentication @default "native" (optional)
     * @return array
     */
    public static function post(WSController $controller, $params = array())
    {

        $post = \Lib\DataRequest::getInstance()->get('post');

        $access_token = !empty($_SERVER['AP-UTOKEN']) ? $_SERVER['AP-UTOKEN'] : $post['token'];
        $password = !empty($_SERVER['AP-SEC-DATA']['password']) ? $_SERVER['AP-SEC-DATA']['password'] : $post['password'];
        $source = !empty($post['source']) ? $post['source'] : 'native';

        return LoginController::post($post['login'], $password, $access_token, $source);
    }
}