How to access session data in another route?

Hello … Good night. I have a big problem.
I cannot access session data from another host.
What I am trying to do is possible?

If not, what is the best way to do this?
I saw that apparently the problem is because of cookies. I don’t know how to solve this. Two days researching, reading documentation, but couldn’t solve it. Help me please.

Note:
If I’m accessing from the same host and port, it works.

-----------------Client:-----------------
// On http://127.0.0.1:5000

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <button id="teste1">Test 1</button>
    <button id="teste2">Test 2</button>
    <script>
        const baseUrl = 'http://127.0.0.1:3333';
        
        document.getElementById('teste1').onclick = () => {

            fetch(baseUrl + "/a", {
                method: "GET",
                mode: 'cors'
            }).then(response => {
                if (response.status == 200) {
                    return response.json();
                } else {
                    throw new Error('Ops! Houve um erro em nosso servidor.');
                }
                return response => response.json();
            }).then(response => {
                console.log(response);
            }).catch(error => {
                console.error(error);
            });
        }

        document.getElementById('teste2').onclick = () => {

            fetch(baseUrl + "/b", {
                method: "GET",
                mode: 'cors'
            }).then(response => {
                if (response.status == 200) {
                    return response.json();
                } else {
                    throw new Error('Ops! Houve um erro em nosso servidor.');
                }
                return response => response.json();
            }).then(response => {
                console.log(response);
            }).catch(error => {
                console.error(error);
            });
        }

    </script>
</body>

</html>
// On http://127.0.0.1:3333
// ----------------- Routes: -----------------

Route.get ("a", ({request, session, response}) => {
  console.log (response);
  session.put ("math", Math.random ());
  return {_math: session.get ("math")};
});

Route.get ("b", ({request, session}) => {
  return {_math: session.get ("math")};
});

//-----------------start/app.js-----------------
"use strict";

const providers = [
  "@adonisjs/framework/providers/AppProvider",
  "@adonisjs/framework/providers/ViewProvider",
  "@adonisjs/lucid/providers/LucidProvider",
  "@adonisjs/bodyparser/providers/BodyParserProvider",
  "@adonisjs/cors/providers/CorsProvider",
  "@adonisjs/shield/providers/ShieldProvider",
  "@adonisjs/session/providers/SessionProvider",
  "@adonisjs/auth/providers/AuthProvider",
  "@adonisjs/vow/providers/VowProvider",
  "@adonisjs/vow-browser/providers/VowBrowserProvider",
  "@adonisjs/validator/providers/ValidatorProvider",
  "adonis-bundler/providers/BundlerProvider"
];

const aceProviders = ["@adonisjs/lucid/providers/MigrationsProvider"];
const aliases = {};
const commands = [];
module.exports = { providers, aceProviders, aliases, commands };

//-----------------start/kernel.js-----------------

'use strict'

const Server = use('Server')

const globalMiddleware = [
  'Adonis/Middleware/BodyParser',
  'Adonis/Middleware/Session',
  'Adonis/Middleware/Shield',
  'Adonis/Middleware/AuthInit'
]

const namedMiddleware = {
  auth: 'Adonis/Middleware/Auth',
  guest: 'App/Middleware/RedirectIfAuthenticated',
}

const serverMiddleware = [
  'Adonis/Middleware/Static',
  'Adonis/Middleware/Cors'
]

Server
  .registerGlobal(globalMiddleware)
  .registerNamed(namedMiddleware)
  .use(serverMiddleware)

1 Like

I belive you need to set fetch credentials to include or same-origin

fetch(baseUrl + "/b", {
  method: 'GET',
  credentials: 'include'
   ...
})
1 Like

Good afternoon. I managed to solve. thank you
Following a series of tasks:
The first: add to the second parameter the api fetch credentials: ‘include’.
The second: in the config/cors file, change the origin keys to true and credentials to true.

1 Like