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

feat(auth, ios): Add support for Facebook Limited Login #6073

Merged
merged 3 commits into from
Feb 15, 2022
Merged
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
41 changes: 41 additions & 0 deletions docs/auth/social-auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,47 @@ async function onFacebookButtonPress() {
}
```

### Facebook Limited Login (iOS only)

To use Facebook Limited Login instead of "classic" Facebook Login, the `onFacebookButtonPress` can then be implemented as follows:

```js
import auth from '@react-native-firebase/auth';
import { LoginManager, AuthenticationToken } from 'react-native-fbsdk-next';
import { sha256 } from 'react-native-sha256';

async function onFacebookButtonPress() {
// Create a nonce and the corresponding
// sha256 hash of the nonce
const nonce = '123456';
const nonceSha256 = await sha256(nonce);
// Attempt login with permissions and limited login
const result = await LoginManager.logInWithPermissions(
['public_profile', 'email'],
'limited',
nonceSha256,
);

if (result.isCancelled) {
throw 'User cancelled the login process';
}

// Once signed in, get the users AuthenticationToken
const data = await AuthenticationToken.getAuthenticationTokenIOS();

if (!data) {
throw 'Something went wrong obtaining authentication token';
}

// Create a Firebase credential with the AuthenticationToken
// and the nonce (Firebase will validates the hash against the nonce)
const facebookCredential = auth.FacebookAuthProvider.credential(data.authenticationToken, nonce);

// Sign-in the user with the credential
return auth().signInWithCredential(facebookCredential);
}
```

Upon successful sign-in, any [`onAuthStateChanged`](/auth/usage#listening-to-authentication-state) listeners will trigger
with the new authentication state of the user.

Expand Down
11 changes: 11 additions & 0 deletions packages/auth/e2e/provider.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ describe('auth() -> Providers', function () {
});
});

describe('credentialLimitedLogin', function () {
it('should return a credential object', function () {
const token = '123456';
const nonce = '654321';
const credential = firebase.auth.FacebookAuthProvider.credential(token, nonce);
credential.providerId.should.equal('facebook.com');
credential.token.should.equal(token);
credential.secret.should.equal(nonce);
});
});

describe('PROVIDER_ID', function () {
it('should return facebook.com', function () {
firebase.auth.FacebookAuthProvider.PROVIDER_ID.should.equal('facebook.com');
Expand Down
5 changes: 5 additions & 0 deletions packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,11 @@ - (FIRAuthCredential *)getCredentialForProvider:(NSString *)provider
credential = credentials[authToken];
} else if ([provider compare:@"twitter.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
credential = [FIRTwitterAuthProvider credentialWithToken:authToken secret:authTokenSecret];
} else if ([provider compare:@"facebook.com" options:NSCaseInsensitiveSearch] == NSOrderedSame &&
![authTokenSecret isEqualToString:@""]) {
credential = [FIROAuthProvider credentialWithProviderID:provider
IDToken:authToken
rawNonce:authTokenSecret];
} else if ([provider compare:@"facebook.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
credential = [FIRFacebookAuthProvider credentialWithAccessToken:authToken];
} else if ([provider compare:@"google.com" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/lib/providers/FacebookAuthProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ export default class FacebookAuthProvider {
return providerId;
}

static credential(token) {
static credential(token, secret = '') {
return {
token,
secret: '',
secret,
providerId,
};
}
Expand Down