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

Add a reauthenticate method to expose refresh token use #167

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions android/src/main/java/io/fullstack/oauth/OAuthManagerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public void onOAuth2AccessToken(final OAuth2AccessToken accessToken) {
}
}

@ReactMethod
public void reauthenticate(
final String providerName,
@Nullable final ReadableMap params,
final Callback callback)
{
Log.e(TAG, "Reauthenticate is not implemented for android");
WritableMap err = Arguments.createMap();
err.putString("status", "error");
err.putString("msg", "Reauthenticate is not implemented for android");
callback.invoke(err);
return;
}


@ReactMethod
public void makeRequest(
final String providerName,
Expand Down
62 changes: 62 additions & 0 deletions ios/OAuthManager/OAuthManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,61 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
}];
}

RCT_EXPORT_METHOD(reauthenticate:(NSString *) providerName
opts:(NSDictionary *) opts
callback:(RCTResponseSenderBlock) callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
DCTAuthAccountStore *store = [manager accountStore];
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];

NSString *newToken = [opts valueForKey:@"accessToken"];

DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
if (existingAccount == nil) {
NSDictionary *resp = @{
@"status": @"error",
@"msg": [NSString stringWithFormat:@"No account found for %@", providerName]
};
callback(@[resp]);
} else if (newToken != nil) {
DCTOAuth2Credential *existingCredential = existingAccount.credential;
existingAccount.credential = [[DCTOAuth2Credential alloc] initWithClientID:existingCredential.clientID
clientSecret:existingCredential.clientSecret
password:existingCredential.password
accessToken:newToken
refreshToken: existingCredential.refreshToken
type:existingCredential.type];

[store saveAccount:existingAccount];
NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg];
callback(@[[NSNull null], @{
@"status": @"ok",
@"provider": providerName,
@"response": accountResponse
}]);
} else {
[existingAccount reauthenticateWithHandler:^(DCTAuthResponse *response, NSError *error) {
if (error != nil) {
NSDictionary *resp = @{
@"status": @"error",
@"msg": [error localizedDescription]
};
callback(@[resp]);
}
else {
[store saveAccount:existingAccount];
NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg];
callback(@[[NSNull null], @{
@"status": @"ok",
@"provider": providerName,
@"response": accountResponse
}]);
}
}];
}
}

RCT_EXPORT_METHOD(makeRequest:(NSString *)providerName
urlOrPath:(NSString *) urlOrPath
opts:(NSDictionary *) opts
Expand All @@ -403,6 +458,8 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name

NSDictionary *creds = [self credentialForAccount:providerName cfg:cfg];

id<DCTAuthAccountCredential> existingCredential = [existingAccount credential];

// If we have the http in the string, use it as the URL, otherwise create one
// with the configuration
NSURL *apiUrl;
Expand Down Expand Up @@ -474,6 +531,11 @@ - (NSDictionary *) getConfigForProvider:(NSString *)name
NSInteger statusCode = response.statusCode;
NSData *rawData = response.data;

// if account.credential has changed through use of a refresh token, save the updated token
if ([existingAccount credential] != existingCredential) {
[[manager accountStore] saveAccount:existingAccount];
}

NSError *err;
NSArray *data;

Expand Down
7 changes: 7 additions & 0 deletions react-native-oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ export default class OAuthManager {
return promisify('authorize')(provider, options);
}

reauthenticate(provider, opts={}) {
const options = Object.assign({}, this._options, opts, {
app_name: this.appName
})
return promisify('reauthenticate')(provider, options);
}

savedAccounts(opts={}) {
// const options = Object.assign({}, this._options, opts, {
// app_name: this.appName
Expand Down