How to create user via rest api and authenticate them ?
Create a file: cockpit/config/api/user/register.php
<?php
$uname = $this->param('name');
$username = $this->param('username');
$email = $this->param('email');
$password = $this->param('password');
$role = $this->param('role');
$created= time();
$app = Cockpit::instance();
$checkuser = $app->dataStorage->findOne('system/users', ['user' => $username]);
$checkemail = $app->dataStorage->findOne('system/users', ['user' => $email]);
if (!$checkuser && !$checkemail) {
$user = [
'active' => true,
'name' => $uname,
'user' => $username,
'email' => $email,
'password' => $app->hash('$password'),
'i18n' => 'en',
'role' => $role,
'theme' => 'auto',
'_modified' => $created,
'_created' => $created
];
$app->dataStorage->save('system/users', $user);
return [
'status' => 'success'];
}
else{
if($checkuser){return $this->stop(['error' => 'username already exist'], 412);}
if($checkemail){return $this->stop(['error' => 'email already exist'], 412);}
}
?>
Now from your app use curl to send relevant user registration data to route cockpit/api/user/auth
Tweak the above code as per as your convenience. I hope it helps. Cheers
1 Like
works for me … thanks