Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 86164257 authored by Akhil's avatar Akhil
Browse files

Add POST call to add username to common data store

parent c7166395
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -142,6 +142,7 @@ class AccountController extends Controller {
		}

		$this->session->remove(self::SESSION_USERNAME_CHECK);
		$this->userService->addUsernameToCommonDataStore($username);
		return $response;
	}
	/**
+32 −0
Original line number Diff line number Diff line
@@ -418,4 +418,36 @@ class UserService {

		throw new Exception("Error checking if username is taken at common source, status code: " . (string) $statusCode);
	}

	public function addUsernameToCommonDataStore(string $username) : void {
		$commonApiUrl = $this->apiConfig['commonApiUrl'];
		$commonApiVersion = $this->apiConfig['commonApiVersion'];

		if (!isset($commonApiUrl) || empty($commonApiUrl)) {
			return;
		}
		$endpoint = $commonApiVersion . '/users/';
		$url = $commonApiUrl . $endpoint ;
		
		$params = [
			'username' => $username
		];

		$token = $this->apiConfig['commonServiceToken'];
		$headers = [
			"Authorization: Bearer $token"
		];
		
		try {
			$this->curl->post($url, $params, $headers);

			if ($this->curl->getLastStatusCode() !== 200) {
				throw new Exception();
			}
		} catch (Exception $e) {
			$this->logger->error('Error adding username ' . $username . ' to common data store');
		}
		

	}
}