From 4a1937c3123638d27aea724e6d8f84c6e14ff232 Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 12 Jan 2021 14:51:12 +0800 Subject: [PATCH] fix: roll back when auth failed. --- Storage/Storage/LCUser.cs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/Storage/Storage/LCUser.cs b/Storage/Storage/LCUser.cs index af1aa03..70e8f28 100644 --- a/Storage/Storage/LCUser.cs +++ b/Storage/Storage/LCUser.cs @@ -326,7 +326,7 @@ namespace LeanCloud.Storage { if (string.IsNullOrEmpty(platform)) { throw new ArgumentNullException(nameof(platform)); } - return LinkWithAuthData(platform, null); + return UnlinkWithAuthData(platform); } /// @@ -523,11 +523,34 @@ namespace LeanCloud.Storage { return new LCQuery(CLASS_NAME); } - Task LinkWithAuthData(string authType, Dictionary data) { + async Task LinkWithAuthData(string authType, Dictionary data) { + Dictionary oriAuthData = new Dictionary(AuthData); AuthData = new Dictionary { { 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 oriAuthData = new Dictionary(AuthData); + AuthData = new Dictionary { + { authType, null } + }; + try { + await Save(); + oriAuthData.Remove(authType); + AuthData = oriAuthData; + } catch (Exception e) { + AuthData = oriAuthData; + throw e; + } } static async Task Login(Dictionary data) {