Skip to content

Commit 7806718

Browse files
chrbayerphillwiggins
authored andcommitted
Add support for getting current session (#104)
1 parent f4ccb7a commit 7806718

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

lib/parse_server_sdk.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ part 'src/objects/parse_response.dart';
4848

4949
part 'src/objects/parse_user.dart';
5050

51+
part 'src/objects/parse_session.dart';
52+
5153
part 'src/objects/parse_installation.dart';
5254

5355
part 'src/utils/parse_decoder.dart';

lib/src/base/parse_constants.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const String keyEndPointUserName = '/users/me';
99
const String keyEndPointLogin = '/login';
1010
const String keyEndPointLogout = '/logout';
1111
const String keyEndPointUsers = '/users';
12+
const String keyEndPointSessions = '/sessions';
1213
const String keyEndPointVerificationEmail = '/verificationEmailRequest';
1314
const String keyEndPointRequestPasswordReset = '/requestPasswordReset';
1415
const String keyEndPointClasses = '/classes/';
@@ -28,6 +29,7 @@ const String keyVarAcl = 'ACL';
2829
// Classes
2930
const String keyClassMain = 'ParseMain';
3031
const String keyClassUser = '_User';
32+
const String keyClassSession = '_Session';
3133
const String keyClassInstallation = '_Installation';
3234
const String keyGeoPoint = 'GeoPoint';
3335
const String keyFile = 'File';

lib/src/objects/parse_session.dart

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
part of flutter_parse_sdk;
2+
3+
class ParseSession extends ParseObject implements ParseCloneable {
4+
@override
5+
clone(Map map) {
6+
print(map);
7+
return this.fromJson(map);
8+
}
9+
10+
static final String keyVarUser = 'user';
11+
static final String keyVarCreatedWith = 'createdWith';
12+
static final String keyVarRestricted = 'restricted';
13+
static final String keyVarExpiresAt = 'expiresAt';
14+
static final String keyVarInstallationId = 'installationId';
15+
16+
String get sessionToken => super.get<String>(keyVarSessionToken);
17+
18+
ParseObject get user => super.get<ParseObject>(keyVarUser);
19+
20+
Map<String, dynamic> get createdWith =>
21+
super.get<Map<String, dynamic>>(keyVarCreatedWith);
22+
23+
bool get restricted => super.get<bool>(keyVarRestricted);
24+
25+
DateTime get expiresAt => super.get<DateTime>(keyVarExpiresAt);
26+
27+
String get installationId => super.get<String>(keyVarInstallationId);
28+
29+
ParseSession({String sessionToken, bool debug, ParseHTTPClient client})
30+
: super(keyClassSession) {
31+
_debug = isDebugEnabled(objectLevelDebug: debug);
32+
_client = client ??
33+
ParseHTTPClient(
34+
autoSendSessionId: true,
35+
securityContext: ParseCoreData().securityContext);
36+
}
37+
38+
Future<ParseResponse> getCurrentSessionFromServer() async {
39+
try {
40+
Uri tempUri = Uri.parse(_client.data.serverUrl);
41+
42+
Uri url = Uri(
43+
scheme: tempUri.scheme,
44+
host: tempUri.host,
45+
path: "${tempUri.path}$keyEndPointSessions/me");
46+
47+
final response = await _client.get(url);
48+
49+
return _handleResponse(
50+
this, response, ParseApiRQ.logout, _debug, className);
51+
} on Exception catch (e) {
52+
return _handleException(e, ParseApiRQ.logout, _debug, className);
53+
}
54+
}
55+
56+
/// Handles an API response and logs data if [bool] debug is enabled
57+
static ParseResponse _handleException(
58+
Exception exception, ParseApiRQ type, bool debug, String className) {
59+
ParseResponse parseResponse = ParseResponse.handleException(exception);
60+
61+
if (debug) {
62+
logger(
63+
ParseCoreData().appName, className, type.toString(), parseResponse);
64+
}
65+
66+
return parseResponse;
67+
}
68+
69+
/// Handles all the response data for this class
70+
static ParseResponse _handleResponse(ParseSession session, Response response,
71+
ParseApiRQ type, bool debug, String className) {
72+
ParseResponse parseResponse =
73+
ParseResponse.handleResponse<ParseSession>(session, response);
74+
75+
if (debug) {
76+
logger(
77+
ParseCoreData().appName, className, type.toString(), parseResponse);
78+
}
79+
80+
return parseResponse;
81+
}
82+
}

0 commit comments

Comments
 (0)