How to send a react-created cookie to flask to be used in flask session?

490 Views Asked by At

My code currently consists of a frontend in react and a flask backend api. I'm trying to establish a session in my backend using flask sessions, but I'm unable to, since flask never makes contact with the user, and all requests go through the frontend. To that end, I'm trying to post cookies to

In the frontend, I have something like

import Cookies from 'universal-cookie';
class thingy extends React.Component{
     constructor(props) {
          this.backendurl = 'localhost:5000'
          const cookies = new Cookies();
          this.username = this.props.match.params.user


          cookies.set('UserName', ''+JSON.stringify(this.username), { path: 
               '/profile' });


     }
     onclick = ()=>{
      #post something using axios that sends the cookie to the flask backend so it can be processed
}

}
     render(){
          return(
               <button onClick = {this.onclick}>Click Here</button>
          )
     }
}

backend:

app = Flask(__name__)
app.config["SESSION_PERMANENT"] = False
Session(app)
@app.route('/user',methods=['POST'])
def info():
    #do something with the cookie that react posted in order to user flask_session
return user.name

Does someone know how to send the cookies from react to flask, and then input the cookies recieved by flask into the flask session?

0

There are 0 best solutions below