title: How to store Cookies in flutter and use it to authenticate if user is logged in or not
body: <p>I'm trying to create a platform, once I log in it, creates a token and store it in the cookie. I have successfully been able to create a route that stores the cookie using node js(I could see it saved in postman). But once I try to use the route in my flutter app, it seems the the token doesn't save anywhere.</p>
  <p>How do I save the cookie and use it to validate if the user should be logged in or should log in again</p>
  <blockquote>
  <p>Login.dart</p>
  </blockquote>
  <pre><code>Future&lt;void&gt; _signIn(email, password) async {
      try {
        setState(() {
          LoadingWidget.show(context);
          bool _absorbme = true;
        });
  
        var url = &quot;http://192.168.8.23:3000/route/login&quot;; // iOS
        final http.Response response = await http.post(
          Uri.parse(url),
          headers: &lt;String, String&gt;{
            'Content-Type': 'application/json; charset=UTF-8',
          },
          body: jsonEncode(&lt;String, String&gt;{
            'email': email.toLowerCase(),
            'password': password,
          }),
        );
  
        SharedPreferences prefs = await SharedPreferences.getInstance();
        var parse = jsonDecode(response.body);
        await prefs.setString('msg', parse[&quot;msg&quot;]);
        await prefs.setString('success', parse[&quot;success&quot;]);
        String? msg = prefs.getString(&quot;msg&quot;);
  
      } finally {
        setState(() {
          LoadingWidget.hide(context);
          bool _absorbme = false;
        });
      }
    }
  </code></pre>
  <blockquote>
  <p>My Route.js file</p>
  </blockquote>
  <pre><code>const express = require('express');
  const router = express.Router();
  const Users = require('./model/model')
  const passport = require('passport')
  const bcrypt = require(&quot;bcrypt&quot;);
  const { createTokens, validateToken, authenticateToken } = require(&quot;./JWT&quot;);
  const Server = require('http-proxy');
  router.get('/', (req, res)=&gt;{
    res.send('Hello world')
  })
  
  router.post(&quot;/signup&quot;, async (req, res) =&gt;  {
  
    const { username, email, password } = req.body;
    let user = await Users.findOne({ email });
  
    if (user){ 
      
      return res.status(400).json({success: 'false', msg: &quot;Email Already Exist&quot;});
    }
    bcrypt.hash(password, 10).then((hash) =&gt; {
      Users.create({
        username: username,
        password: hash,
        email:email,
      })
        .then(() =&gt; {
          res.json({success: 'true', msg: 'login Successfully'});
        })
        .catch((err) =&gt; {
          if (err) {
            res.status(400).json({success: 'false', msg: 'Failed to save'});
          }
        });
    });
  });
  
  router.post(&quot;/login&quot;, async (req, res) =&gt; {
    const { email, password } = req.body;
  
    let user = await Users.findOne({ email });
  
    if (!user) {
      
      return res.status(400).json({success: 'false', msg: 'Authentication Failed, User not found'});
    }
  
    const dbPassword = user.password;
    bcrypt.compare(password, dbPassword).then((match) =&gt; {
      if (!match) {
        res
          .status(400)
          .json({success: 'false',  msg: 'Authentication failed, wrong password'});
      } else {
        const accessToken = createTokens(user);
  
        var token = res.cookie(&quot;access-token&quot;, accessToken, {
          maxAge: 60 * 60 * 24 * 30 * 1000,
          httpOnly: true,
        });
        
        res
          .status(200)
          .json({success: 'true',  msg: 'Successfully logged In'});
      }
    });
  });
  
  router.get(&quot;/profile&quot;, validateToken, async (req, res) =&gt; {
   let user = await Users.findOne({email: req.decoded.email});
   return res.json({
     email: user.email, 
     balance: user.balance,
     name: user.username
  })
  
  
  router.get(&quot;/logout&quot;, validateToken, (req, res) =&gt; {
    return res
      .clearCookie(&quot;access-token&quot;)
      .status(200)
      .json({success: &quot;true&quot; , msg: &quot;Successfully logged out&quot; });
  });
  
  router.get(&quot;/authenticate&quot; ,authenticateToken, (req,res)=&gt;{
  
  })
  
  
    module.exports = router;
  </code></pre>
  <p><a href="https://i.stack.imgur.com/HmF7l.png" rel="nofollow noreferrer">enter image description here</a></p>