fix: roll back when auth failed.

oneRain 2021-01-12 14:51:12 +08:00
parent 346dcb3ba6
commit 4a1937c312
1 changed files with 26 additions and 3 deletions

View File

@ -326,7 +326,7 @@ namespace LeanCloud.Storage {
if (string.IsNullOrEmpty(platform)) { if (string.IsNullOrEmpty(platform)) {
throw new ArgumentNullException(nameof(platform)); throw new ArgumentNullException(nameof(platform));
} }
return LinkWithAuthData(platform, null); return UnlinkWithAuthData(platform);
} }
/// <summary> /// <summary>
@ -523,11 +523,34 @@ namespace LeanCloud.Storage {
return new LCQuery<LCUser>(CLASS_NAME); return new LCQuery<LCUser>(CLASS_NAME);
} }
Task LinkWithAuthData(string authType, Dictionary<string, object> data) { async Task LinkWithAuthData(string authType, Dictionary<string, object> data) {
Dictionary<string, object> oriAuthData = new Dictionary<string, object>(AuthData);
AuthData = new Dictionary<string, object> { AuthData = new Dictionary<string, object> {
{ authType, data } { authType, data }
}; };
return Save(); try {
await Save();
oriAuthData.Add(authType, data);
AuthData = oriAuthData;
} catch (Exception e) {
AuthData = oriAuthData;
throw e;
}
}
async Task UnlinkWithAuthData(string authType) {
Dictionary<string, object> oriAuthData = new Dictionary<string, object>(AuthData);
AuthData = new Dictionary<string, object> {
{ authType, null }
};
try {
await Save();
oriAuthData.Remove(authType);
AuthData = oriAuthData;
} catch (Exception e) {
AuthData = oriAuthData;
throw e;
}
} }
static async Task<LCUser> Login(Dictionary<string, object> data) { static async Task<LCUser> Login(Dictionary<string, object> data) {