Skip to content

Commit 276a850

Browse files
committed
feature/Upload user avatar from github
1 parent b4f074e commit 276a850

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

app/controllers/omniauth_callbacks_controller.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ class OmniauthCallbacksController < Devise::OmniauthCallbacksController
33
skip_before_action :verify_authenticity_token, only: :github
44

55
def github
6-
# You need to implement the method below in your model (e.g. app/models/user.rb)
76
@user = User.from_omniauth(request.env["omniauth.auth"])
87

98
if @user.persisted?
10-
flash[:notice] = 'Signed in from GitHub'
11-
sign_in_and_redirect @user# this will throw if @user is not activated
9+
sign_in_and_redirect @user
1210
else
13-
# byebug
14-
# session["devise.github_data"] = request.env["omniauth.auth"].except(:extra) # Removing extra as it can overflow some session stores
15-
redirect_to new_user_registration_url(notice: 'Unable to sign in with Github. Perhaps you already signed up with the associated email?')
11+
redirect_to new_user_registration_url(notice: 'Unable to sign in with Github.')
1612
end
1713
end
1814

app/models/user.rb

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
require 'open-uri'
2+
13
class User < ApplicationRecord
24
include Rails.application.routes.url_helpers
35

@@ -142,7 +144,7 @@ def self.authenticate(params)
142144
end
143145

144146
def self.from_omniauth(auth)
145-
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
147+
user = where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
146148
user.email = auth.info.email
147149
user.password = Devise.friendly_token[0, 20]
148150
user.name = auth.info.nickname # assuming the user model has a name
@@ -151,10 +153,30 @@ def self.from_omniauth(auth)
151153
# uncomment the line below to skip the confirmation emails.
152154
user.skip_confirmation!
153155
end
156+
157+
byebug
158+
159+
if user.persisted?
160+
user.attach_avatar_from_omniauth(auth.info.image)
161+
end
162+
163+
byebug
164+
165+
user
166+
end
167+
168+
def attach_avatar_from_omniauth(avatar_url)
169+
begin
170+
image = open(avatar_url)
171+
172+
avatar.attach(io: image, filename: 'whocares')
173+
rescue
174+
end
154175
end
155176

156177
private
157178

179+
158180
def create_default_folder
159181
folders.create!(name: DEFAULT_FOLDER_NAME)
160182
end

0 commit comments

Comments
 (0)