redirect with google_oauth2 on validation - ruby-on-rails

I got a validation on my model to allow only some emails to log in with GoogleOauth but for some reasons, it does not redirect me to the root path
I got my admin Model :
class Admin < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :confirmable, :lockable, :registerable, :rememberable, :recoverable, :timeoutable,
:trackable, :validatable
devise :omniauthable, omniauth_providers: [:google_oauth2]
validates :email, :test_email
def self.from_omniauth(auth)
admin = Admin.find_or_initialize_by(email: auth.info.email)
admin.password = Devise.friendly_token.first(12)
admin.skip_confirmation!
admin.save!
admin
end
private
def test_email
errors.add(:email, message: 'Email is not correct') unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
end
end
And my OmniauthCallbacksController :
def google_oauth2
#admin = Admin.from_omniauth(request.env['omniauth.auth'])
if #admin.persisted?
flash[:notice] = 'success'
sign_in_and_redirect #admin
else
session['devise.google_data'] = request.env['omniauth.auth'].except('extra')
flash[:error] = #admin.errors.full_messages.join("\n")
redirect_to root_path
end
end
end

validates expects a boolean return. Just adding an error is not enough.
Try this:
def test_email
return true unless email.match?(/\A[a-z.\-]+(\+\d+)?#test\.fr?\z/)
errors.add(:email, message: 'Email is not correct')
false
end

Related

Devise sending welcome email after confirmation

I'm using rails 5.2 and devise 4.4.3, I have confirmable working, however, I'm trying to have it so when the user confirms their account they get sent another email welcoming them to the website.
On sign up the confirmation email sends and confirmation can take place by clicking the link in the email but the welcome email that I want to be sent after this currently doesn't send.
The code I have is like so, I have generated a UserMailer and done the following
user.rb model
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
has_one_attached :avatar
def confirm!
welcome_email
super
end
protected
def welcome_email
UserMailer.welcome_email(self).deliver
end
end
views/user_mailer/welcome_email.html.erb
<h2>Welcome <%= #user.email %></h2>
mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
#user = user
mail(to: #user.email, subject: "Welcome! You are awesome!")
end
end
You can use the after_confirmation callback instead of trying to override confirm.
def after_confirmation
UserMailer.welcome_email(self).deliver
end
def after_confirmation
UserMailer.with(user: #user, password: #user.password).welcome_email.deliver_later
end

reddit-omniauth gem getting " you sent an invalid request — invalid scope requested"

I am trying to implement reddit login in rails application using omniauth-reddit
login is succesfull but i don't no where its going wrong
My OmniauthCallbacksController.rb file
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def reddit_oauth2
#user = User.from_omniauth(request.env["omniauth.auth"])
sign_in_and_redirect root_path
end
My User Model
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
devise :omniauthable, :omniauth_providers => [:reddit]
def self.from_omniauth(auth)
where(provider: auth.provider,uid: auth.id).first_or_create do |user|
user.provider = auth.provider
user.id = auth.id
user.password = Devise.friendly_token[0,20]
end
end
end
can anybody suggest me scope or solution ?? Thanks in Advance

Devise Sending Welcome Email

I have a devise model called members i am using devise confirmable. Upon confirm i want to send a welcome email to the User
class Member < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :confirmable
# Methods
# Override devise confirm! message
def confirm!
welcome_email
super
end
# Private Methods
private
def welcome_email
MemberMailer.welcome_email(self).deliver
end
end
My MemberMailer resides in mailers/brands/member_mailer.rb
class Brands::MemberMailer < ApplicationMailer
# Send Welcome Email once Member confirms the account
def welcome_email(member)
#member = member
mail(to: #member.email, subject: "Welcome to Skreem! Now you Rock!")
end
end
But upon confirming through the mail Link the confirm! is not being overridden and I am not getting any error or email.
Add this to your Member model:
def after_confirmation
welcome_email
end
For more info check after_confirmation
#Pavan thanks for pointing this.
Your welcome_email should be:
def welcome_email
Brands::MemberMailer.welcome_email(self).deliver
end

Omniauth callback processing after authorization

In my Rails app, I have user and authorization tables to handle users and auth data. I set up both Devise and Omniauth to use Twitter to sign up, it redirects to Twitter, but after returning to my app, it gives an error like:
NoMethodError at /users/auth/twitter/callback
undefined method `authorizations' for #<Class:0xbdc8100>
In which side, did I go wrong and how can I fix this issue?
Here are related parts: omniauth_callbacks_controller.rb:
class OmniauthCallbacksController < Devise::OmniauthCallbacksController
def all
user = User.authorizations.from_auth(auth_hash)
if user.persisted?
flash.notice = "Signed in!"
sign_in_and_redirect user
else
session["devise.user_attributes"] = user.attributes
redirect_to new_user_registration_url
end
end
alias_method :twitter, :all
protected
def auth_hash
request.env['omniauth.auth']
end
end
authorization.rb:
class Authorization < ActiveRecord::Base
attr_accessible :uid, :provider
belongs_to :user
def self.from_auth(auth)
where(auth.slice(:provider, :uid)).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
end
end
user.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:twitter, :facebook]
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :name
# attr_accessible :title, :body
has_many :authorizations, dependent: :destroy
end
Your issue is in this line...
user = User.authorizations.from_auth(auth_hash)
You call authorizations on the class User, but as an attribute it needs to be called on an instance of the User class, i.e. a specific user.

devise_invitable: Confirm after Invitation

I override devise's confirm! method to send a welcome message to my users:
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
# ...
# Devise confirm! method overriden
def confirm!
UserMailer.welcome_alert(self).deliver
super
end
end
With devise_invitable when the user accept the invitation and set his password the confirm! method is never triggered, is it possible to force it? How does devise_invitable confirms the User?
Or maybe I can override the accept_invite (or whatever its called) method the same way?
I want that invited users remain unconfirmed, and then confirmed upon accepting the invitation.
Thanks, any help very appreciated!
Original Source
UPDATE
Looking through devise_invitable model I found the two methods who may be causing this misbehavior:
# Accept an invitation by clearing invitation token and confirming it if model
# is confirmable
def accept_invitation!
if self.invited? && self.valid?
self.invitation_token = nil
self.save
end
end
# Reset invitation token and send invitation again
def invite!
if new_record? || invited?
#skip_password = true
self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!)
generate_invitation_token if self.invitation_token.nil?
self.invitation_sent_at = Time.now.utc
if save(:validate => self.class.validate_on_invite)
self.invited_by.decrement_invitation_limit! if self.invited_by
!!deliver_invitation unless #skip_invitation
end
end
end
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
# ...
# devise confirm! method overriden
def confirm!
welcome_message
super
end
# devise_invitable accept_invitation! method overriden
def accept_invitation!
self.confirm!
super
end
# devise_invitable invite! method overriden
def invite!
super
self.confirmed_at = nil
self.save
end
private
def welcome_message
UserMailer.welcome_message(self).deliver
end
end
I tried benoror's answer and at first it appeared to work - but when you a user accepts the invitation and fills in the form as invalid it will actually override the token invalidating the invitation.
Instead, a callback is available to do this:
class User < ActiveRecord::Base
devise :invitable, :database_authenticatable, :registerable, :recoverable,
:rememberable, :confirmable, :validatable, :encryptable
after_invitation_accepted :send_welcome_email
def send_welcome_email
end
end

Resources