I am writing a Flutter app that should interact with my Backendless CloudCode. For some reason, when I make calls from Flutter to CloudCode, there is no userId in this.request.context. I have a user logged in and verified in the app. I have used this same code with JS client and I have used the built in request engine in Backendless and they both seem to work fine.
Here is my flutter code and its outputs:
void init() async {
await Backendless.initApp(
applicationId: APPLICATION_ID,
androidApiKey: ANDROID_API_KEY,
iosApiKey: IOS_API_KEY);
List<String> channels = ["default"];
Backendless.messaging.registerDevice(channels).then((response) {
print("Device registered!");
});
var user = await Backendless.userService.getCurrentUser();
if (user == null) {
Navigator.push(context, MaterialPageRoute(builder: (_) => LoginPage()));
} else {
var userId = user.getUserId();
print("User Id: $userId");
var mood = await OctoMood.getMood(userId);
print(mood);
}
}
Logs:
E/flutter (31735): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: BackendlessException: Access denied, code = 14008
E/flutter (31735): #0 Invoker.invoke.<anonymous closure>
package:backendless_sdk/…/common/invoker.dart:20
E/flutter (31735): #1 _rootRunUnary (dart:async/zone.dart:1362:47)
E/flutter (31735): #2 _CustomZone.runUnary (dart:async/zone.dart:1265:19)
E/flutter (31735): <asynchronous suspension>
E/flutter (31735): #3 _MyHomePageState.init
package:octomood/main.dart:55
E/flutter (31735): <asynchronous suspension>
E/flutter (31735):
I/flutter (31735): Device registered!
Restarted application in 776ms.
I/flutter (31735): Login page user: BackendlessUser{{lastLogin: 2021-10-24 13:29:27.840, userStatus: ENABLED, created: 2021-10-21 21:41:15.000, accountType: BACKENDLESS, ownerId: B454DEFE-553B-4C1A-818A-A488C6BCAEDD, socialAccount: BACKENDLESS, oAuthIdentities: null, name: null, ___class: Users, blUserLocale: en, updated: 2021-10-24 13:29:24.000, objectId: BFBD0788-F632-416E-ABDB-5DD874753C80, email: josh@greencrypted.com}}
I/flutter (31735): Build
I/flutter (31735): User Id: BFBD0788-F632-416E-ABDB-5DD874753C80
The Access Denied error is caused by access not being permitted to Unauthenticated users.
Here is my CloudCode:
async getMood( user ) {
var response = "";
var whereClause = "user like '" + user + "'";
var queryBuilder = Backendless.DataQueryBuilder.create().setWhereClause( whereClause );
var currentUserId = this.request.context.userId;
console.log(this.request.context);
if(currentUserId != null){
if(currentUserId == user){
await Backendless.Data.of( "moods" ).find( queryBuilder )
.then( function( foundMoods ) {
console.log(foundMoods[0].mood);
var userMood = foundMoods[0];
response = userMood.mood;
})
.catch( function( fault ) {
response = fault;
});
} else {
var followersArr = await this.getFollowers(user, true);
if(followersArr.includes(currentUserId)){
await Backendless.Data.of( "moods" ).find( queryBuilder )
.then( function( foundMoods ) {
console.log(foundMoods[0].mood);
var userMood = foundMoods[0];
response = userMood.mood;
})
.catch( function( fault ) {
response = fault;
});
} else {
response = "Unauthorized";
}
}
} else {
response = "Unauthorized";
}
console.log(response);
return response;
}