Skip to content

Fix User objects not saving, Add support for Parse Files, Anonymous Login #67

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
Feb 5, 2019
Merged
Show file tree
Hide file tree
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.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import 'package:meta/meta.dart';
import 'package:path/path.dart' as path;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:web_socket_channel/io.dart';
import 'package:uuid/uuid.dart';
import 'package:path_provider/path_provider.dart';

part 'src/base/parse_constants.dart';

Expand Down
1 change: 1 addition & 0 deletions lib/src/base/parse_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const String keyLibraryName= 'Flutter Parse SDK';
// End Points
const String keyEndPointUserName = '/users/me';
const String keyEndPointLogin = '/login';
const String keyEndPointUsers = '/users';
const String keyEndPointVerificationEmail = '/verificationEmailRequest';
const String keyEndPointRequestPasswordReset = '/requestPasswordReset';
const String keyEndPointClasses = '/classes/';
Expand Down
1 change: 1 addition & 0 deletions lib/src/enums/parse_enum_api_rq.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ enum ParseApiRQ {
currentUser,
signUp,
login,
loginAnonymous,
verificationEmailRequest,
requestPasswordReset,
destroy,
Expand Down
55 changes: 51 additions & 4 deletions lib/src/objects/parse_file.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class ParseFile extends ParseObject {

String get url => _fileUrl;

File get file => _file;

set url(String url) => _fileUrl = url;

set name(String name) => _fileName = name;

bool get saved => url != null;

@override
Expand All @@ -24,12 +30,53 @@ class ParseFile extends ParseObject {
/// Creates a new file
///
/// {https://docs.parseplatform.org/rest/guide/#files/}
ParseFile(this._file, {bool debug, ParseHTTPClient client}) : super (keyFile){
ParseFile(this._file, {String name, String url, bool debug, ParseHTTPClient client}) : super (keyFile){
client == null ? _client = ParseHTTPClient() : _client = client;
_debug = isDebugEnabled(objectLevelDebug: debug);
if(_file != null) {
this._fileName = path.basename(_file.path);
this._path = 'files/$_fileName';
}
else {
this._fileName = name;
this._fileUrl = url;
}
}

Future<ParseFile> loadStorage() async {
Directory tempPath = await getTemporaryDirectory();

if(_fileName == null) {
_file = null;
return this;
}

File possibleFile = new File("${tempPath.path}/$_fileName");
bool exists = await possibleFile.exists();

if(exists) {
_file = possibleFile;
}
else {
_file = null;
}

return this;
}

Future<ParseFile> download() async {
if(_fileUrl == null) {
return this;
}

Directory tempPath = await getTemporaryDirectory();
this._file = new File("${tempPath.path}/$_fileName");
await _file.create();

var response = await _client.get(_fileUrl);
_file.writeAsBytes(response.bodyBytes);

this._fileName = path.basename(_file.path);
this._path = 'files/$_fileName';
return this;
}

/// Uploads a file to Parse Server
Expand All @@ -48,4 +95,4 @@ class ParseFile extends ParseObject {
final response = await _client.post(uri, headers: headers, body: body);
return super.handleResponse<ParseFile>(response, ParseApiRQ.upload);
}
}
}
35 changes: 34 additions & 1 deletion lib/src/objects/parse_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,37 @@ class ParseUser extends ParseObject implements ParseCloneable {
}
}

// Logs in a user anonymously
Future<ParseResponse> loginAnonymous() async {
try {
Uri tempUri = Uri.parse(_client.data.serverUrl);

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

var uuid = new Uuid();

final response = await _client.post(url, headers: {
keyHeaderRevocableSession: "1",

}, body: jsonEncode({
"authData": {
"anonymous": {
"id": uuid.v4()
}
}
}));

return _handleResponse(response, ParseApiRQ.loginAnonymous);
}
on Exception catch (e) {
return _handleException(e, ParseApiRQ.loginAnonymous);
}
}

/// Removes the current user from the session data
logout() {
_client.data.sessionId = null;
Expand Down Expand Up @@ -198,7 +229,9 @@ class ParseUser extends ParseObject implements ParseCloneable {
var uri = _client.data.serverUrl + "$path/$objectId";
var body =
json.encode(toJson(forApiRQ: true), toEncodable: dateTimeEncoder);
final response = await _client.put(uri, body: body);
final response = await _client.put(uri,
headers: {keyHeaderSessionToken: _client.data.sessionId},
body: body);
return _handleResponse(response, ParseApiRQ.save);
} on Exception catch (e) {
return _handleException(e, ParseApiRQ.save);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/parse_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ dynamic parseDecode(dynamic value) {
}
return ParseObject(className).fromJson(map);
case "File":
return new ParseFile(null).fromJson(map);
return new ParseFile(null, url: map["url"], name: map["name"]).fromJson(map);
case "GeoPoint":
num latitude = map["latitude"] ?? 0.0;
num longitude = map["longitude"] ?? 0.0;
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ dependencies:

# Utils
shared_preferences: ^0.4.3
path_provider: ^0.4.1
path_provider: ^0.4.1
uuid: ^1.0.0