Skip to content

Add support for getting current session #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/parse_server_sdk.dart
Original file line number Diff line number Diff line change
@@ -48,6 +48,8 @@ part 'src/objects/parse_response.dart';

part 'src/objects/parse_user.dart';

part 'src/objects/parse_session.dart';

part 'src/objects/parse_installation.dart';

part 'src/utils/parse_decoder.dart';
2 changes: 2 additions & 0 deletions lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const String keyEndPointUserName = '/users/me';
const String keyEndPointLogin = '/login';
const String keyEndPointLogout = '/logout';
const String keyEndPointUsers = '/users';
const String keyEndPointSessions = '/sessions';
const String keyEndPointVerificationEmail = '/verificationEmailRequest';
const String keyEndPointRequestPasswordReset = '/requestPasswordReset';
const String keyEndPointClasses = '/classes/';
@@ -28,6 +29,7 @@ const String keyVarAcl = 'ACL';
// Classes
const String keyClassMain = 'ParseMain';
const String keyClassUser = '_User';
const String keyClassSession = '_Session';
const String keyClassInstallation = '_Installation';
const String keyGeoPoint = 'GeoPoint';
const String keyFile = 'File';
82 changes: 82 additions & 0 deletions lib/src/objects/parse_session.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
part of flutter_parse_sdk;

class ParseSession extends ParseObject implements ParseCloneable {
@override
clone(Map map) {
print(map);
return this.fromJson(map);
}

static final String keyVarUser = 'user';
static final String keyVarCreatedWith = 'createdWith';
static final String keyVarRestricted = 'restricted';
static final String keyVarExpiresAt = 'expiresAt';
static final String keyVarInstallationId = 'installationId';

String get sessionToken => super.get<String>(keyVarSessionToken);

ParseObject get user => super.get<ParseObject>(keyVarUser);

Map<String, dynamic> get createdWith =>
super.get<Map<String, dynamic>>(keyVarCreatedWith);

bool get restricted => super.get<bool>(keyVarRestricted);

DateTime get expiresAt => super.get<DateTime>(keyVarExpiresAt);

String get installationId => super.get<String>(keyVarInstallationId);

ParseSession({String sessionToken, bool debug, ParseHTTPClient client})
: super(keyClassSession) {
_debug = isDebugEnabled(objectLevelDebug: debug);
_client = client ??
ParseHTTPClient(
autoSendSessionId: true,
securityContext: ParseCoreData().securityContext);
}

Future<ParseResponse> getCurrentSessionFromServer() async {
try {
Uri tempUri = Uri.parse(_client.data.serverUrl);

Uri url = Uri(
scheme: tempUri.scheme,
host: tempUri.host,
path: "${tempUri.path}$keyEndPointSessions/me");

final response = await _client.get(url);

return _handleResponse(
this, response, ParseApiRQ.logout, _debug, className);
} on Exception catch (e) {
return _handleException(e, ParseApiRQ.logout, _debug, className);
}
}

/// Handles an API response and logs data if [bool] debug is enabled
static ParseResponse _handleException(
Exception exception, ParseApiRQ type, bool debug, String className) {
ParseResponse parseResponse = ParseResponse.handleException(exception);

if (debug) {
logger(
ParseCoreData().appName, className, type.toString(), parseResponse);
}

return parseResponse;
}

/// Handles all the response data for this class
static ParseResponse _handleResponse(ParseSession session, Response response,
ParseApiRQ type, bool debug, String className) {
ParseResponse parseResponse =
ParseResponse.handleResponse<ParseSession>(session, response);

if (debug) {
logger(
ParseCoreData().appName, className, type.toString(), parseResponse);
}

return parseResponse;
}
}