Skip to content

Commit f681cc5

Browse files
authored
feat(auth, ios): Add support for Facebook Limited Login (#6073)
* feat(auth,ios): Add support for Facebook Limited Login Add support for Facebook Limited Login by making passing a second argument to the providers `credential` function optional and handling this later on in the native code by calling the OAuth provider for Facebook. Fixes: #5441 Signed-off-by: Martin Mazein <[email protected]> * tests(auth): Add test case for FB Limited Login This is just simply testing if the nonce passed to credentials is the nonce we see in the providers credential object. Signed-off-by: Martin Mazein <[email protected]> * docs(auth,ios): Add code snippet for Facebook Limited Login Signed-off-by: Martin Mazein <[email protected]>
1 parent a217978 commit f681cc5

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

docs/auth/social-auth.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,47 @@ async function onFacebookButtonPress() {
136136
}
137137
```
138138

139+
### Facebook Limited Login (iOS only)
140+
141+
To use Facebook Limited Login instead of "classic" Facebook Login, the `onFacebookButtonPress` can then be implemented as follows:
142+
143+
```js
144+
import auth from '@react-native-firebase/auth';
145+
import { LoginManager, AuthenticationToken } from 'react-native-fbsdk-next';
146+
import { sha256 } from 'react-native-sha256';
147+
148+
async function onFacebookButtonPress() {
149+
// Create a nonce and the corresponding
150+
// sha256 hash of the nonce
151+
const nonce = '123456';
152+
const nonceSha256 = await sha256(nonce);
153+
// Attempt login with permissions and limited login
154+
const result = await LoginManager.logInWithPermissions(
155+
['public_profile', 'email'],
156+
'limited',
157+
nonceSha256,
158+
);
159+
160+
if (result.isCancelled) {
161+
throw 'User cancelled the login process';
162+
}
163+
164+
// Once signed in, get the users AuthenticationToken
165+
const data = await AuthenticationToken.getAuthenticationTokenIOS();
166+
167+
if (!data) {
168+
throw 'Something went wrong obtaining authentication token';
169+
}
170+
171+
// Create a Firebase credential with the AuthenticationToken
172+
// and the nonce (Firebase will validates the hash against the nonce)
173+
const facebookCredential = auth.FacebookAuthProvider.credential(data.authenticationToken, nonce);
174+
175+
// Sign-in the user with the credential
176+
return auth().signInWithCredential(facebookCredential);
177+
}
178+
```
179+
139180
Upon successful sign-in, any [`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners will trigger
140181
with the new authentication state of the user.
141182

packages/auth/e2e/provider.e2e.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ describe('auth() -> Providers', function () {
7575
});
7676
});
7777

78+
describe('credentialLimitedLogin', function () {
79+
it('should return a credential object', function () {
80+
const token = '123456';
81+
const nonce = '654321';
82+
const credential = firebase.auth.FacebookAuthProvider.credential(token, nonce);
83+
credential.providerId.should.equal('facebook.com');
84+
credential.token.should.equal(token);
85+
credential.secret.should.equal(nonce);
86+
});
87+
});
88+
7889
describe('PROVIDER_ID', function () {
7990
it('should return facebook.com', function () {
8091
firebase.auth.FacebookAuthProvider.PROVIDER_ID.should.equal('facebook.com');

packages/auth/ios/RNFBAuth/RNFBAuthModule.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,11 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
953953
credential = credentials[authToken];
954954
} else if ([provider compare:@"twitter.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
955955
credential = [FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret];
956+
} else if ([provider compare:@"facebook.com" options:NSCaseInsensitiveSearch] == NSOrderedSame &&
957+
![authTokenSecret isEqualToString:@""]) {
958+
credential = [FIROAuthProvider credentialWithProviderID:provider
959+
IDToken:authToken
960+
rawNonce:authTokenSecret];
956961
} else if ([provider compare:@"facebook.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
957962
credential = [FIRFacebookAuthProvider credentialWithAccessToken:authToken];
958963
} else if ([provider compare:@"google.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {

packages/auth/lib/providers/FacebookAuthProvider.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ export default class FacebookAuthProvider {
2626
return providerId;
2727
}
2828

29-
static credential(token) {
29+
static credential(token, secret = '') {
3030
return {
3131
token,
32-
secret: '',
32+
secret,
3333
providerId,
3434
};
3535
}

0 commit comments

Comments
 (0)