Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix authentication on Glitch #1303

Merged
merged 5 commits into from
May 3, 2022
Merged

Fix authentication on Glitch #1303

merged 5 commits into from
May 3, 2022

Conversation

lfdebrux
Copy link
Member

@lfdebrux lfdebrux commented Apr 29, 2022

Fixes #1302.

Browsersync and Glitch don't work well together. Currently when on Glitch, Browsersync runs, and for reasons I don't understand this stops the authentication working (you can never log in).

As a fix/workaround, this PR changes listen-on-port.js to not use Browsersync if the prototype is running on Glitch. I think this shouldn't be a problem for users, because using Browsersync with Glitch doesn't make much sense (it won't work, and Glitch auto-relaods for you anyway).

With this PR I've tried to follow the maxim of 'leave the code better than you found it', so the way I've approached the fix is to take the logic of determining whether we're running on Glitch, and whether we're running on production, and make a function in utils, with tests, that can be used in all the places we were previously checking environment variables in various different ways.

I also removed some Glitch related code that wasn't needed, as ExpressJS does the same thing for us, and added a comment to config.js making it clear that Browsersync won't run if your prototype is published (whether with Heroku or Glitch).

lib/utils.js Outdated Show resolved Hide resolved
@lfdebrux lfdebrux force-pushed the ldeb-fix-glitch branch 2 times, most recently from 84f8089 to 4a91593 Compare April 29, 2022 15:56
We no longer need this code, as express does the parsing of
x-forwarded-proto for us [[1]].

[1]: https://github.com/expressjs/express/blob/4.17.1/lib/request.js#L316-L323
@lfdebrux
Copy link
Member Author

The tests are currently passing, but Jest is complaining about open handles, due to 'asynchronous operations that weren't stopped in your tests'. I've isolated the issue down to the changes in server.js causing problems with the tests in __tests__/spec/sanity-checks.js... but I have absolutely no idea why these changes would cause a hang like this 😕

@joelanman
Copy link
Contributor

joelanman commented Apr 29, 2022

tested and got this working on Glitch, however .env file for the password didn't exist, which is odd given another file created in start.js - session-data-defaults.js did.

Update

Both files are only created by start.js if they doesn't exist. .env doesn't exist in the repo, session-data-defaults.js exists in the repo (we check in case people update from an old version that didnt have it)

@joelanman
Copy link
Contributor

This is a good fix to have, but in terms of suggesting it as a fix for the heroku situation, I'm not sure we know what the new workflow is. In addition to the .env issue, if someone is collaborating do they all work directly in Glitch or is GitHub involved? If so, how?

@lfdebrux
Copy link
Member Author

lfdebrux commented May 3, 2022

The tests are currently passing, but Jest is complaining about open handles, due to 'asynchronous operations that weren't stopped in your tests'. I've isolated the issue down to the changes in server.js causing problems with the tests in __tests__/spec/sanity-checks.js... but I have absolutely no idea why these changes would cause a hang like this confused

Ah, I see what happened, I assumed that NODE_ENV would be either development or production, forgetting that Jest would be setting it to test. My faulty logic caused Nunjucks watch and Browsersync to run during tests, which is not desirable. Will fix now.

@lfdebrux
Copy link
Member Author

lfdebrux commented May 3, 2022

This is a good fix to have, but in terms of suggesting it as a fix for the heroku situation, I'm not sure we know what the new workflow is. In addition to the .env issue, if someone is collaborating do they all work directly in Glitch or is GitHub involved? If so, how?

Good questions, but let's discuss on Slack, or an a different issue, as it isn't really relevant for this PR!

@lfdebrux
Copy link
Member Author

lfdebrux commented May 3, 2022

tested and got this working on Glitch, however .env file for the password didn't exist, which is odd given another file created in start.js - session-data-defaults.js did.

I think this is expected, the ``template.env file only contains comments, it doesn't set a password.

Update

Both files are only created by start.js if they doesn't exist. .env doesn't exist in the repo, session-data-defaults.js exists in the repo (we check in case people update from an old version that didnt have it)

It might also be that Glitch treats .env specially? It has a little GUI for editing environment variables that appears when you open .env in the Glitch editor.

Refactor code to use `inProduction` function instead of checking
environment variables directly. Hopefully this should a) reduce
duplication b) make programmer intent clearer, thus making maintenance
easier.

This has the side effect of fixing issue #1302, because now we don't run
BrowserStack if serving from Glitch.
Browsersync doesn't play well with Glitch, so now it won't run if your
prototype is on Glitch, even if you have `useBrowserSync: true` in your
config file. I think this shouldn't actually affect users, because using
Browsersync with Glitch doesn't make much sense (it won't work, and
Glitch auto-relaods for you anyway), but this commit adds a comment
anyway to make it clear that Browsersync is only used for development
environments (the config variable has always been ignored when NODE_ENV
is true).
Copy link

@owenatgov owenatgov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking fab 🔒

@lfdebrux lfdebrux merged commit 8b1951e into main May 3, 2022
@lfdebrux lfdebrux deleted the ldeb-fix-glitch branch May 3, 2022 11:27
@lfdebrux lfdebrux mentioned this pull request May 3, 2022
@joelanman
Copy link
Contributor

@lfdebrux just to note, my experience when I tried this was to import the prototype from GitHub. The blank project has no .env, it's not created at runtime and it's not created by Glitch (I know it does as you say in some situations, but it didnt do it for me - maybe part of the import from Github process?).

If this experience is the same for others, at the least we'd need to tell people to make a .env themselves for the password.

@lfdebrux
Copy link
Member Author

If this experience is the same for others, at the least we'd need to tell people to make a .env themselves for the password.

Yeah, we have something explaining this in our draft Glitch docs, similar to what we have in our Heroku docs. See ticket prototype-team-internal#14, or prototype-team-internal#16 for where we are currently with Glitch.

@joelanman
Copy link
Contributor

ah thanks, hadn't seen that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New password page doesn't work on Glitch
3 participants