NoamB/sorcery — pull request #260 by foohey (2012-04-16 16:04:46+00:00)
Title: Added validations for external users & multiple providers by account

Because some providers doesn't give some informations, and in certains cases, user model have other required fields that need to be rendered in the registration form.

Here i use build association for authentications model. If a validation fail, provider information is stored in a session (uid etc...) then in the controller, we can render registration form.

oauth_controller:

```ruby
def callback
provider = params[:provider]
if @user = login_from(provider)
redirect_to profile_index_path, :notice => "Vous êtes désormais connecté ! (via #{provider.titleize})"
else
if logged_in?
@user = add_provider_to_user(provider)
if @user
redirect_to root_path, :notice => "#{provider.titleize} ajouté à votre compte"
else
redirect_to root_path, :notice => "Impossible d'ajouter #{provider.titleize} :("
end
else
@user = create_and_validate_from(provider)
unless @user.errors.size > 0
@user = login_from(provider)
redirect_to profile_index_path, :notice => "Bienvenue !"
else
render "users/registrations/new"
end
end
end
end
````

registrations_controller:

```ruby

def new
@user = User.new((session[:incomplete_user][:user_hash] if session[:incomplete_user]))
end

def create
@user = User.new user_params
@user.authentications.build(session[:incomplete_user][:provider]) if session[:incomplete_user]

if @user.save
session[:incomplete_user] = nil
redirect_to root_url, :notice => "Inscription réussie. Vous allez recevoir un email afin de valider votre compte"
else
render :new
end
end