Base solution for your next web application
Open Closed

Delete user account: best practice #4894


User avatar
0
affern created

Hello.

I want the user to be able to delete his/her account, but I'm not sure how I should do this. I'm trying to add a public method to the AccountController that is similar to the logout method. But the class does'nt implement a appservice interface, so the method is not visible in the api. How can I make this method visible? Do I need to sign out the user before I delete the record from db? Or how is the best way to do this?


7 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    But the class does'nt implement a appservice interface, so the method is not visible in the api. How can I make this method visible?

    Swagger needs a HTTP method and a route, so add [HttpPost] and [Route] attributes.

    Do I need to sign out the user before I delete the record from db?

    Yes, otherwise the browser will attempt to use the invalid cookie and then get an error.

    Or how is the best way to do this?

    Do this:

    [HttpPost]
    [Route(nameof(DeleteMe))]
    public async Task<RedirectToActionResult> DeleteMe()
    {
        await _signInManager.SignOutAsync();
    
        var user = await _userManager.GetUserByIdAsync(AbpSession.UserId.Value);
        await _userManager.DeleteAsync(user);
    
        return RedirectToAction("Login");
    }
    

    More info:

  • User Avatar
    0
    affern created

    I have copied your code into the AccountController class, but it still not shows in AppAuthService. It is only logout that is available. And the logout method is not marked with [HttpPost] or [Route] attributes. I have run the refresh.bat file but with no luck :?

  • User Avatar
    0
    alirizaadiyahsi created

    Hi @affern, why don't use AccountAppService instead of AccountController? What is your project type? Angular or mvc?

  • User Avatar
    0
    affern created

    <cite>alirizaadiyahsi: </cite> Hi @affern, why don't use AccountAppService instead of AccountController? What is your project type? Angular or mvc?

    My project type is Angular. Maybe I can use AccountAppService. But I don't think it works to redirect to login in Angular after I have deleted the user. I tried to put the DeleteMe method in my own service and do the redirect in Angular after, but I did'nt get it to work. The AccountAppService can't return RedirectToAction result, so how do you solve this?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @affern,

    If you use app service approach, then you can remove the auth token on angular client and then reload the page using regular javascript.

    You can check logout method of angular app to remove token.

  • User Avatar
    0
    affern created

    <cite>ismcagdas: </cite> Hi @affern,

    If you use app service approach, then you can remove the auth token on angular client and then reload the page using regular javascript.

    You can check logout method of angular app to remove token.

    Thanks, @ismcagdas! I called the logout method in AppAuthService after the DeleteMe method. It works fine.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Great :)