public IActionResult DeleteUser() { var encodedToken = _tokenManager.ReadAndValidateToken(Request.Headers[_config.authHeader]); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken decodedToken; try { decodedToken = handler.ReadJwtToken(encodedToken); } catch (ArgumentException) { return(new UnauthorizedObjectResult("User Token Is Not Valid")); } if (_tokenManager.IsTokenValid(encodedToken)) { var userId = _tokenManager.GetUserIdFromToken(decodedToken); _tokenManager.DeleteUserTokens(userId); var user = _dbCtx.UserAccounts.Where(x => x.Id == userId).FirstOrDefault(); if (user != null) { _dbCtx.UserAccounts.Remove(user); try { _dbCtx.SaveChanges(); return(new OkObjectResult($"User Account {user.AppUserName} was successfully deleted!")); } catch (Exception) { // Rollback user deleteion _dbCtx.UserAccounts.Add(user); return(new NotFoundObjectResult("Failed to Delete User Profile")); } } return(new NotFoundObjectResult("Failed to find User")); } return(new UnauthorizedObjectResult("User Token Is Not Valid")); }
public IActionResult LogoutUser() { var encodedToken = _tokenManager.ReadAndValidateToken(Request.Headers[_config.authHeader]); var handler = new JwtSecurityTokenHandler(); JwtSecurityToken decodedToken; try { decodedToken = handler.ReadJwtToken(encodedToken); } catch (ArgumentException) { return(new UnauthorizedObjectResult(new AuthResponse() { ResponseMessage = "User Token Is Not Valid" })); } if (_tokenManager.IsTokenValid(encodedToken)) { var userId = _tokenManager.GetUserIdFromToken(decodedToken); if (userId != -1) { _tokenManager.DeleteUserTokens(userId); return(new OkObjectResult(new AuthResponse() { ResponseMessage = "Successful Logout" })); } return(new NotFoundObjectResult(new AuthResponse() { ResponseMessage = "Failed to Find user" })); } return(new BadRequestObjectResult(new AuthResponse() { ResponseMessage = "The Supplied Token is Invalid" })); }