diff --git a/samples/WinUI3/App.xaml.cs b/samples/WinUI3/App.xaml.cs index 2ace63a..6746e82 100644 --- a/samples/WinUI3/App.xaml.cs +++ b/samples/WinUI3/App.xaml.cs @@ -55,7 +55,10 @@ private void AuthStateChanged(object sender, UserEventArgs e) { if (e.User == null) { - await FirebaseUI.Instance.Client.SignInAnonymouslyAsync(); + if (FirebaseUI.Instance.Config.IsAnonymousAllowed) + { + await FirebaseUI.Instance.Client.SignInAnonymouslyAsync(); + } (Window.Content as Frame).Navigate(typeof(LoginPage)); } else if (e.User.IsAnonymous) diff --git a/src/Auth.UI.WinUI3/Auth.UI.WinUI3.csproj b/src/Auth.UI.WinUI3/Auth.UI.WinUI3.csproj index 81cd1df..9d23a4b 100644 --- a/src/Auth.UI.WinUI3/Auth.UI.WinUI3.csproj +++ b/src/Auth.UI.WinUI3/Auth.UI.WinUI3.csproj @@ -36,12 +36,12 @@ The library provides a drop-in auth solution that handles the flows for signing - <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))"/> + <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))" /> - + diff --git a/src/Auth.UI.WinUI3/Helpers/Json.cs b/src/Auth.UI.WinUI3/Helpers/Json.cs new file mode 100644 index 0000000..e401590 --- /dev/null +++ b/src/Auth.UI.WinUI3/Helpers/Json.cs @@ -0,0 +1,31 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + + +namespace Firebase.Auth.UI.Helpers +{ + public static class Json + { + public static async Task ToObjectAsync(string value) + { + return await Task.Run(() => + { + return JsonConvert.DeserializeObject(value); + }).ConfigureAwait(false); + } + + public static async Task StringifyAsync(object value) + { + return await Task.Run(() => + { + return JsonConvert.SerializeObject(value); + }).ConfigureAwait(false); + } + } + +} + diff --git a/src/Auth.UI.WinUI3/Helpers/RuntimeHelper.cs b/src/Auth.UI.WinUI3/Helpers/RuntimeHelper.cs new file mode 100644 index 0000000..4aea06d --- /dev/null +++ b/src/Auth.UI.WinUI3/Helpers/RuntimeHelper.cs @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.InteropServices; +using System.Text; +using System.Threading.Tasks; + +namespace Firebase.Auth.UI.Helpers +{ + public class RuntimeHelper + { + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); + + public static bool IsMSIX + { + get + { + var length = 0; + + return GetCurrentPackageFullName(ref length, null) != 15700L; + } + } + } + +} diff --git a/src/Auth.UI.WinUI3/Repository/LocalSettingsWrapper.cs b/src/Auth.UI.WinUI3/Repository/LocalSettingsWrapper.cs new file mode 100644 index 0000000..07ba3b2 --- /dev/null +++ b/src/Auth.UI.WinUI3/Repository/LocalSettingsWrapper.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Text; +using System.Threading.Tasks; +using Windows.Storage; + +using Windows.ApplicationModel; +using Firebase.Auth.UI.Helpers; +using System.Reflection; +using System.Diagnostics; + + +namespace Firebase.Auth.UI.Repository +{ + internal class LocalSettingsWrapper + { + private const string _defaultLocalSettingsFile = "LocalSettings.json"; + private readonly string _localApplicationData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); + private readonly string _applicationDataFolder; + private readonly string _localsettingsFile; + + private IDictionary _settings; + + private bool _isInitialized; + + //make as singletion + private static LocalSettingsWrapper _instance; + public static LocalSettingsWrapper Instance + { + get + { + if (_instance == null) + { + _instance = new LocalSettingsWrapper(); + } + + return _instance; + } + } + public LocalSettingsWrapper() + { + var process = Process.GetCurrentProcess(); + string mainExecutablePath = process.MainModule.FileName; + string appName = System.IO.Path.GetFileNameWithoutExtension(mainExecutablePath); + + _applicationDataFolder = System.IO.Path.Combine(_localApplicationData, $"{appName}Data"); + _localsettingsFile = _defaultLocalSettingsFile; + + _settings = new Dictionary(); + } + + public bool Exists(string key) + { + if (RuntimeHelper.IsMSIX) + { + return ApplicationData.Current.LocalSettings.Values.ContainsKey(key); + } + else + { + return _settings.ContainsKey(key); + } + } + + private async Task InitializeAsync() + { + if (!_isInitialized) + { + //_settings = await Task.Run(() => _fileService.Read>(_applicationDataFolder, _localsettingsFile)) ?? new Dictionary(); + + string filepath = System.IO.Path.Combine(_applicationDataFolder, _localsettingsFile); + if (System.IO.File.Exists(filepath)) + { + string content = await System.IO.File.ReadAllTextAsync(filepath).ConfigureAwait(false); + _settings = await Json.ToObjectAsync>(content).ConfigureAwait(false); + } + else + { + _settings = new Dictionary(); + } + + _isInitialized = true; + } + } + + public async Task ReadSettingAsync(string key) + { + if (RuntimeHelper.IsMSIX) + { + if (ApplicationData.Current.LocalSettings.Values.TryGetValue(key, out var obj)) + { + return await Json.ToObjectAsync((string)obj).ConfigureAwait(false); + } + } + else + { + await InitializeAsync().ConfigureAwait(false); + + if (_settings != null && _settings.TryGetValue(key, out var obj)) + { + return await Json.ToObjectAsync((string)obj).ConfigureAwait(false); + } + } + + return default; + } + + public async Task SaveSettingAsync(string key, T value) + { + if (RuntimeHelper.IsMSIX) + { + ApplicationData.Current.LocalSettings.Values[key] = await Json.StringifyAsync(value).ConfigureAwait(false); + } + else + { + await InitializeAsync().ConfigureAwait(false); + + _settings[key] = await Json.StringifyAsync(value).ConfigureAwait(false); + if (System.IO.Directory.Exists(_applicationDataFolder) == false) + { + System.IO.Directory.CreateDirectory(_applicationDataFolder); + } + + string filepath = System.IO.Path.Combine(_applicationDataFolder, _localsettingsFile); + await System.IO.File.WriteAllTextAsync(filepath, await Json.StringifyAsync(_settings)).ConfigureAwait(false); + + + //await Task.Run(() => _fileService.Save(_applicationDataFolder, _localsettingsFile, _settings)); + } + } + } + +} diff --git a/src/Auth.UI.WinUI3/Repository/StorageRepository.cs b/src/Auth.UI.WinUI3/Repository/StorageRepository.cs index 364dfb3..45c0b88 100644 --- a/src/Auth.UI.WinUI3/Repository/StorageRepository.cs +++ b/src/Auth.UI.WinUI3/Repository/StorageRepository.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Firebase.Auth.UI.Repository; +using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System.Threading.Tasks; using Windows.Storage; @@ -10,39 +11,52 @@ public class StorageRepository : IUserRepository private const string UserStorageKey = "FirebaseUser"; private const string CredentialStorageKey = "FirebaseCredential"; - private readonly ApplicationDataContainer settings; + //private readonly ApplicationDataContainer settings; private readonly JsonSerializerSettings options; public StorageRepository() { - this.settings = ApplicationData.Current.LocalSettings; + //this.settings = ApplicationData.Current.LocalSettings; this.options = new JsonSerializerSettings(); this.options.Converters.Add(new StringEnumConverter()); } public void DeleteUser() { - this.settings.Values[UserStorageKey] = null; - this.settings.Values[CredentialStorageKey] = null; + LocalSettingsWrapper.Instance.SaveSettingAsync(UserStorageKey, null).Wait(); + LocalSettingsWrapper.Instance.SaveSettingAsync(CredentialStorageKey, null).Wait(); + //this.settings.Values[UserStorageKey] = null; + //this.settings.Values[CredentialStorageKey] = null; } public (UserInfo userInfo, FirebaseCredential credential) ReadUser() { - var info = JsonConvert.DeserializeObject(this.settings.Values[UserStorageKey].ToString(), this.options); - var credential = JsonConvert.DeserializeObject(this.settings.Values[CredentialStorageKey].ToString(), this.options); - - return (info, credential); + string storageKey = LocalSettingsWrapper.Instance.ReadSettingAsync(UserStorageKey).Result; + string credentialKey = LocalSettingsWrapper.Instance.ReadSettingAsync(CredentialStorageKey).Result; + if (storageKey == null || credentialKey == null) + { + return (null, null); + } + var info = JsonConvert.DeserializeObject(storageKey, this.options); + var credential = JsonConvert.DeserializeObject(credentialKey, this.options); + + return (info, credential); } public void SaveUser(User user) { - this.settings.Values[UserStorageKey] = JsonConvert.SerializeObject(user.Info, this.options); - this.settings.Values[CredentialStorageKey] = JsonConvert.SerializeObject(user.Credential, this.options); + LocalSettingsWrapper.Instance.SaveSettingAsync(UserStorageKey, JsonConvert.SerializeObject(user.Info, this.options)).Wait(); + LocalSettingsWrapper.Instance.SaveSettingAsync(CredentialStorageKey, JsonConvert.SerializeObject(user.Credential, this.options)).Wait(); + //this.settings.Values[UserStorageKey] = JsonConvert.SerializeObject(user.Info, this.options); + //this.settings.Values[CredentialStorageKey] = JsonConvert.SerializeObject(user.Credential, this.options); } public bool UserExists() { - return this.settings.Values.ContainsKey(UserStorageKey); + string storageKey = LocalSettingsWrapper.Instance.ReadSettingAsync(UserStorageKey).Result; + return storageKey != null; + //return LocalSettingsWrapper.Instance.Exists(UserStorageKey); + //return this.settings.Values.ContainsKey(UserStorageKey); } } }