diff options
Diffstat (limited to 'src/controllers/UserTokenController.php')
-rw-r--r-- | src/controllers/UserTokenController.php | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/controllers/UserTokenController.php b/src/controllers/UserTokenController.php new file mode 100644 index 0000000..20bd06c --- /dev/null +++ b/src/controllers/UserTokenController.php @@ -0,0 +1,51 @@ +<?php +namespace CamilStaps\BotleaguesApi\Controllers; + +use CamilStaps\BotleaguesApi\Database\UserToken; +use Illuminate\Support\Facades\Input; +use Illuminate\Support\Facades\Auth; + +class UserTokenController extends BaseController { + + protected $userToken; + + public function __construct(UserToken $userToken) { + $this->userToken = $userToken; + } + + /** + * Only the tokens of the authenticated user are shown + */ + public function index() { + return $this->userToken->where('userId', '=', Auth::user()->id)->get(); + } + + /** + * Only the tokens of the authenticated user are available + */ + public function show($id) { + return $this->userToken->where('userId', '=', Auth::user()->id)->findOrFail($id); + } + + /** + * Set the userId and create a random token + */ + public function store() { + $this->userToken->userId = Auth::user()->id; + $this->userToken->token = base64_encode(openssl_random_pseudo_bytes(64)); + + if ($this->userToken->save()) { + // Remove the token field from the hidden fields + $hidden = $this->userToken->getHidden(); + foreach ($hidden as $k => $v) + if ($v == 'token') + unset($hidden[$k]); + $this->userToken->setHidden($hidden); + + return $this->userToken; + } else { + throw new \Dingo\Api\Exception\StoreResourceFailedException; + } + } + +}
\ No newline at end of file |