Skip to content

Instantly share code, notes, and snippets.

@dandrews
Forked from travisp/invitations_controller.rb
Created April 27, 2017 16:28
Show Gist options
  • Save dandrews/69a901a7e2fe6901512d61a732422333 to your computer and use it in GitHub Desktop.
Save dandrews/69a901a7e2fe6901512d61a732422333 to your computer and use it in GitHub Desktop.
devise_invitable with omniauthable
class InvitationsController < Devise::InvitationsController
# GET /resource/invitation/accept?invitation_token=abcdef
def edit
if params[:invitation_token] && self.resource = resource_class.to_adapter.find_first( :invitation_token => params[:invitation_token] )
session[:invitation_token] = params[:invitation_token]
render :edit
else
set_flash_message(:alert, :invitation_token_invalid)
redirect_to after_sign_out_path_for(resource_name)
end
end
# PUT /resource/invitation
def update
self.resource = resource_class.accept_invitation!(params[resource_name])
if resource.errors.empty?
session[:invitation_token] = nil
set_flash_message :notice, :updated
sign_in(resource_name, resource)
respond_with resource, :location => after_accept_path_for(resource)
else
respond_with_navigational(resource){ render :edit }
end
end
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook/twitter/whatever
auth = request.env["omniauth.auth"]
identity = #find user by omniauth provider and uid
if identity
#do all regular sign in stuff here
else
#no identity found
token = session[:invitation_token]
if token && user = User.where(:invitation_token => token).first
#some app specific code
else
user = User.new
end
user.apply_omniauth(auth)
if user.valid?
if user.invited?
user.accept_invitation!
else
user.save
end
sign_in(:user, user)
redirect_to user_url(user)
else
#app specific code
end
end
session[:invitation_token] = nil
end
devise_for :users,
:controllers => {
:invitations => 'invitations',
:omniauth_callbacks => 'users/omniauth_callbacks'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment