Search code examples
ruby-on-railsreactjsdevise

Devise DELETE /users/sign_out returns status 204 and doesn't redirect?


I'm wondering why nothing happens when I use Fetch API to call the sign out endpoint with redirect: 'follow' set.

My simple React component:

<a href="users/sign_out" onClick={this.handleSignOutClick}>Sign Out</a>

My event handler:

  handleSignOutClick (event) {
    event.preventDefault();

    fetch('/users/sign_out', {
      method: 'DELETE',
      mode: 'cors',
      credentials: 'include',
      redirect: 'follow',
    });
  }

Within my ApplicationController:

  def after_sign_in_path_for(resource)
    resource.referrer
  end

Wondering if it's because I'm using Fetch API the wrong way? I'm able to redirect to a page on successful sign-in.


Solution

  • I didn't realize you can do DELETE in anchors. After inspecting the default sign_out button, I used

    <a href="/users/sign_out" data-method="delete" rel="nofollow">Sign Out</a>