Can you perform Twitch login without redirecting the page?

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