Skip to content

Commit c575e18

Browse files
committed
Allowing users to authenticate with SoundCloud
soundcloud_id, username, and name added to User model. Using Omniauth with Devise, ignoring SSL certs while in dev mode
1 parent bb42561 commit c575e18

File tree

12 files changed

+104
-6
lines changed

12 files changed

+104
-6
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ config/secrets.yml
2929
# if using bower-rails ignore default bower_components path bower.json files
3030
/vendor/assets/bower_components
3131
*.bowerrc
32-
bower.json
32+
bower.json
33+
34+
# dotenv
35+
.env

Gemfile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,20 @@ gem 'jquery-rails', '~> 3.1.0'
1515
gem 'responders', '~> 1.0.0'
1616
gem 'slim-rails', '~> 2.1.4'
1717

18+
#authentication
19+
gem 'omniauth-soundcloud', '~> 1.0.1'
20+
1821
group :test, :development do
1922
gem 'factory_girl_rails'
2023
gem 'pry-rescue'
2124
gem 'simplecov', :require => false
2225
gem 'minitest-reporters'
2326
gem 'faker', '~> 1.4.2', require: false
27+
gem 'minitest-spec-context'
2428
end
2529

2630
group :development do
31+
gem 'dotenv-rails', '~> 1.0.2'
2732
gem 'html2slim'
2833
gem 'thin'
2934
gem 'quiet_assets'
@@ -34,7 +39,7 @@ group :development do
3439
gem 'binding_of_caller'
3540
gem 'spring'
3641
gem 'byebug'
37-
# gem 'foreman'
42+
gem 'foreman'
3843
# gem 'guard', :require => false
3944
# gem 'guard-livereload', :require => false
4045
# gem 'rack-livereload'

Gemfile.lock

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ GEM
6363
thread_safe (~> 0.1)
6464
warden (~> 1.2.3)
6565
docile (1.1.5)
66+
dotenv (1.0.2)
67+
dotenv-rails (1.0.2)
68+
dotenv (= 1.0.2)
6669
erubis (2.7.0)
6770
eventmachine (1.0.3)
6871
excon (0.40.0)
@@ -74,6 +77,8 @@ GEM
7477
railties (>= 3.0.0)
7578
faker (1.4.3)
7679
i18n (~> 0.5)
80+
faraday (0.9.0)
81+
multipart-post (>= 1.2, < 3)
7782
fog (1.24.0)
7883
fog-brightbox
7984
fog-core (~> 1.23)
@@ -109,7 +114,11 @@ GEM
109114
fog-xml (0.1.0)
110115
fog-core
111116
nokogiri (~> 1.5, >= 1.5.11)
117+
foreman (0.63.0)
118+
dotenv (>= 0.7)
119+
thor (>= 0.13.6)
112120
formatador (0.2.5)
121+
hashie (3.3.1)
113122
hike (1.2.3)
114123
hpricot (0.8.6)
115124
html2slim (0.1.0)
@@ -122,6 +131,7 @@ GEM
122131
railties (>= 3.0, < 5.0)
123132
thor (>= 0.14, < 2.0)
124133
json (1.8.1)
134+
jwt (1.0.0)
125135
kgio (2.9.2)
126136
mail (2.6.1)
127137
mime-types (>= 1.16, < 3)
@@ -134,13 +144,32 @@ GEM
134144
builder
135145
minitest (>= 5.0)
136146
ruby-progressbar
147+
minitest-spec-context (0.0.3)
137148
multi_json (1.10.1)
149+
multi_xml (0.5.5)
150+
multipart-post (2.0.0)
138151
net-scp (1.2.1)
139152
net-ssh (>= 2.6.5)
140153
net-ssh (2.9.1)
141154
newrelic_rpm (3.9.6.257)
142155
nokogiri (1.6.3.1)
143156
mini_portile (= 0.6.0)
157+
oauth2 (0.9.4)
158+
faraday (>= 0.8, < 0.10)
159+
jwt (~> 1.0)
160+
multi_json (~> 1.3)
161+
multi_xml (~> 0.5)
162+
rack (~> 1.2)
163+
omniauth (1.2.2)
164+
hashie (>= 1.2, < 4)
165+
rack (~> 1.0)
166+
omniauth-oauth2 (1.1.2)
167+
faraday (>= 0.8, < 0.10)
168+
multi_json (~> 1.3)
169+
oauth2 (~> 0.9.3)
170+
omniauth (~> 1.2)
171+
omniauth-soundcloud (1.0.1)
172+
omniauth-oauth2 (~> 1.1.0)
144173
orm_adapter (0.5.0)
145174
pg (0.17.1)
146175
pry (0.10.1)
@@ -241,12 +270,16 @@ DEPENDENCIES
241270
byebug
242271
cancan (~> 1.6.10)
243272
devise (~> 3.4.0)
273+
dotenv-rails (~> 1.0.2)
244274
factory_girl_rails
245275
faker (~> 1.4.2)
276+
foreman
246277
html2slim
247278
jquery-rails (~> 3.1.0)
248279
minitest-reporters
280+
minitest-spec-context
249281
newrelic_rpm
282+
omniauth-soundcloud (~> 1.0.1)
250283
pg (~> 0.17.1)
251284
pry
252285
pry-rails
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
2+
def soundcloud
3+
@user = User.from_omniauth(request.env["omniauth.auth"])
4+
if @user.persisted?
5+
flash[:notice] = "Signed in!"
6+
sign_in_and_redirect @user, event: :authentication
7+
else
8+
flash[:alert] = "Error logging in"
9+
redirect_to '/'
10+
end
11+
end
12+
end

app/models/user.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
class User < ActiveRecord::Base
22
# Include default devise modules. Others available are:
3-
# :confirmable, :lockable, :timeoutable and :omniauthable
3+
# :confirmable, :lockable, :timeoutable
44
devise :database_authenticatable, :registerable,
5-
:recoverable, :rememberable, :trackable, :validatable
5+
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:soundcloud]
6+
7+
def self.from_omniauth(auth)
8+
user = User.where(soundcloud_id: auth.uid.to_s).first_or_create
9+
user.username = auth.extra.raw_info.username
10+
user.name = auth.info.name
11+
user.password = auth.credentials.token
12+
user.email = user.soundcloud_id + "@soundcloud.com"
13+
user.save
14+
user
15+
end
16+
617
end

config/environments/development.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@
2727
# This option may cause significant delays in view rendering with a large
2828
# number of complex assets.
2929
config.assets.debug = true
30+
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
3031
end

config/initializers/omniauth.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Rails.application.config.middleware.use OmniAuth::Builder do
2+
provider :soundcloud, ENV['SOUNDCLOUD_CLIENT_ID'], ENV['SOUNDCLOUD_SECRET'], {provider_ignores_state: true}
3+
end

config/routes.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Apollo::Application.routes.draw do
2-
devise_for :users
2+
devise_for :users, path_names: {sign_in: "login", sign_out: "logout"}, controllers: {omniauth_callbacks: "omniauth_callbacks"}
33
# The priority is based upon order of creation: first created -> highest priority.
44
# See how all your routes lay out with "rake routes".
55

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AddInfoToUsers < ActiveRecord::Migration
2+
def change
3+
add_column :users, :soundcloud_id, :string
4+
add_column :users, :username, :string
5+
add_column :users, :name, :string
6+
end
7+
end

db/schema.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20141025161136) do
14+
ActiveRecord::Schema.define(version: 20141025165208) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -29,6 +29,9 @@
2929
t.inet "last_sign_in_ip"
3030
t.datetime "created_at"
3131
t.datetime "updated_at"
32+
t.string "soundcloud_id"
33+
t.string "username"
34+
t.string "name"
3235
end
3336

3437
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree

0 commit comments

Comments
 (0)