Skip to content

Commit

Permalink
Handled Social Login
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaison Titus committed Jul 28, 2017
1 parent 7925e48 commit 7d6ab47
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 11 deletions.
5 changes: 5 additions & 0 deletions sdk/src/main/java/io/hasura/sdk/HasuraClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public void onAuthTokenChanged(String authToken) {

@Override
public void onRolesChanged(String[] roles) {
//TODO: Handle
}

@Override
public void onSocialLoginAccessTokenChanged(Map<HasuraSocialLoginType, String> map) {
//TODO: Handle
}
};

Expand Down
52 changes: 47 additions & 5 deletions sdk/src/main/java/io/hasura/sdk/HasuraUser.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -49,7 +50,10 @@ public class HasuraUser implements AnonymousUserApi, AuthenticatedUserApi {

interface StateChangeListener {
void onAuthTokenChanged(String authToken);

void onRolesChanged(String[] roles);

void onSocialLoginAccessTokenChanged(Map<HasuraSocialLoginType, String> map);
}

private Integer id;
Expand All @@ -59,6 +63,7 @@ interface StateChangeListener {
private String[] roles;
private String authToken;
private String password;
private Map<HasuraSocialLoginType, String> socialLoginTypeAccessTokenMap = new HashMap<>();

private AnonymousApiService anonApiService;
private UserApiService userApiService;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -263,6 +274,37 @@ public void onFailure(HasuraException e) {
}
}

private class SocialAuthResponseCallbackHandler implements Callback<SocialLoginResponse, HasuraException> {

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<MessageResponse, HasuraException> {

SuccessFailureResponseListener listener;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -530,7 +572,7 @@ public void onSuccess(MessageResponse response) {

@Override
public void onFailure(HasuraException e) {
if(listener != null) {
if (listener != null) {
listener.onFailure(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Call<AuthResponse, HasuraException> otpLogin(AuthRequest r) {
* @return {@link SocialLoginResponse}
* @throws HasuraException
*/
public Call<AuthResponse, HasuraException> socialAuth(SocialLoginRequest r) {
public Call<SocialLoginResponse, HasuraException> socialAuth(SocialLoginRequest r) {
// the URL is prepared inside the request class
String url = r.prepareRequestURL();
Type respType = new TypeToken<SocialLoginResponse>() {
Expand Down

0 comments on commit 7d6ab47

Please sign in to comment.