Jump to content

The ultimate community for Ruby on Rails developers.


Model won't validate when created from custom method create_from _hash

omiauth activemodel SecurePassword facebook

  • Please log in to reply
1 reply to this topic

#1 rccursach



  • Members
  • 3 posts

Posted 05 February 2014 - 03:58 AM

  I'm trying to mix a custom User authentication mechanism based on SecurePassword with Facebook integration through omniauth-facebook gem.

my app uses Ruby 2.0.0 and Rails 4.0.0.
i tried to follow this guide omniauth and some other articles to came up with something like this for the User and Authentication Models
User model:
class User < ActiveRecord::Base

has_one :user_playlist
has_one :user_info

has_many :band_likes
has_many :song_likes

has_many :band_comments
has_many :song_comments

has_many :authorizations

#many to many relation between User and Band
#todo: make a bands_users migration
has_and_belongs_to_many :bands


validates :username, presence: true, uniqueness: {case_sensitive: false}, length: {in: 8..64}, format: {with: /\A[a-zA-Z ]+\Z/, message: 'Debe poseer solo letras y espacios.'}
validates :email, presence: true, uniqueness: {case_sensitive: false}, format: {with: /@/, message: 'Dirección de correo inváilda.'}
validates :password, length: {in: 8..24}
validates :password_confirmation, length: {in: 8..24}

def self.create_from_hash!(hash)
        create(:email => hash['info']['email'], :username => hash['info']['name'], :password => hash['uid'], :password_confirmation => hash['uid'] )
Authorization Model:
class Authorization < ActiveRecord::Base
belongs_to :user
validates_presence_of :user_id, :uid, :provider
validates_uniqueness_of :uid, :scope => :provider

def self.find_from_hash(hash)
  find_by_provider_and_uid(hash['provider'], hash['uid'])

def self.create_from_hash(hash, user = nil)
  user ||= User.create_from_hash!(hash)
  Authorization.create(:user => user, :uid => hash['uid'], :provider => hash['provider'])
class SessionsController < ApplicationController

def create
  auth = request.env['omniauth.auth']
  unless @auth = Authorization.find_from_hash(auth)
    # Create a new user or add an auth to existing user, depending on
    # whether there is already a user signed in.
    @auth = Authorization.create_from_hash(auth, current_user)
  # Log the authorizing user in.
  self.current_user = @auth.user

  render :text => "Welcome, #{current_user.username}. <br />User saved = #{current_user.save} .<br/>User valid = #{current_user.valid?}.<br />errors= #{current_user.errors.full_messages}"
The last render was written to check about the fact that my password does not gets validated, it doesn't matter if i use hash['uid'], hash['info']['name'], or whatever.
The reason why i use this values is just because, i will figure out later how to build a random password for the oauth-ed user, but i don't want blank ones nor disable the validations.

But, no matter what value i use, always get only my name and email:

Welcome, (My Real Name Here).
User saved = false.
User valid = false.
errors= ["Password is too short (minimum is 8 characters)", "Password confirmation is too short (minimum is 8 characters)"]

When creating users in Rails Console got no problem, just when OAuth tries to create a User with create_from_hash.
also, if i try to assign a non existing value from hash to password fields, it adds the message that can be blank. so, it isn't blank.
and rendering hash['uid'] in controller shows that it's longer than 8.

I Must warn that i'm new to rails, so if you can, explain me with apples xD
Thanks in advance!

#2 rccursach



  • Members
  • 3 posts

Posted 05 February 2014 - 07:23 AM   Best Answer

finally i came up with this on User model:

If something is wrong or can be done better i'm up to hear read opinions.

def self.create_from_hash!(hash)
   self.where(:email => hash.info.email.to_s).first_or_create do |user|
      user.email = hash.info.email
      user.username = hash.info.name
      user.password = hash.uid
      user.password_confirmation = hash.uid

Anyways, it's not tested at all, so if it works i'll mark it as solved. maybe some other rails noobs like me will be lost the same way in some future, hope not.

Good Night !

Also tagged with one or more of these keywords: omiauth, activemodel, SecurePassword, facebook

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users