Merge pull request #4038 from markbiegel/instagram-login

FEATURE: Instagram OmniAuth login methods
This commit is contained in:
Sam 2016-03-07 14:05:07 +11:00
commit fe6ea48123
12 changed files with 94 additions and 2 deletions

View file

@ -80,6 +80,7 @@ gem 'omniauth-openid'
gem 'openid-redis-store'
gem 'omniauth-facebook'
gem 'omniauth-twitter'
gem 'omniauth-instagram'
# forked while https://github.com/intridea/omniauth-github/pull/41 is being upstreamd
gem 'omniauth-github-discourse', require: 'omniauth-github'

View file

@ -37,6 +37,7 @@ Discourse.LoginMethod.reopenClass({
"cas",
"twitter",
"yahoo",
"instagram",
"github"
].forEach(function(name){
if (Discourse.SiteSettings["enable_" + name + "_logins"]) {

View file

@ -129,6 +129,12 @@
content: $fa-var-google;
}
}
&.instagram {
background: $instagram;
&:before {
content: $fa-var-instagram;
}
}
&.facebook {
background: $facebook;
&:before {

View file

@ -13,6 +13,7 @@ $large-width: 1110px !default;
// --------------------------------------------------
$google: #5b76f7 !default;
$instagram: #125688 !default;
$facebook: #3b5998 !default;
$cas: #70BA61 !default;
$twitter: #00bced !default;

View file

@ -10,7 +10,8 @@ class Users::OmniauthCallbacksController < ApplicationController
Auth::GoogleOAuth2Authenticator.new,
Auth::OpenIdAuthenticator.new("yahoo", "https://me.yahoo.com", trusted: true),
Auth::GithubAuthenticator.new,
Auth::TwitterAuthenticator.new
Auth::TwitterAuthenticator.new,
Auth::InstagramAuthenticator.new
]
skip_before_filter :redirect_to_login_if_required
@ -18,7 +19,7 @@ class Users::OmniauthCallbacksController < ApplicationController
layout false
def self.types
@types ||= Enum.new(:facebook, :twitter, :google, :yahoo, :github, :persona, :cas)
@types ||= Enum.new(:facebook, :instagram, :twitter, :google, :yahoo, :github, :persona, :cas)
end
# need to be able to call this

View file

@ -0,0 +1,5 @@
class InstagramUserInfo < ActiveRecord::Base
belongs_to :user
end

View file

@ -895,6 +895,9 @@ en:
twitter:
title: "with Twitter"
message: "Authenticating with Twitter (make sure pop up blockers are not enabled)"
instagram:
title: "with Instagram"
message: "Authenticating with Instagram (make sure pop up blockers are not enabled)"
facebook:
title: "with Facebook"
message: "Authenticating with Facebook (make sure pop up blockers are not enabled)"

View file

@ -941,6 +941,10 @@ en:
twitter_consumer_key: "Consumer key for Twitter authentication, registered at http://dev.twitter.com"
twitter_consumer_secret: "Consumer secret for Twitter authentication, registered at http://dev.twitter.com"
enable_instagram_logins: "Enable Instagram authentication, requires instagram_consumer_key and instagram_consumer_secret"
instagram_consumer_key: "Consumer key for Instagram authentication"
instagram_consumer_secret: "Consumer secret Instagram authentication"
enable_facebook_logins: "Enable Facebook authentication, requires facebook_app_id and facebook_app_secret"
facebook_app_id: "App id for Facebook authentication, registered at https://developers.facebook.com/apps"
facebook_app_secret: "App secret for Facebook authentication, registered at https://developers.facebook.com/apps"

View file

@ -234,6 +234,15 @@ login:
twitter_consumer_secret:
default: ''
regex: "^[a-zA-Z0-9_+-]+$"
enable_instagram_logins:
client: true
default: false
instagram_consumer_key:
default: ''
regex: "^[a-z0-9]+$"
instagram_consumer_secret:
default: ''
regex: "^[a-z0-9]+$"
enable_facebook_logins:
client: true
default: false

View file

@ -0,0 +1,11 @@
class CreateInstagramUserInfos < ActiveRecord::Migration
def change
create_table :instagram_user_infos do |t|
t.integer :user_id
t.string :screen_name
t.integer :instagram_user_id
t.timestamps null: false
end
end
end

View file

@ -7,3 +7,4 @@ require_dependency 'auth/open_id_authenticator'
require_dependency 'auth/github_authenticator'
require_dependency 'auth/twitter_authenticator'
require_dependency 'auth/google_oauth2_authenticator'
require_dependency 'auth/instagram_authenticator'

View file

@ -0,0 +1,49 @@
class Auth::InstagramAuthenticator < Auth::Authenticator
def name
"instagram"
end
# TODO twitter provides all sorts of extra info, like website/bio etc.
# it may be worth considering pulling some of it in.
def after_authenticate(auth_token)
result = Auth::Result.new
data = auth_token[:info]
result.username = screen_name = data["nickname"]
result.name = name = data["name"].slice!(0)
instagram_user_id = auth_token["uid"]
result.extra_data = {
instagram_user_id: instagram_user_id,
instagram_screen_name: screen_name
}
user_info = InstagramUserInfo.find_by(instagram_user_id: instagram_user_id)
result.user = user_info.try(:user)
result
end
def after_create_account(user, auth)
data = auth[:extra_data]
InstagramUserInfo.create(
user_id: user.id,
screen_name: data[:instagram_screen_name],
instagram_user_id: data[:instagram_user_id]
)
end
def register_middleware(omniauth)
omniauth.provider :instagram,
:setup => lambda { |env|
strategy = env["omniauth.strategy"]
strategy.options[:client_id] = SiteSetting.instagram_consumer_key
strategy.options[:client_secret] = SiteSetting.instagram_consumer_secret
}
end
end