Merge pull request #1294 from PineappleIOnic/feat-get-session-by-id-endpoint
Add session by ID API Endpoint
This commit is contained in:
commit
91b2a139a5
4 changed files with 93 additions and 5 deletions
|
@ -20,6 +20,7 @@
|
||||||
- Added new environment variables to the `telegraf` service: (#1202)
|
- Added new environment variables to the `telegraf` service: (#1202)
|
||||||
- _APP_INFLUXDB_HOST
|
- _APP_INFLUXDB_HOST
|
||||||
- _APP_INFLUXDB_PORT
|
- _APP_INFLUXDB_PORT
|
||||||
|
- Added new endpoint to get a session based on it's ID (#1294)
|
||||||
|
|
||||||
## Breaking Changes (Read before upgrading!)
|
## Breaking Changes (Read before upgrading!)
|
||||||
- Renamed `env` param on `/v1/functions` to `runtime` (#1314)
|
- Renamed `env` param on `/v1/functions` to `runtime` (#1314)
|
||||||
|
|
|
@ -254,9 +254,13 @@ App::post('/v1/account/sessions')
|
||||||
|
|
||||||
$countries = $locale->getText('countries');
|
$countries = $locale->getText('countries');
|
||||||
|
|
||||||
|
$countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))]))
|
||||||
|
? $countries[strtoupper($session->getAttribute('countryCode'))]
|
||||||
|
: $locale->getText('locale.country.unknown');
|
||||||
|
|
||||||
$session
|
$session
|
||||||
->setAttribute('current', true)
|
->setAttribute('current', true)
|
||||||
->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))])) ? $countries[strtoupper($session->getAttribute('countryCode'))] : $locale->getText('locale.country.unknown'))
|
->setAttribute('countryName', $countryName)
|
||||||
;
|
;
|
||||||
|
|
||||||
$response->dynamic($session, Response::MODEL_SESSION);
|
$response->dynamic($session, Response::MODEL_SESSION);
|
||||||
|
@ -753,9 +757,13 @@ App::post('/v1/account/sessions/anonymous')
|
||||||
->setStatusCode(Response::STATUS_CODE_CREATED)
|
->setStatusCode(Response::STATUS_CODE_CREATED)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
$countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))]))
|
||||||
|
? $countries[strtoupper($session->getAttribute('countryCode'))]
|
||||||
|
: $locale->getText('locale.country.unknown');
|
||||||
|
|
||||||
$session
|
$session
|
||||||
->setAttribute('current', true)
|
->setAttribute('current', true)
|
||||||
->setAttribute('countryName', (isset($countries[$session->getAttribute('countryCode')])) ? $countries[$session->getAttribute('countryCode')] : $locale->getText('locale.country.unknown'))
|
->setAttribute('countryName', $countryName)
|
||||||
;
|
;
|
||||||
|
|
||||||
$response->dynamic($session, Response::MODEL_SESSION);
|
$response->dynamic($session, Response::MODEL_SESSION);
|
||||||
|
@ -878,9 +886,11 @@ App::get('/v1/account/sessions')
|
||||||
foreach ($sessions as $key => $session) {
|
foreach ($sessions as $key => $session) {
|
||||||
/** @var Document $session */
|
/** @var Document $session */
|
||||||
|
|
||||||
$session->setAttribute('countryName', (isset($countries[strtoupper($session->getAttribute('countryCode'))]))
|
$countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))]))
|
||||||
? $countries[strtoupper($session->getAttribute('countryCode'))]
|
? $countries[strtoupper($session->getAttribute('countryCode'))]
|
||||||
: $locale->getText('locale.country.unknown'));
|
: $locale->getText('locale.country.unknown');
|
||||||
|
|
||||||
|
$session->setAttribute('countryName', $countryName);
|
||||||
$session->setAttribute('current', ($current == $session->getId()) ? true : false);
|
$session->setAttribute('current', ($current == $session->getId()) ? true : false);
|
||||||
|
|
||||||
$sessions[$key] = $session;
|
$sessions[$key] = $session;
|
||||||
|
@ -968,6 +978,47 @@ App::get('/v1/account/logs')
|
||||||
$response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
|
$response->dynamic(new Document(['logs' => $output]), Response::MODEL_LOG_LIST);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
App::get('/v1/account/sessions/:sessionId')
|
||||||
|
->desc('Get Session By ID')
|
||||||
|
->groups(['api', 'account'])
|
||||||
|
->label('scope', 'account')
|
||||||
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_JWT])
|
||||||
|
->label('sdk.namespace', 'account')
|
||||||
|
->label('sdk.method', 'getSession')
|
||||||
|
->label('sdk.description', '/docs/references/account/get-session.md')
|
||||||
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
||||||
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
||||||
|
->label('sdk.response.model', Response::MODEL_SESSION)
|
||||||
|
->param('sessionId', null, new UID(), 'Session unique ID. Use the string \'current\' to get the current device session.')
|
||||||
|
->inject('response')
|
||||||
|
->inject('user')
|
||||||
|
->inject('locale')
|
||||||
|
->inject('projectDB')
|
||||||
|
->action(function ($sessionId, $response, $user, $locale, $projectDB) {
|
||||||
|
/** @var Appwrite\Utopia\Response $response */
|
||||||
|
/** @var Appwrite\Database\Document $user */
|
||||||
|
/** @var Utopia\Locale\Locale $locale */
|
||||||
|
/** @var Appwrite\Database\Database $projectDB */
|
||||||
|
|
||||||
|
$sessionId = ($sessionId === 'current')
|
||||||
|
? Auth::sessionVerify($user->getAttribute('sessions'), Auth::$secret)
|
||||||
|
: $sessionId;
|
||||||
|
|
||||||
|
$session = $projectDB->getDocument($sessionId); // get user by session ID
|
||||||
|
|
||||||
|
if ($session->isEmpty() || Database::SYSTEM_COLLECTION_SESSIONS != $session->getCollection()) {
|
||||||
|
throw new Exception('Session not found', 404);
|
||||||
|
};
|
||||||
|
|
||||||
|
$countryName = (isset($countries[strtoupper($session->getAttribute('countryCode'))]))
|
||||||
|
? $countries[strtoupper($session->getAttribute('countryCode'))]
|
||||||
|
: $locale->getText('locale.country.unknown');
|
||||||
|
|
||||||
|
$session->setAttribute('countryName', $countryName);
|
||||||
|
|
||||||
|
$response->dynamic($session, Response::MODEL_SESSION);
|
||||||
|
});
|
||||||
|
|
||||||
App::patch('/v1/account/name')
|
App::patch('/v1/account/name')
|
||||||
->desc('Update Account Name')
|
->desc('Update Account Name')
|
||||||
->groups(['api', 'account'])
|
->groups(['api', 'account'])
|
||||||
|
|
1
docs/references/account/get-session.md
Normal file
1
docs/references/account/get-session.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Use this endpoint to get a logged in user's session using a Session ID. Inputting 'current' will return the current session being used.
|
|
@ -424,4 +424,39 @@ class AccountCustomClientTest extends Scope
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testGetSessionByID() {
|
||||||
|
$session = $this->testCreateAnonymousAccount();
|
||||||
|
|
||||||
|
$response = $this->client->call(Client::METHOD_GET, '/account/sessions/current', array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($response['headers']['status-code'], 200);
|
||||||
|
$this->assertEquals($response['body']['provider'], 'anonymous');
|
||||||
|
|
||||||
|
$sessionID = $response['body']['$id'];
|
||||||
|
|
||||||
|
$response = $this->client->call(Client::METHOD_GET, '/account/sessions/'.$sessionID, array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($response['headers']['status-code'], 200);
|
||||||
|
$this->assertEquals($response['body']['provider'], 'anonymous');
|
||||||
|
|
||||||
|
$response = $this->client->call(Client::METHOD_GET, '/account/sessions/97823askjdkasd80921371980', array_merge([
|
||||||
|
'origin' => 'http://localhost',
|
||||||
|
'content-type' => 'application/json',
|
||||||
|
'x-appwrite-project' => $this->getProject()['$id'],
|
||||||
|
'cookie' => 'a_session_'.$this->getProject()['$id'].'=' . $session,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->assertEquals($response['headers']['status-code'], 404);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue