This is the laravel api auth system intended for the longman/telegram-bot library. Important! This library uses the californiamountainsnake/simple-laravel-auth-system library, you must install and config one first.
Install this package through Composer.
Edit your project's composer.json
file to require californiamountainsnake/longmantelegrambot-laravel-api-auth-system
:
{
"name": "yourproject/yourproject",
"type": "project",
"require": {
"php": "^7.2",
"californiamountainsnake/longmantelegrambot-laravel-api-auth-system": "*"
}
}
and run composer update
run this command in your command line:
composer require californiamountainsnake/longmantelegrambot-laravel-api-auth-system
- Extend the abstract classes
AuthTelegrambotUserEntity
andAuthTelegrambotUserRepository
. - Include the
AuthBotAccessUtils
,AuthApiUtils
andAuthUserUtils
traits into your base Command class and realise the abstract methods. Set the correct return type hints in the phpdoc for the methodsgetUserEntity()
,getUserRole()
andgetUserAccountType()
.
<?php
class BaseCommand extends Command {
use AuthBotAccessUtils;
use AuthApiUtils;
use AuthUserUtils;
/**
* Get user's entity. Just specify return type hint.
* @return UserEntity|null
*/
protected function getUserEntity(): ?AuthUserEntity
{
return $this->userUtilsGetUserEntity();
}
/**
* Get user's role. Just specify return type hint.
* @return UserRoleEnum
*/
protected function getUserRole(): AuthUserRoleEnum
{
return $this->userUtilsGetUserRole();
}
/**
* Get user's account type. Just specify return type hint.
* @return UserAccountTypeEnum
*/
protected function getUserAccountType(): AuthUserAccountTypeEnum
{
return $this->userUtilsGetUserAccountType();
}
}
- Call
$this->initTelegramParams()
,$this->reInitUserParams()
and then$this->assertUserHasAccessToMainRoute()
in thepreExecute()
method and handle the exceptions:
<?php
class BaseCommand extends Command {
use AuthBotAccessUtils;
use AuthApiUtils;
use AuthUserUtils;
public function preExecute(): ServerResponse
{
$this->initTelegramParams();
$this->reInitUserParams();
try {
// If user try to execute a wrong command, the exception will throw.
$this->assertUserHasAccessToMainRoute($this->getUserEntity());
return parent::preExecute();
} catch (ApiProxyException $e) {
// handle the error!
} catch (UserRoleNotEqualsException $e) {
// handle the error!
} catch (UserAccountTypeNotEqualsException $e) {
// handle the error!
} catch (\Throwable $t) {
// A good idea is to handle other exceptions.
}
}
}
- Use AuthApiUtils's methods to execute queries to your laravel api:
<?php
class MyCommand extends BaseCommand {
public function execute (): ServerResponse {
$json = $this->callApiNotAuth($this->getMainRoute(), [
'email' => 'new@email.com',
])->getContent();
}
}
- If you want, you can realise the ApiProxyInterface and process api queries as you prefer.