Can you perform Twitch login without redirecting the page?

My users find it annoying to navigate away from my site in order to login to Twitch. I’ve seen facebook logins done in a modal. Has anybody done this with Twitch API yet?

You can use popups to login, but not modals/iframes. You will need to develop the logic to handle these popups yourself, however.

How could I use a pop up and how is that different from a modal?

You can do login with pop-ups using the Twitch javascript sdk

As an example, this is what I’m using on a page I want the popup to display on. (Make sure to call Twitch.init somewhere as well).

function setAuthToken(token) {
	// Now, do whatever you want with the token.
}

Twitch.login({
    redirect_uri: 'Your Redirect URI',
    scope: ['user_read'],
    popup: true
});

Then I use this in the page that redirect_uri mentions:

<script type="text/javascript">
	if (window.location.hash.indexOf('access_token') != -1) {
		var accessToken = window.location.hash.replace("#access_token=", "");
		accessToken = accessToken.substr(0, accessToken.indexOf('&scope'));
		window.opener.setAuthToken(accessToken);
		window.close();
	} else {
		// This is executed if someone blindly accesses the page.
	}
</script>

I’m not sure if there’s a better way to do this, but this is what has been working for me.

1 Like

Yeah that might be the only way. Thanks for the help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.