Beispiel #1
0
        private static async Task UpdateSpotifyUserAccessToken(IEnumerable <User> users, string refreshToken, SpotifyService spotifyService, ApplicationDbContext context)
        {
            var token = await spotifyService.GetAccessTokenFromRefreshToken(refreshToken);

            if (string.IsNullOrWhiteSpace(token.Error))
            {
                foreach (var user in users)
                {
                    user.SpotifyAccessToken = token.AccessToken;

                    if (string.IsNullOrWhiteSpace(token.RefreshToken) == false)
                    {
                        user.SpotifyRefreshToken = token.RefreshToken;
                    }

                    user.LastUpdated = DateTime.UtcNow;
                }
            }
            else
            {
                // Invalid tokens, we remove from database
                context.Users.RemoveRange(users);
            }

            await context.SaveChangesAsync();
        }