4

I'm using React on Rails and currently logging out using a ERB component. I'm creating a hamburger menu for the app and putting the logout in it. Currently it just sitting out in the opening using <%= link_to "Logout", destroy_user_session_path, method: :delete %> in Rails. I would like to put it in the React component. Please help. I'm still new at using React on Rails. The code for this is below.

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

This^ is being imported to here(below). It works with the rest of the code.

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 Answers2

2

I got the log out working using this code. Thank you all who responded.

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

P.S. I should of mention this earlier. The log in is though a API and using Devise.

0

You can actually log out through client-side. All rails session does was setting browser cookies. You check that by opening console and type

document.cookie

If you're logged in, you might see something like

"auth_token=icHQZ7QB5WK1PPXCWIiF0A"

auth_token is the cookie name. In order to delete this cookie, you will need to set its expiry date to the past:

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

If that works, that means on your React, you simply do something like

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
Liren Yeo
  • 3,215
  • 2
  • 20
  • 41
  • Thank you for responding. I used document.cookie in the console and nothing came up while using "rails s" in the terminal. Does it matter that I'm using "devise"? –  Jan 26 '19 at 19:42
  • make sure you are logged in. and type document.cookie in browser console. – Liren Yeo Jan 26 '19 at 20:56