Problem with Facebook login using MVVMCross and Xamarin.Auth

Hey guys,

I'm kinda new to Xamarin and i'm kinda lost. I'm trying to do a simple facebook login for an Android and iOS application. Here's what i have right now.

In my PCL i have the IAuthenticationService with the simple LoginWithFacebook function

public interface IAuthenticationService
    {
        void LoginWithFacebook();
    }

And a simple LoginViewModel

public class LoginViewModel : MvxViewModel
    {
        private readonly IAuthenticationService _authenticationService;

        public LoginViewModel(IAuthenticationService authenticationService)
        {
            _authenticationService = authenticationService;
        }

        public override void Start()
        {
            _email = string.Empty;
            _password = string.Empty;
            base.Start();
        }

        private string _email;
        private string _password;

        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        private MvxCommand _loginWithFacebook;
        public ICommand LoginWithFacebook{
            get { 

                _loginWithFacebook =  _loginWithFacebook ?? (_loginWithFacebook = new MvxCommand(_authenticationService.LoginWithFacebook));
                return _loginWithFacebook;
            }
        }

 }

So in my UI, when i click the login with facebook button, the LoginWithFacebool command is called. (this is working)

Since i'm not using Xamarin.Form, my Xamarin.Auth code must be in each platform specific project. I created a class that inherit from my PCL IAuthenticationService in the android project. (i'll take the android project);

public class DroidAuthenticationService : IAuthenticationService 
    {
        public void LoginWithFacebook(){        
            var auth = new OAuth2Authenticator(
                clientId: "250154754491", // your OAuth2 client id (For FB Also called App-ID)
                scope: "email", // the scopes for the particular API you're accessing, delimited by "+" symbols
                authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"), // the auth URL for the service (i.e FB, Twitter)
                redirectUrl: new Uri("https://www.facebook.com/connect/login_success.html")); // the redirect URL for the service
            auth.Completed += (sender, eventArgs) => {
                if (eventArgs.IsAuthenticated)
                {
                    //Saves Token, and Calls LoginSuccess() to change Screen
                    //App.getToken(eventArgs.Account.Properties["access_token"]);
                    //LoginPage.LoginSuccess();
                }
                else
                {
                    //LoginPage.LoginCancel();
                }
            };
            Mvx.Resolve<IMvxAndroidGlobals> ().ApplicationContext.StartActivity(auth.GetUI(this)); (this is not working)


        }

    }

When i'm debugging, i can go thought the android specific "LoginWithFacebook" function. But this line
Mvx.Resolve<IMvxAndroidGlobals> ().ApplicationContext.StartActivity(auth.GetUI(this)); (this is not working)
is not working and i didn't find any solution.

Since i don't have any context in this specific class, how can i call StartActivity? Should i just put this code in my LoginView in the OnCreated Method?
Also, is my architecture OK? Any input would be really appreciated.

Thanks
Nic

Posts

  • pochocostapochocosta ARMember

    Hi!

    I'm looking like the same thing you're doing. Have you been able to make it work?

    I will greatly appreciate your response. I've been doing a lot of research and I can not find anything about it.

    Greetings and thanks in advance!

Sign In or Register to comment.