예제 #1
0
        private async Task <string> GetToken()
        {
            var authProvider = new FirebaseAuthProvider(new FirebaseConfig(_apiKey.Value));
            var customToken  = await FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(Guid.NewGuid().ToString());

            return((await authProvider.SignInWithCustomTokenAsync(customToken)).FirebaseToken);
        }
예제 #2
0
        public static async Task <ErrorModel> CheckIfLoggedIn()
        {
            System.IO.Directory.CreateDirectory(userDataDirectory);

            FirebaseAuthProvider fbAuthProvider = new FirebaseAuthProvider(new FirebaseConfig(apiKey));

            var uId = "";

            try
            {
                uId = System.IO.File.ReadAllText(userDataFile);
            }
            catch (Exception ex)
            {
                Console.WriteLine("=====>\nCheckSavedUserId func, ex : " + ex.Message + "\n<=======");
                uId = null;
            }

            if (string.IsNullOrEmpty(uId))
            {
                return(new ErrorModel {
                    ErrorCode = false, Message = null
                });
            }



            var user = await GetUser(uId);

            if (user == null)
            {
                System.IO.File.WriteAllText(userDataFile, string.Empty);
                return(new ErrorModel {
                    ErrorCode = false, Message = "You need to log In."
                });
            }
            var customTokent = await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(user.Uid);

            FirebaseAuthLink auth = await fbAuthProvider.SignInWithCustomTokenAsync(customTokent);

            _auth = auth;
            _user = user;
            return(new ErrorModel {
                ErrorCode = true, Message = null
            });
        }
예제 #3
0
 internal Task <FirebaseAuthLink> SignInWithCustomTokenAsync(string token) =>
 _authProvider.SignInWithCustomTokenAsync(token);
예제 #4
0
        public static async Task <ErrorModel> LogInWithEmailAndPassword(string email, string password)
        {
            if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
            {
                // MessageBox.Show("E-postadress och lösenord är obligatoriska.", "Fel");
                return(new ErrorModel {
                    ErrorCode = false, Message = "E-postadress och lösenord är obligatoriska."
                });
            }


            UserRecord           user           = null;
            FirebaseAuthLink     auth           = null;
            FirebaseAuthProvider fbAuthProvider = new FirebaseAuthProvider(new FirebaseConfig(apiKey));

            try
            {
                //TODO: try internet
                auth = await fbAuthProvider.SignInWithEmailAndPasswordAsync(email, password);


                user = await GetUser(auth.User.LocalId);

                if (user == null)
                {
                    return new ErrorModel {
                               ErrorCode = false, Message = null
                    }
                }
                ;
            }
            catch (Exception ex)
            {
                Console.WriteLine("=====>\nbtnLogin_Click func, ex : " + ex.Message + "\n<=======");

                //MessageBox.Show("E-postadress eller lösenord är felaktiga.", "Fel");
                return(new ErrorModel {
                    ErrorCode = false, Message = "E-postadress eller lösenord är felaktiga."
                });
            }
            try
            {
                System.IO.File.WriteAllText(userDataFile, auth.User.LocalId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("=====>\nbtnLogin_Click func, ex : " + ex.Message + "\n<=======");


                //MessageBox.Show("Kunde inte skriva till fil.", "Fel");
                return(new ErrorModel {
                    ErrorCode = false, Message = "Skrivning till filen misslyckades."
                });
            }


            var ct = await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.CreateCustomTokenAsync(auth.User.LocalId);

            auth = await fbAuthProvider.SignInWithCustomTokenAsync(ct);

            var tt = await FirebaseAdmin.Auth.FirebaseAuth.DefaultInstance.VerifyIdTokenAsync(auth.FirebaseToken);

            _auth = auth;
            _user = user;
            return(new ErrorModel {
                ErrorCode = true, Message = null
            });
        }