Skip to content

Commit

Permalink
Merge pull request #378 from okta/rn_sessionClient_fix
Browse files Browse the repository at this point in the history
Fix sessionClient being null when not calling signIn
  • Loading branch information
rajdeepnanua-okta authored Aug 8, 2023
2 parents 7ed0203 + 24c6feb commit f8727f7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.9.1

### Bug Fix
- [#378](https://github.com/okta/okta-react-native/pull/378) Fix sessionClient being null when not calling login

# 2.9.0

### Bug Fix
Expand Down
26 changes: 23 additions & 3 deletions android/src/main/java/com/oktareactnative/OktaSdkBridgeModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -55,18 +57,26 @@

public class OktaSdkBridgeModule extends ReactContextBaseJavaModule implements ActivityEventListener {

private static final String SESSION_CLIENT_SHARED_PREFS = "OKTA_SDK_BRIDGE_MODULE_SESSION_CLIENT";
private static final String PREFS_KEY = "SESSION_CLIENT";
private static final String SESSION_CLIENT_AUTH = "AUTH";
private static final String SESSION_CLIENT_WEB = "WEB";
private final ReactApplicationContext reactContext;
private OIDCConfig config;
private WebAuthClient webClient;
private AuthClient authClient;
private SessionClient sessionClient;
private Promise queuedPromise;
private LastRequestType mLastRequestType;
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor sharedPreferencesEditor;

public OktaSdkBridgeModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
this.reactContext.addActivityEventListener(this);
this.sharedPreferences = reactContext.getSharedPreferences(SESSION_CLIENT_SHARED_PREFS, Context.MODE_PRIVATE);
this.sharedPreferencesEditor = sharedPreferences.edit();
}

@Override
Expand Down Expand Up @@ -130,6 +140,12 @@ public void createConfig(String clientId,
this.authClient = authClientBuilder.create();

promise.resolve(true);

if (SESSION_CLIENT_WEB.equals(sharedPreferences.getString(PREFS_KEY, SESSION_CLIENT_WEB))) {
sessionClient = this.webClient.getSessionClient();
} else {
sessionClient = this.authClient.getSessionClient();
}
} catch (Exception e) {
promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
}
Expand Down Expand Up @@ -219,6 +235,7 @@ public void authenticate(String sessionToken, final Promise promise) {
public void onSuccess(@NonNull Result result) {
if (result.isSuccess()) {
try {
sharedPreferencesEditor.putString(PREFS_KEY, SESSION_CLIENT_AUTH).apply();
sessionClient = authClient.getSessionClient();
Tokens tokens = sessionClient.getTokens();
String token = tokens.getAccessToken();
Expand All @@ -233,13 +250,15 @@ public void onSuccess(@NonNull Result result) {
params.putString(OktaSdkConstant.ACCESS_TOKEN_KEY, token);
promise.resolve(params);
} catch (AuthorizationException e) {
sharedPreferencesEditor.clear().apply();
WritableMap params = Arguments.createMap();
params.putString(OktaSdkConstant.ERROR_CODE_KEY, OktaSdkError.SIGN_IN_FAILED.getErrorCode());
params.putString(OktaSdkConstant.ERROR_MSG_KEY, OktaSdkError.SIGN_IN_FAILED.getErrorMessage());
sendEvent(reactContext, OktaSdkConstant.ON_ERROR, params);
promise.reject(OktaSdkError.SIGN_IN_FAILED.getErrorCode(), OktaSdkError.SIGN_IN_FAILED.getErrorMessage());
}
} else {
sharedPreferencesEditor.clear().apply();
WritableMap params = Arguments.createMap();
params.putString(OktaSdkConstant.ERROR_CODE_KEY, OktaSdkError.SIGN_IN_FAILED.getErrorCode());
params.putString(OktaSdkConstant.ERROR_MSG_KEY, OktaSdkError.SIGN_IN_FAILED.getErrorMessage());
Expand All @@ -250,6 +269,7 @@ public void onSuccess(@NonNull Result result) {

@Override
public void onError(String error, AuthorizationException exception) {
sharedPreferencesEditor.clear().apply();
WritableMap params = Arguments.createMap();
params.putString(OktaSdkConstant.ERROR_CODE_KEY, OktaSdkError.OKTA_OIDC_ERROR.getErrorCode());
params.putString(OktaSdkConstant.ERROR_MSG_KEY, error);
Expand Down Expand Up @@ -447,7 +467,6 @@ public void clearTokens(final Promise promise) {
authClient.getSessionClient().clear();
}

sessionClient = null;
promise.resolve(true);
} catch (Exception e) {
promise.reject(OktaSdkError.OKTA_OIDC_ERROR.getErrorCode(), e.getLocalizedMessage(), e);
Expand Down Expand Up @@ -514,6 +533,7 @@ public void onSuccess(@NonNull AuthorizationStatus status) {
}
sendEvent(reactContext, OktaSdkConstant.SIGN_IN_SUCCESS, params);
sessionClient = localSessionClient;
sharedPreferencesEditor.putString(PREFS_KEY, SESSION_CLIENT_WEB);
queuedPromise = null;
} catch (AuthorizationException e) {
WritableMap params = Arguments.createMap();
Expand All @@ -523,12 +543,12 @@ public void onSuccess(@NonNull AuthorizationStatus status) {
promise.reject(e);
}
sendEvent(reactContext, OktaSdkConstant.ON_ERROR, params);
sessionClient = null;
sharedPreferencesEditor.clear().apply();
queuedPromise = null;
}
} else if (status == AuthorizationStatus.SIGNED_OUT) {
localSessionClient.clear();
sessionClient = null;
sharedPreferencesEditor.clear().apply();
WritableMap params = Arguments.createMap();
params.putString(OktaSdkConstant.RESOLVE_TYPE_KEY, OktaSdkConstant.SIGNED_OUT);
if (promise != null) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "@okta/okta-react-native",
"title": "React Native Okta Sdk Bridge",
"version": "2.9.0",
"version": "2.9.1",
"types": "types/index.d.ts",
"description": "Okta OIDC for React Native",
"main": "dist/index.js",
Expand Down

0 comments on commit f8727f7

Please sign in to comment.