I'm facing an issue, I've created some code to be able to login in my app with facebook. The steps are working as expected:
- The external facebook login web page opens
- The Second activity intercept the redirectUrl after a successfull login
At this point OAuth2Autenticator.Completed(object o, AuthenticatorCompletedEventArgs e) is called but e is not authenticated.
My code in ViewModel:
async Task FacebookAuth()
{
var facebookAppId = "0123...";
_authenticator = new OAuth2Authenticator(
clientId: facebookAppId,
scope: "email",
authorizeUrl: new Uri("https://www.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("https://www.myRedirectUrl.com"),
isUsingNativeUI: true
);
_authenticator.Completed += Authenticator_Facebook_Completed;
_authenticator.Error += _authenticator_Error;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(_authenticator);
}
In the interceptor activity:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
var mUri = new Uri(Intent.Data.ToString());
_authenticator.OnPageLoading(mUri);
Finish();
}
Back to the viewModel:
private async void Authenticator_Facebook_Completed(object sender, AuthenticatorCompletedEventArgs e)
{
_authenticator.Completed -= Authenticator_Facebook_Completed;
if (e.IsAuthenticated)
{
var acc = e.Account;
var val = JsonConvert.SerializeObject(e.Account);
var model = JsonConvert.DeserializeObject<FacebookSigninModel>(val);
var user = await GetFacebookProfile(model.Properties.AccessToken);
await SignInToAPI(new APIUser
{
TokenId = model.Properties.AccessToken,
Email = user.Email,
FamilyName = user.FirstName,
GivenName = user.LastName,
Picture = user.ProfilePic
}, AuthProvider.Facebook);
}
}
Here e is not authenticated and i have a null account.
Another strange detail is that even if i remove the line:
_authenticator.OnPageLoading(mUri);
the method Authenticator_Facebook_Completed is called anyway with the exact same result.
I've also tried to change the redirect url in order to no catch it in the interceptor activity. So as expected i have to manually go back to my app. Whe the system returns to my app, the Authenticator_Facebook_Completed method is called with parameter values equals to the other approaches.
I have written my code following this sample:
https://github.com/coolc0ders/SocialAuthXamarinFormsAspNetCore/tree/master/AuthDemoXForms
Running this app the Authenticator_Facebook_Completed is called only after the _authenticator.OnPageLoading(Uri); is called too and not when the system reopens the app after the redirect from the web page.
I cannot figure out why my Authenticator_Facebook_Completed is always called no matter what and why _authenticator.OnPageLoading(mUri); has no relevance at all in my code.
Thanks,