omniauth-twitchtv2 provides a Twitch OAuth2 strategy for OmniAuth.
Add this line to your application's Gemfile:
gem 'omniauth-twitchtv2'Then run:
bundle installConfigure OmniAuth in your Rack/Rails app:
Rails.application.config.middleware.use OmniAuth::Builder do
provider :twitchtv,
ENV.fetch('TWITCH_CLIENT_ID'),
ENV.fetch('TWITCH_CLIENT_SECRET'),
scope: 'user:read:email'
end- Twitch developer console: https://dev.twitch.tv/console/apps
- OAuth docs: https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/
- Register callback URL (example):
https://your-app.example.com/auth/twitchtv/callback
scope(for exampleuser:read:email)force_verify
Example payload from request.env['omniauth.auth'] (realistic shape, anonymized):
{
"uid": "12345678",
"info": {
"name": "sample_user",
"nickname": "sample_user",
"email": "sample@example.test",
"image": "https://static-cdn.jtvnw.net/jtv_user_pictures/example-profile_image-300x300.png",
"description": "Streaming something cool",
"urls": {
"twitchtv": "https://www.twitch.tv/sample_user"
}
},
"credentials": {
"token": "sample-access-token",
"refresh_token": "sample-refresh-token",
"expires_at": 1710000000,
"expires": true,
"scope": "user:read:email"
},
"extra": {
"raw_info": {
"id": "12345678",
"login": "sample_user",
"display_name": "sample_user",
"type": "",
"broadcaster_type": "",
"description": "Streaming something cool",
"profile_image_url": "https://static-cdn.jtvnw.net/jtv_user_pictures/example-profile_image-300x300.png",
"offline_image_url": "",
"view_count": 42,
"email": "sample@example.test",
"created_at": "2020-01-01T00:00:00Z"
}
}
}info.email is returned only when your app requests a scope that exposes email (for example user:read:email).
bundle install
bundle exec rakeRun Rails integration tests with an explicit Rails version:
RAILS_VERSION='~> 8.1.0' bundle install
RAILS_VERSION='~> 8.1.0' bundle exec rake test_rails_integrationtest/omniauth_twitchtv_test.rb: strategy/unit behaviortest/rails_integration_test.rb: full Rack/Rails request+callback flowtest/test_helper.rb: shared test bootstrap
- Ruby:
>= 3.2(tested on3.2,3.3,3.4,4.0) omniauth-oauth2:>= 1.8,< 2.0- Rails integration lanes:
~> 7.1.0,~> 7.2.0,~> 8.0.0,~> 8.1.0
Tag releases as vX.Y.Z; GitHub Actions publishes the gem to RubyGems.
MIT