androidsyncano

Syncano Android Social Login with custom class results in error


Social Login in Syncano doesn't work with custom profile. I use SocialAuthBackend.FACEBOOKand pass a token with custom class such as:

@SyncanoClass(name = "user_profile")
public class UserProfile extends AbstractUser {
}

together as:

syncano.loginSocialUser(UserProfile.class, SocialAuthBackend.FACEBOOK, loginResult.getAccessToken().getToken()).sendAsync(new SyncanoCallback<UserProfile>() {
      //...
});

Which result in:

IllegalArgumentException: field com.syncano.library.data.AbstractUser.profile has type com.syncano.library.data.Profile, got com.google.gson.internal.LinkedTreeMap
                                                                               at java.lang.reflect.Field.set(Native Method)
                                                                               at java.lang.reflect.Field.set(Field.java:557)
                                                                               at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:119)
                                                                               at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:217)
                                                                               at com.google.gson.Gson.fromJson(Gson.java:861)
                                                                               at com.google.gson.Gson.fromJson(Gson.java:826)
                                                                               at com.google.gson.Gson.fromJson(Gson.java:775)
                                                                               at com.google.gson.Gson.fromJson(Gson.java:747)

Can't fix it or find any sample online using this feature (or docs on this).


Solution

  • When you want to declare own user profile you should create 2 class:

    1. Implementation of AbstractUser<T> (where T is your profile class name)
    2. Your own class UserProfile class with extend Profile class (from syncano package).

    Sample code:

        @SyncanoClass(name = "user_profile")
        public class MyUserProfile extends Profile {
            @SyncanoField(name = "avatar")
            public SyncanoFile avatar;
        }
    
        public class CustomUser extends AbstractUser<MyUserProfile> {
                public CustomUser(String login, String pass) {
                     super(login, pass);
                 }
        }
    

    Soon release:

    From version 4.0.6 (not published yet) you don't need anymore @SyncanoClass annotation in profile (optional).