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

Admin login not working? #3

Closed
Furgas opened this issue Oct 27, 2017 · 17 comments
Closed

Admin login not working? #3

Furgas opened this issue Oct 27, 2017 · 17 comments

Comments

@Furgas
Copy link

Furgas commented Oct 27, 2017

I published the site (without any modification after creating) on Netlify (just by dropping the public folder), and enabled the Identity service (with gitlab provider). After logging in with Gitlab account I'm redirected to main site page (with OAuth access_token) but it didn't redirect me back to admin interface.
I had to add

<script src="https://identity.netlify.com/v1/netlify-identity-widget.js"></script>

in <head>
and

<script>
  if (window.netlifyIdentity) {
    window.netlifyIdentity.on("init", user => {
      if (!user) {
        window.netlifyIdentity.on("login", () => {
          document.location.href = "/admin/";
        });
      }
    });
  }
</script>

before <body> end in main site index.html just as the documentation says. Shouldn't you make gatsby add it automatically to public/index.html?

Nonetheless, admin interface now loads, but I fail to see any content that is already present on the site. But maybe it's because the site was not created from Git.

@AustinGreen
Copy link
Contributor

Thanks for the catch, my latest commit should fix it. Let me know if it's still not working.

@AustinGreen
Copy link
Contributor

Actually, I can't seem to get the redirect to the admin after logging in to work. The init event never fires. Looks like this is a known issue. Were you able to get it working?

@Furgas
Copy link
Author

Furgas commented Oct 28, 2017

It works here.

@AustinGreen
Copy link
Contributor

Yes it's working there, do you mind sharing where you included this in your gatsby project:

<script>
  if (window.netlifyIdentity) {
    window.netlifyIdentity.on("init", user => {
      if (!user) {
        window.netlifyIdentity.on("login", () => {
          document.location.href = "/admin/";
        });
      }
    });
  }
</script>

@Furgas
Copy link
Author

Furgas commented Oct 28, 2017

I've added it to the generated index.html by hand, just to check if this will work. I'm gatsby newbie, so I dont' know yet how to configure gatsby to do it.

@Furgas
Copy link
Author

Furgas commented Oct 28, 2017

I'm also React newbie, but I've managed to find this ugly solution [1] [2]:

  1. cp .cache/default-html.js src/html.js
  2. Edit html.js and add before </body>:
<script dangerouslySetInnerHTML={{ __html:
	`if (window.netlifyIdentity) {
		window.netlifyIdentity.on("init", user => {
			if (!user) {
				window.netlifyIdentity.on("login", () => {
					document.location.href = "/admin/";
				});
			}
		});
	}`
}}
/>

[1] https://www.gatsbyjs.org/docs/custom-html/
[2] https://stackoverflow.com/a/39685948/170403

@AustinGreen
Copy link
Contributor

I was hoping to find another solution because editing the html.js means the script would appear on every page. Looks like others are struggling with this as well decaporg/decap-cms#600 (comment)

@Furgas
Copy link
Author

Furgas commented Oct 28, 2017

I see.
The linked struggle is with not redirecting to admin (only showing modal box). I'm not experiencing it with this hack - the modal box is shown for a moment but the redirect kicks in just fine.

@Furgas
Copy link
Author

Furgas commented Oct 28, 2017

Another ugly hack, I've moved that inline-before-body script to src/pages/index.js. It works.

@erquhart
Copy link
Contributor

erquhart commented Nov 7, 2017

@AustinGreen source issue here: netlify/netlify-identity-widget#86

@AlvaroR156
Copy link

I have worked it out. I think that the issue is that you need to deploy the app again after you have finished activating the Identity service, I did just a simple text change and publish. Refresh and it works.

@luczaki114
Copy link
Contributor

luczaki114 commented Nov 19, 2017

I seem to be having an issue with netlify identity as well.

When I go to /admin and login, I get redirected to the home page with a json token in the URL. I tried to login again, and keep returning to the home page with the token. It turns out that the

if (window.netlifyIdentity) {
	window.netlifyIdentity.on("init", user => {
		if (!user) {
			window.netlifyIdentity.on("login", () => {
				document.location.href = "/admin/";
			});
		}
	});
}

script is not actually running. An async callback does the trick, making sure the CDN netlify identity script runs before adding this on init function. netlifyIdentity also needs to be manually initialized with netlifyIdentity.init() after the on init eventhandler is passed. Once you have called the init() function with the token in the URL, you should get the logged in modal popup.

Side note: I got the CMS to work locally as well, by passing in the name "test-repo" in config.yml, which lets you use the browsers' local storage to test whether your configuration is bug free. This will not let you actually publish to your github repo, so its really just for testing config.

Question: Austin, since you have both index.html and config.ym is static/admin, did you have to customize Gatsby's webpack config to route to admin? it seems to only work in production. I took you boilerplate and added in the admin page as a widget. Any reason you didn't go that route?

@AustinGreen
Copy link
Contributor

@luczaki114 Great, I'll give this a try, or feel free to submit a PR! I've tried both options, and I like having the admin in the static directory. You can still test locally on localhost:9000 after a gatsby build and gatsby serve. It keeps these files separate from your public facing site. Just a personal preference though.

@luczaki114
Copy link
Contributor

I was able to test locally on 9000, but netlify notices that, asks me to link my site URL, and then redirects me to that sites admin page, which is not what i want. I would love to be able to play with the config file on local without a redirect. Are you experiencing that?

@AustinGreen
Copy link
Contributor

No, I'm able to access the admin locally. Try going to localhost:9000 in an incognito window.

@luczaki114
Copy link
Contributor

I believe this issue can be closed after that last merge.

@AustinGreen
Copy link
Contributor

Thanks for the PR @luczaki114

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

No branches or pull requests

5 participants