Quantcast
Channel: Ionic Framework - Ionic Forum
Viewing all articles
Browse latest Browse all 49086

Ionic front-end + Strapi back-end device http conection trouble

$
0
0

I am having and old and solved trouble on my app development. I had this error before with CORS and my app.

Well, It seems that my problem configurating middleware.js is a version problem. After several troubles, I upgrade my strapi to v3.2.5 following the strapi.io tutorials. But, I am having the same problem. My app works in firefox on windows but, when I compile apk and test it in my device I have the same problem:

Http failure response for http://192.168.0.50:1337/auth/local: 0 Unkonwn Error Example image

Once I could configurate on the elder version of strapi configurating on config/enviorement/… security.json. Now day, I couldn’t. This is my middleware.js:

module.exports = {
  time: 100,
  load: {
    before: ['timer', 'responseTime', 'logger', 'cors', 'responses', 'gzip'],
    after: ['parser', 'router'],
  },
  settings: {
    cors: {
      enabled: true,
      origin: '*',
      methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD'],
    },
    language: {
      enabled: true,
      defaultLocate: 'en_us',
      modes: [
        'query','subdomain','cookie','header','url','tld'
      ],
      cookieName: 'locate',
    },
    gzip: {
      enabled: false,
    },
    responseTime: {
      enabled: false,
    },
    poweredBy: {
      enabled: true,
      value: 'Strapi <strapi.io>',
    },
    favicon: {
      path: 'favicon.ico',
      maxAge: 86400000,
    },
    public: {
      path: './public',
      maxAge: 60000,
    },
    session: {
      enabled: true,
      client: 'cookie',
      key: 'strapi.sid',
      prefix: 'strapi:sess:',
      secretKeys: ['mySecretKey1', 'mySecretKey2'],
      httpOnly: true,
      maxAge: 86400000,
      overwrite: true,
      signed: false,
      rolling: false
    },
    logger: {
      level: 'debug',
      exposeInContext: true,
      requests: true
    },
    parser: {
      enabled: true,
      multipart: true
    },
    csp: {
      enabled: true,
      policy: ['block-all-mixed-content']
    },
    p3p: {
      enabled: true,
      value: ''
    },
    hsts: {
      enabled: true,
      maxAge: 31536000,
      includeSubDomains: true
    },
    xframe: {
      enabled: true,
      value: 'SAMEORIGIN'
    },
    xss: {
      enabled: true,
      mode: 'block'
    },
    ip: {
      enabled: false,
      whiteList: [],
      blackList: []
    }
  },

};

Indeed on my firefox it works fine. The problem is when I try to connect from my phone once the app is compile and run on it.
I am using node v14.15.3. Strapi 3.4.1 installed step by step using strapi documentation.
The only thing I maintain is the API folder.
Anyway, this is the code on my front-end Ionic:

public login(email: string, pass: string) {
    const loginInputData = new LoginInputData();
    loginInputData.identifier = email;
    loginInputData.password = pass;
    const h2 = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json; charset=utf-8',
        'Access-Control-Allow-Origin': '*',
      })
    };
    console.log('Login headers: ', h2.headers.get('Access-Control-Allow-Origin'));
    return this.http.post<UserData>(this.strapiCnx.getLoginStr(), JSON.stringify(loginInputData), h2);
  }

As you can see, it is when I try to login an user. I repeat, this code works on my pc where I am programming the app.

I got this from my pc where the connection success perfectly:

How can I see the same on my device (webview)? On Android I got something like “ERROR TypeError: Cannot read property ‘0’ of undefined”

When I searched about this, people said that it is a CORS problem.

In the version 3.0.0 beta xx and before, I solved the problem adding the config to the security.json and adding headers to the front-end. Now, I do what tutorial says and it doesn’t work for me.

It is a mess for me. I am wasting my time right now…

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 49086

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>