在React on Rails的汉堡菜单中如何添加注销功能?顺便说一下,我是一名学生。

4

我正在使用React on Rails,目前正在使用ERB组件进行注销。我正在为应用程序创建汉堡菜单,并将注销放在其中。目前,它只是使用Rails中的<%= link_to "Logout", destroy_user_session_path, method: :delete %>放在开头。我想把它放在React组件中。请帮忙。我还不太熟悉在React on Rails上使用。以下是此代码。

import React, { Component } from 'react'

class MenuContent extends Component {
  constructor(props) {
    super(props)

  }

  render() {
    return (
      <div className="menu">
        <div className="Logout">
        I'm trying to add the JavaScript code here. Here how I'm doing it in Ruby. 
 <%= link_to "Logout", destroy_user_session_path, method: :delete %>
        </div>

        <p className="hint">Click outside the menu to close it, or swipe it closed on touch device</p>
      </div>
    )
  }
}

export default MenuContent

这^被导入到这里(下面)。它与代码的其余部分配合工作。
import React, {Component} from 'react'
import CheeseburgerMenu from "cheeseburger-menu";
import HamburgerMenu from "react-hamburger-menu";
import MenuContent from "./MenuContent";

class Navbar extends Component {
  constructor(props) {
    super(props);

    this.state = {
      menuOpen: false
    };
  }

  openMenu() {
    this.setState({ menuOpen: true });
  }

  closeMenu() {
    this.setState({ menuOpen: false });
  }
  render(){
    return(
      <div>
        <CheeseburgerMenu
          isOpen={this.state.menuOpen}
          closeCallback={this.closeMenu.bind(this)}>
          <MenuContent closeCallback={this.closeMenu.bind(this)} />
        </CheeseburgerMenu>

        <HamburgerMenu
          isOpen={this.state.menuOpen}
          menuClicked={this.openMenu.bind(this)}
          width={32}
          height={24}
          strokeWidth={3}
          rotate={0}
          color="black"
          borderRadius={0}
          animationDuration={0.5} />
      </div>
    )
  }
}

export default Navbar
2个回答

2

我使用这段代码使注销功能正常工作。感谢所有回复的人。

最初的回答:

import React, { Component } from 'react'
import axios from 'axios'

// import './menuContent.scss'

class MenuContent extends Component {
  constructor(props) {
    super(props)
  }
   handleLogout = () => {
const link = document.createElement('a');
link.setAttribute('href', '/users/sign_out');
link.setAttribute('rel', 'nofollow');
link.setAttribute('data-method', 'delete');
document.body.appendChild(link);
link.click();
}

  render() {
    return (
      <div className="grandMenu">
          <div className="menu">
            <div className="signButton"></div>
              <button onClick={this.handleLogout}>Sign Out</button>
            <p>hello world</p>
          </div>
            <p className="hint">
              Click outside the menu to close it, or swipe it away.
            </p>
        </div>
    )
  }
}


export default MenuContent

补充一点,这个登录是通过API和Devise实现的。

最初的回答:


0

你实际上可以通过客户端注销。 所有的Rails session只是设置浏览器cookie。你可以通过打开控制台并输入以下内容来检查:

document.cookie

如果您已登录,您可能会看到类似于以下内容:

"auth_token=icHQZ7QB5WK1PPXCWIiF0A"

auth_token 是 cookie 的名称。为了删除此 cookie,您需要将其到期日期设置为过去:

document.cookie = 'auth_token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
// Replace 'auth_token' with whatever cookie name you're using.

如果这样做可以正常工作,那么在你的React中,你只需要像这样做:
import React, { Component } from 'react'

class MenuContent extends Component {
  onLogout = () => {
    document.cookie = 'auth_token=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'
  }

  render() {
    return (
      <div className="menu">
        <div onClick={this.onLogout} className="Logout">
          Click to Log Out
        </div>
        <p className="hint">
          Click outside the menu to close it, or swipe it closed on touch device
        </p>
      </div>
    )
  }
}

export default MenuContent

谢谢您的回复。在控制台中使用document.cookie并在终端中使用“rails s”时未出现任何内容。我使用“devise”是否有影响? - user10839797
请确保您已登录,并在浏览器控制台中键入document.cookie。 - Liren Yeo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接