Set token http cookie from node.js express backend

  Kiến thức lập trình

I have hosted my application on Azure Web App. My frontend is Angular on the domain example.com, and my backend is Node.js Express on the domain api.example.com.

The front end works, the backend/API works, but I have a problem with setting the HTTP cookie where I store my authentication token.

The token is generated correctly, and I set it in the following way:

res.cookie('token', token, { httpOnly: true, secure: true, SameSite: 'None', path: '/' });

I do not receive any errors in the console.

This is my web.config for the backend:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <!-- Map all requests to the Node.js application -->
      <add name="iisnode" path="index.js" verb="*" modules="iisnode" />
    </handlers>
    <rewrite>
      <rules>
        <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js.iisnode"/>
          <action type="None"/>
        </rule>
        <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="^index.js/debug"/>
          <action type="None"/>
        </rule>
        <!-- Redirect all non-static requests to index.js -->
       <rule name="DynamicContent">
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{{REQUEST_FILENAME}}" matchType="IsFile" negate="true" />
    <add input="{{REQUEST_FILENAME}}" matchType="IsDirectory" negate="true" />
  </conditions>
  <action type="Rewrite" url="index.js" />
</rule>

      </rules>
    </rewrite>
    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>
    <iisnode watchedFiles="web.config;*.js" />
  </system.webServer>
</configuration>

This is my cors

const corsOptions = {
    origin: ['https://example.com'],
    credentials: true,
  };
  
app.use(cors(corsOptions));

How do I send the cookie so that it is saved in the user’s browser? Everything works without any issues on localhost.

I want to set http cookie into browser on azure web app.

LEAVE A COMMENT