Skip to content
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

After logging in user server side through cloud code ParseUser cannot be set using returned User object #700

Closed
SexyBeast007 opened this issue Jan 28, 2022 · 4 comments
Labels
type:question Support or code-level question

Comments

@SexyBeast007
Copy link

Issue Description

After logging in on the server side with credentials, I cannot set the ParseUser that was used to make the cloud call, as a logged in user with the returned User object.

Steps to reproduce

I will be showing two different ways I have attempted this.

/// Cloud login method - SERVER SIDE
Parse.Cloud.define("login", async function(request, response) {
  const username = request.params.username;
  const password = request.params.password;
  // Login
  try{
    let user = await Parse.User.logIn(username, password);
    if (user) return user
  } catch (error) {
    throw new Error(error.message);
  }
});

/// Cloud login method - CLIENT SIDE
  Future<ParseResponse> login(WidgetRef ref, String username, String password) async {
    ParseUser user = ParseUser(username, password, null);
    final ParseCloudFunction cloudLogin = ParseCloudFunction('login');
    final Map<String, dynamic> credentials = <String, dynamic>{
      'username': user.username,
      'password': user.password,
    };
    final ParseResponse loginAttempt = await cloudLogin.execute(parameters: credentials);
    
    /// ATTEMPT 1
    ref.read(authenticationServices).setAuthorizedUser(user);

    /// ATTEMPT 2
    ref.read(authenticationServices).setAuthorizedUser(loginAttempt.result);
    
    return loginAttempt;
  }

Actual Outcome

ATTEMPT 1

SUCCESSFUL LOGIN PAYLOAD

Payload: {
username: 001, 
email: 0001@gmail.com, 
name: 001, 
createdAt: 2022-01-23T05:04:42.888Z,
 updatedAt: 2022-01-23T05:04:42.888Z, 
ACL: {*: {read: true}, 
KMq2UWO7h8: {read: true, write: true}}, 
sessionToken: r:5d3c7409d572d30bad3e40db3586304a,
 objectId: KMq2UWO7h8, 
__type: Object, 
className: _User
}

LOGOUT FAILURE

flutter: {"className":"_User","username":"001"}         <--------- ParseUser object does not get mutated on login with authorized object
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: Null check operator used on a null value
#0      ParseUser.logout
package:parse_server_sdk/…/objects/parse_user.dart:318
#1      CloudCodeAPI.logout
package:beautiful_ui/…/api/cloud_code.dart:16
Restarted application in 4,458ms.
flutter: ╭-- Parse Request
flutter: curl -X POST -H 'content-type: text/plain; charset=utf-8' -H 'user-agent: Flutter Parse SDK 3.1.0' -H 'X-Parse-Application-Id: HO98yteRBT9CwwJhMp6vSQUfumYeh0ziApCH7isl' -H 'X-Parse-Client-Key: 6sWGVKDby4BCTbAS3bvkUCpn1oeTUbaXDA10vYjR' -d '{"username":"001","password":"001"}' https://parseapi.back4app.com/functions/login
 https://parseapi.back4app.com/functions/login
flutter: ╰--
flutter: ╭-- Parse Response
Class: login
Function: ParseApiRQ.execute
#2      AuthenticationServices.logout
package:beautiful_ui/…/logic/authentication_services.dart:66
#3      AuthenticationEndpoints.logout
package:beautiful_ui/…/logic/athentication_endpoints.dart:33
#4      Welcome.build.<anonymous closure>.<anonymous closure>
package:beautiful_ui/…/widgets/welcome.dart:29
#5      Welcome.build.<anonymous closure>.<anonymous closure>
package:beautiful_ui/…/widgets/welcome.dart:26
#6      _InkResponseState._handleTap
package:flutter/…/material/ink_well.dart:989
#7      GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:198
#8      TapGesture<…>

ATTEMPT 2

LOGIN FAILURE

error:
type '_InternalLinkedHashedMap<String, dynamic>' is not a subtype of ParseUser

Expected Outcome

I expected to be able to assign return object from Login payload as ParseUser or to have it happen automatically as in ATTEMPT 1 above, neither work.

Environment

  • Flutter: >=2.15.1 <3.0.0

Parse Flutter SDK

  • SDK version: 1.0.1
  • Operating system version: MacOS Big Sur 11.6.2

Server

  • Parse Server version: 4.5.0
@parse-github-assistant
Copy link

Thanks for opening this issue!

  • ❌ Please edit your post and use the provided template when creating a new issue. This helps everyone to understand your post better and asks for essential information to quicker review the issue.

@SexyBeast007
Copy link
Author

I have also tried the following, setting ParseUser explicitly with all the parts of the payload.

late ParseUser? _authorizedUser;
  ParseUser? get authorizedUser => _authorizedUser;
  void setAuthorizedUser(Map<String, dynamic> user) {
    _authorizedUser = ParseUser(
      user['username'],
      user['password'],
      user['email'],
    )
      ..set('sessionToken', user['sessionToken'])
      ..set('objectId', user['objectId'])
      ..set('className', user['className'])
      ..set('name', user['name'])
      ..set('createdAt', user['createdAt'])
      ..set('updatedAt', user['updatedAt'])
      ..setACL(user['ACL']);
  }

I get an error traced back to ParseCoreData().sessionId being null

@mtrezza mtrezza added the type:bug Impaired feature or lacking behavior that is likely assumed label Jan 28, 2022
@RodrigoSMarques
Copy link
Contributor

RodrigoSMarques commented Jan 29, 2022

Try calling this line of code after setting the user data.

ParseCoreData().setSessionId(user['sessionToken']!);

The sessionToken is being configured only in the signin and signup methods of the SDK.

You created a custom process, you need to set it manually.

@SexyBeast007
Copy link
Author

Try calling this line of code after setting the user data.

ParseCoreData().setSessionId(user['sessionToken']!);

The sessionToken is being configured only in the signin and signup methods of the SDK.

You created a custom process, you need to set it manually.

That did it @RodrigoSMarques, thank you very much! I appreciate the expertise!

And yes, I am creating my own system. I am taking security a step further. For certain threat actors HTTPS should no longer be trusted, therefore I will be locking down all CLPs and creating a sort of sub-https hybrid encryption system for all cloud code calls including login and logout. This is the last piece of the puzzle to enable this secure system. Thank you

@mtrezza mtrezza added type:question Support or code-level question and removed type:bug Impaired feature or lacking behavior that is likely assumed labels Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question Support or code-level question
Projects
None yet
Development

No branches or pull requests

3 participants