diff --git a/sdk/src/main/java/io/hasura/sdk/HasuraClient.java b/sdk/src/main/java/io/hasura/sdk/HasuraClient.java index 313de2f..4f701b0 100644 --- a/sdk/src/main/java/io/hasura/sdk/HasuraClient.java +++ b/sdk/src/main/java/io/hasura/sdk/HasuraClient.java @@ -40,7 +40,12 @@ public void onAuthTokenChanged(String authToken) { @Override public void onRolesChanged(String[] roles) { + //TODO: Handle + } + @Override + public void onSocialLoginAccessTokenChanged(Map map) { + //TODO: Handle } }; diff --git a/sdk/src/main/java/io/hasura/sdk/HasuraUser.java b/sdk/src/main/java/io/hasura/sdk/HasuraUser.java index 0e5ccbb..0d36ab2 100644 --- a/sdk/src/main/java/io/hasura/sdk/HasuraUser.java +++ b/sdk/src/main/java/io/hasura/sdk/HasuraUser.java @@ -1,8 +1,8 @@ package io.hasura.sdk; -import android.util.Log; - import java.io.IOException; +import java.util.HashMap; +import java.util.Map; import io.hasura.sdk.exception.HasuraException; import io.hasura.sdk.model.request.AuthRequest; @@ -18,6 +18,7 @@ import io.hasura.sdk.model.response.GetCredentialsResponse; import io.hasura.sdk.model.response.LogoutResponse; import io.hasura.sdk.model.response.MessageResponse; +import io.hasura.sdk.model.response.SocialLoginResponse; import io.hasura.sdk.responseListener.AuthResponseListener; import io.hasura.sdk.responseListener.ChangeEmailResponseListener; import io.hasura.sdk.responseListener.ChangeMobileResponseListener; @@ -49,7 +50,10 @@ public class HasuraUser implements AnonymousUserApi, AuthenticatedUserApi { interface StateChangeListener { void onAuthTokenChanged(String authToken); + void onRolesChanged(String[] roles); + + void onSocialLoginAccessTokenChanged(Map map); } private Integer id; @@ -59,6 +63,7 @@ interface StateChangeListener { private String[] roles; private String authToken; private String password; + private Map socialLoginTypeAccessTokenMap = new HashMap<>(); private AnonymousApiService anonApiService; private UserApiService userApiService; @@ -122,7 +127,8 @@ private HasuraUser(String authUrl, Boolean shouldEnableLogging, StateChangeListe this.stateChangeListener = listener; } - private HasuraUser() {} + private HasuraUser() { + } private HasuraUser(HasuraUser user) { setId(user.getId()); @@ -170,6 +176,11 @@ public void setId(Integer id) { this.id = id; } + public void setAccesstokenForSocialLogin(HasuraSocialLoginType type, String accessToken) { + socialLoginTypeAccessTokenMap.put(type, accessToken); + this.stateChangeListener.onSocialLoginAccessTokenChanged(socialLoginTypeAccessTokenMap); + } + public void setRoles(String[] roles) { this.roles = roles; this.stateChangeListener.onRolesChanged(roles); @@ -263,6 +274,37 @@ public void onFailure(HasuraException e) { } } + private class SocialAuthResponseCallbackHandler implements Callback { + + AuthResponseListener listener; + HasuraSocialLoginType type; + + SocialAuthResponseCallbackHandler(HasuraSocialLoginType type, AuthResponseListener listener) { + this.listener = listener; + this.type = type; + } + + @Override + public void onSuccess(SocialLoginResponse response) { + setId(response.getId()); + setRoles(response.getRoles()); + setAuthToken(response.getAuthToken()); + + HasuraSessionStore.saveUser(HasuraUser.this); + + if (listener != null) { + listener.onSuccess("Login Successful"); + } + } + + @Override + public void onFailure(HasuraException e) { + if (listener != null) { + listener.onFailure(e); + } + } + } + private class SuccessFailureCallbackHandler implements Callback { SuccessFailureResponseListener listener; @@ -332,7 +374,7 @@ public void login(final AuthResponseListener listener) { @Override public void socialLogin(HasuraSocialLoginType type, String token, final AuthResponseListener listener) { anonApiService.socialAuth(new SocialLoginRequest(type.getCode(), token)) - .enqueue(new AuthResponseCallbackHandler(listener)); + .enqueue(new SocialAuthResponseCallbackHandler(type, listener)); } @Override @@ -530,7 +572,7 @@ public void onSuccess(MessageResponse response) { @Override public void onFailure(HasuraException e) { - if(listener != null) { + if (listener != null) { listener.onFailure(e); } } diff --git a/sdk/src/main/java/io/hasura/sdk/model/request/SocialLoginRequest.java b/sdk/src/main/java/io/hasura/sdk/model/request/SocialLoginRequest.java index 5727df4..95fd2e9 100644 --- a/sdk/src/main/java/io/hasura/sdk/model/request/SocialLoginRequest.java +++ b/sdk/src/main/java/io/hasura/sdk/model/request/SocialLoginRequest.java @@ -29,10 +29,10 @@ public void setIdToken(String token) { public String prepareRequestURL() { if(idToken != null) { - return "/" + provider + "/authenticate?id_token=" + idToken; + return provider + "/authenticate?id_token=" + idToken; } else { - return "/" + provider + "/authenticate?access_token=" + accessToken; + return provider + "/authenticate?access_token=" + accessToken; } } } diff --git a/sdk/src/main/java/io/hasura/sdk/model/response/SocialLoginResponse.java b/sdk/src/main/java/io/hasura/sdk/model/response/SocialLoginResponse.java index 331e3d1..58b1bfa 100644 --- a/sdk/src/main/java/io/hasura/sdk/model/response/SocialLoginResponse.java +++ b/sdk/src/main/java/io/hasura/sdk/model/response/SocialLoginResponse.java @@ -18,15 +18,15 @@ public class SocialLoginResponse { @SerializedName("access_token") String access_token; - public int getHasuraId() { + public int getId() { return hasuraId; } - public String[] getHasuraRoles() { + public String[] getRoles() { return hasuraRoles; } - public String getSessionId() { + public String getAuthToken() { return auth_token; } diff --git a/sdk/src/main/java/io/hasura/sdk/service/AnonymousApiService.java b/sdk/src/main/java/io/hasura/sdk/service/AnonymousApiService.java index 6d1abdf..ab915b0 100644 --- a/sdk/src/main/java/io/hasura/sdk/service/AnonymousApiService.java +++ b/sdk/src/main/java/io/hasura/sdk/service/AnonymousApiService.java @@ -96,7 +96,7 @@ public Call otpLogin(AuthRequest r) { * @return {@link SocialLoginResponse} * @throws HasuraException */ - public Call socialAuth(SocialLoginRequest r) { + public Call socialAuth(SocialLoginRequest r) { // the URL is prepared inside the request class String url = r.prepareRequestURL(); Type respType = new TypeToken() {