打開Todo\Models\UserModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Todo.Models
{
public class UserModel
{
public string email { get; set; }
public string password { get; set; }
public string username { get; set; }
}
}
打開Todo\Services\IAuthService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Todo.Models;
namespace Todo.Services
{
public interface IAuthService
{
Task<bool> RegisterUserWithEmail(UserModel user);
Task<bool> LoginUserWithEmail(UserModel user);
Task LogoutWithEmail();
UserModel GetUser();
Task<bool> IsLogin();
}
}
添加AuthService.cs
:
using Firebase.Auth;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Todo.Models;
namespace Todo.Services
{
public class AuthService : IAuthService
{
private readonly String webApikey = "AIzaSyCpllW5JzsuuUkQ0B4cPmZSiTDayle-Gho"; // Type your web api key
private User _user;
public bool isLogin = false;
public UserModel GetUser()
{
UserModel model = new UserModel
{
username = _user.DisplayName,
email = _user.Email,
password = "",
};
return model;
}
public async Task<bool> IsLogin()
{
await Task.Delay(10);
return isLogin;
}
public async Task<bool> LoginUserWithEmail(UserModel user)
{
try {
var authProvider = new FirebaseAuthProvider(new FirebaseConfig(webApikey));
var auth = await authProvider.SignInWithEmailAndPasswordAsync(user.email, user.password);
_user = auth.User;
isLogin = true;
return true;
} catch {
isLogin = false;
return false;
}
}
public async Task LogoutWithEmail()
{
await Task.Delay(10);
_user = new User();
isLogin = false;
}
public async Task<bool> RegisterUserWithEmail(UserModel user)
{
try {
var authProvider = new FirebaseAuthProvider(new FirebaseConfig(webApikey));
var auth = await authProvider.CreateUserWithEmailAndPasswordAsync(user.email, user.password);
return true;
} catch {
return false;
}
}
}
}
打開Todo\ViewModels\AuthViewModel.cs
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Todo.Models;
using Todo.Services;
namespace Todo.ViewModels
{
[QueryProperty(nameof(User), "User")]
public partial class AuthViewModel : ObservableObject
{
[ObservableProperty]
private UserModel _user = new UserModel();
private readonly IAuthService _authService;
private bool _isLogin = false;
public AuthViewModel(IAuthService authService)
{
_authService = authService;
}
[RelayCommand]
public async void GetUser()
{
if(_isLogin) {
_user = _authService.GetUser();
}
await Shell.Current.DisplayAlert("Please Login", "Login", "Ok");
}
[RelayCommand]
public async void IsLogin()
{
_isLogin = await _authService.IsLogin();
}
[RelayCommand]
public async void LogoutUser()
{
await _authService.LogoutWithEmail();
_isLogin = await _authService.IsLogin();
await Shell.Current.DisplayAlert("Status: Logout Success", "Logout Success", "Ok");
}
[RelayCommand]
public async void LoginUserWithEmailAndPassword()
{
var result = await _authService.LoginUserWithEmail(new Models.UserModel
{
email=User.email,
password=User.password,
});
if (result) {
_isLogin = await _authService.IsLogin();
await Shell.Current.DisplayAlert("Status: Login Success", "Login Success", "Ok");
} else {
await Shell.Current.DisplayAlert("Status: Login Failed", "Login failed", "Ok");
}
}
[RelayCommand]
public async void RegisterUserWithEmailAndPassword()
{
var result = await _authService.RegisterUserWithEmail(new Models.UserModel
{
email=User.email,
password=User.password,
});
if (result) {
await Shell.Current.DisplayAlert("Status: Register Success", "User registed", "Ok");
} else {
await Shell.Current.DisplayAlert("Status: Register Failed", "Something is failed", "Ok");
}
}
}
}