0

I have strange problem with Angular 5 + Angular Universal + Https protocal. I can check view-source in browser on every page except home page with /:lang route. I did AuthGuard for home page like that :

import { Injectable }     from '@angular/core';
import { CanActivate }    from '@angular/router';
import { Router } from '@angular/router';
import { Config } from '../config';



@Injectable()
export class AuthGuard implements CanActivate {
langUrl : string;

  constructor(private router: Router, private config: Config) {
      this.langUrl = this.config.getLanguage();
  }
  canActivate() {
    this.router.navigate(['/' + this.langUrl]);
    return true;
  }
}

It's working well for every platform server/browser. In config my function getLanguage() looks like :

  isSessionAvaible() {
    var test = 'test';
    try {
      localStorage.setItem(test, test);
      localStorage.removeItem(test);
      return true;
    } catch (e) {
      return false;
    }
  }

  getLanguage() {
    if (this.isSessionAvaible()) {
      return localStorage
        .getItem('lang.url');
    } else {
      return this.defaultLang;
    }

}

When I'm trying to enter https://example.org. Guard always redirect me to https://example.org/en and there I can't see view-source in browser. See screen below.

HTTPS

When I'm deleting Node.js Express Server https redirect, which looks like :

var forceSsl = function (req, res, next) {
  if (req.headers['x-forwarded-proto'] !== 'https') {
      return res.redirect(['https://', req.get('Host'), req.url].join(''));
  }
  return next();
};


app.use(forceSsl);

And then visit page without https http://example.orgAuthGuard redirect me to http://example.org/en and view-source in browser looks like :

enter image description here

After that when I'm trying to enter https://example.org/ without server https redirect, view-source is same like in first example (with https redirect).

Do you know how to repair it ? How to show view-source on https home page also ?

David
  • 33,444
  • 11
  • 80
  • 118
Patryk Panek
  • 405
  • 4
  • 20
  • So it works for ALL other pages, with https ? And what if you try to access https://example.org/en directly ? – David Feb 13 '18 at 11:36
  • 1. `https://example.org/` - don't work after redirect to `https://example.org/en` 2. `http://example.org/` - don't work after redirect to `https://example.org/en` 3. `http://example.org/` - work (without https redirect) after redirect to `http://example.org/en` 4. `https://example.org/en` 5. `http://example.org/en` - work (without redirect https redirect) – Patryk Panek Feb 13 '18 at 11:51
  • yeah but what about `https://example.com/en` directly ? – David Feb 13 '18 at 12:55
  • It works without any problems. – Patryk Panek Feb 13 '18 at 12:56
  • Can you check the 'Location' header returned by the server when you access `https://example.com`? – David Feb 13 '18 at 12:58
  • How to do it in Node JS with framework Express ? I'm trying to do it like that : `app.get('*', function (req, res) { console.log(req.headers); });` And nothing happend in console. I've also tried to do it like that : `app.get('/', function (req, res) { console.log(req); });` But still nothing – Patryk Panek Feb 14 '18 at 09:02
  • I was just asking you to look at your network tab in chrome and see what the 'Location' header is when you hit `https://example.com` (check preserve logs to make sure you have time to see the request) – David Feb 14 '18 at 09:07
  • There no location header in GET for https://example.org, but I think it's normal. Angular check in AuthGuard and redirect it in routing. I don't do it by server. https://imgur.com/a/18aKs – Patryk Panek Feb 14 '18 at 09:15
  • Here screen from NodeJS console: https://imgur.com/a/JFR6U – Patryk Panek Feb 14 '18 at 09:21
  • But do you have a Location header for `http://example.org` then? (without https) – David Feb 14 '18 at 09:35
  • https://imgur.com/a/6sJPw no :/ – Patryk Panek Feb 14 '18 at 09:42
  • Maybe is it related to that topic : https://github.com/angular/universal/issues/856 ? I'm downloading data from https API, and that problem is very strange. When I'm using Dev Tool to see view-source everything is inside Elements tab, when I'm clicking right mouse button and choose `View site source`, then I see almost empty page (only html tags from index.html). – Patryk Panek Feb 14 '18 at 09:54
  • You have a rule redirecting `http://example.org` to `https://example.org` so don't understand how come you say you have no Location header. You probably have one scenario where SSR is not used and your base index.html is called instead – David Feb 14 '18 at 13:39
  • Yes, but when I'm entering directly to `https://example.org` it should work without any problems, because there is no redirect. Only from http to https. Am I wrong? – Patryk Panek Feb 14 '18 at 14:02
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165161/discussion-between-david-and-patryk-panek). – David Feb 14 '18 at 14:39

0 Answers0