Skip to content

Commit 973e341

Browse files
authored
Add alfred omniauth provider (#11)
* Add alfred omniauth provider * Fix creating account for alfred provider
1 parent bc98123 commit 973e341

File tree

7 files changed

+41
-6
lines changed

7 files changed

+41
-6
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
POSTGRES_USER=
2+
POSTGRES_PASSWORD=
3+
POSTGRES_DB=
4+
ALFRED_KEY=
5+
ALFRED_SECRET=

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ gem 'devise'
4545
# Authorization
4646
gem 'cancancan'
4747
gem 'omniauth-github'
48+
gem "omniauth-alfred", git: 'https://github.com/cybergizer-hq/omniauth-alfred', branch: 'master'
4849
gem 'slack-notifier'
4950

5051
gem 'aasm'

Gemfile.lock

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
GIT
2+
remote: https://github.com/cybergizer-hq/omniauth-alfred
3+
revision: 9cbda091493b3a68fe7d5136d91199db32741ff5
4+
branch: master
5+
specs:
6+
omniauth-alfred (0.0.0)
7+
omniauth (~> 1.0)
8+
omniauth-oauth2 (~> 1.0)
9+
110
GEM
211
remote: https://rubygems.org/
312
specs:
@@ -350,6 +359,7 @@ DEPENDENCIES
350359
kaminari
351360
listen (>= 3.0.5, < 3.2)
352361
oj
362+
omniauth-alfred!
353363
omniauth-github
354364
pg (>= 0.18, < 2.0)
355365
pry-byebug

app/controllers/omniauth_callbacks_controller.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,9 @@ def github
55
@account = Account.from_omniauth(request.env['omniauth.auth'])
66
sign_in_and_redirect @account
77
end
8+
9+
def alfred
10+
@account = Account.from_omniauth(request.env['omniauth.auth'])
11+
sign_in_and_redirect @account
12+
end
813
end

app/models/account.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,21 @@ class Account < ApplicationRecord
2020
def self.from_omniauth(auth)
2121
where(provider: auth.provider, uid: auth.uid).first_or_create do |account|
2222
account.provider = auth.provider
23-
account.name = auth.info.name
24-
account.surname = auth.info.nickname
23+
account.name = account.auth_provider_name(auth)
24+
account.surname = account.auth_provider_surname(auth)
2525
account.uid = auth.uid
2626
account.email = auth.info.email
2727
account.password = Devise.friendly_token[0, 20]
28-
account.github_avatar(auth)
28+
account.auth_provider_avatar(auth)
2929
end
3030
end
3131

32-
def github_avatar(auth)
33-
return unless auth.info.image.present?
32+
def auth_provider_avatar(auth)
33+
image_url = auth.provider == :alfred ? auth.info.avatar_url : auth.info.image
3434

35-
downloaded_image = URI.parse(auth.info.image).open
35+
return unless image_url.present?
36+
37+
downloaded_image = URI.parse(image_url).open
3638

3739
avatar.attach(
3840
io: downloaded_image,
@@ -42,6 +44,14 @@ def github_avatar(auth)
4244
end
4345
# rubocop: enable Metrics/AbcSize
4446

47+
def auth_provider_name(auth)
48+
auth.provider == :alfred ? auth.info.first_name : auth.info.name
49+
end
50+
51+
def auth_provider_surname(auth)
52+
auth.provider == :alfred ? auth.info.last_name : auth.info.nickname
53+
end
54+
4555
def full_name
4656
"#{surname} #{name}"
4757
end

app/views/accounts/sessions/new.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
<%= link_to account_github_omniauth_authorize_path, class: "button is-medium is-black" do %>
2020
<ion-icon name="logo-github"></ion-icon> Sign Up With Github
2121
<% end %>
22+
<%= link_to account_alfred_omniauth_authorize_path, class: "button is-medium is-blue" do %>
23+
Sign Up With Alfred
24+
<% end %>
2225
</div>
2326
</div>
2427
<% end %>

config/initializers/devise.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@
260260
# Add a new OmniAuth provider. Check the wiki for more information on setting
261261
# up on your models and hooks.
262262
config.omniauth :github, Rails.application.credentials.github[:api], Rails.application.credentials.github[:secret], scope: Rails.application.credentials.github[:scope]
263+
config.omniauth :alfred, ENV['ALFRED_KEY'], ENV['ALFRED_SECRET'], scope: 'user'
263264

264265
# ==> Warden configuration
265266
# If you want to use other strategies, that are not supported by Devise, or

0 commit comments

Comments
 (0)