Authenticate API with Username and Password

In Cockpit you can create (file based) custom api end points pretty easy within the config folder.

Create a file (with folder path) in the root folder:

config/api/user/auth.php

with the following contents:


<?php

$user = $this->param('user');
$password = $this->param('password');

if (!$user || !$password) {
    return $this->stop(['error' => 'User and password are required'], 412);
}

$user = $this->helper('auth')->authenticate(compact('user', 'password'));

if (!$user) {
  return $this->stop(['error' => 'Authentication failed!'], 412);
}

return ['apiKey' => $user['apiKey']];


Now you can run requests against /api/user/auth

2 Likes