Reflex is a gem that allows you to connect your application to the React Social API.
Install authlogic (github.com/binarylogic/authlogic) in your rails application, and set it up according to the authlogic docs (rdoc.info/projects/binarylogic/authlogic).
Take a look at the authlogic example app (github.com/binarylogic/authlogic_example), or use it as a basis, if you get stuck.
Add the gem dependencies in your config:
Add the dependencies to your environment:
# config/environment.rb config.gem "reflex"
And install them:
$ rake gems:install
Add the dependencies to your Gemfile
# Gemfile gem "reflex"
And install them:
$ bundle install
Now that we have the reflex gem installed, we need to generate a migration that connects our users to a reflex provider:
$ script/generate reflex_connections_migration
$ rails generate reflex_connections_migration
Create a reflex configuration file:
# config/reflex.yml development: &DEFAULTS key: development_key secret: development_secret endpoint: http://social.react.com/XmlRpc_v2/ production: << DEFAULTS key: production_key secret: production_secret
Save your session objects with a block. Why? Because we need to redirect the user to their login provider so that they can authenticate. When we do this, we don’t want to execute that block of code, because if we do, we will get a DoubleRender error. Using a block allows us to skip that entire block and send the user along their way if they choose to authenticate via React’s OAuth Server.
You probably want to do this in your controllers. It should look something like this:
# app/controllers/user_session_controller.rb def create @user_session.save do |result| if result flash[:notice] = "Login successful!" redirect_back_or_default account_url else render :action => :new end end end
When creating new users, react_profile is set if available. In order to make use of this, alter your user class like this:
# app/models/user.rb class User < ActiveRecord::Base acts_as_authentic def react_profile=(profile) self.name = profile['real_name'] || profile['user_name'] end end
Add providers to your login form:
# app/views/new.html.erb <h1>Log in</h1> <% form_for(@user_session) do |f| %> <%= f.error_messages %> <h2>Log in with your account</h2> <p> <%= f.label :login %> <br> <%= f.text_field :login %> </p> <p> <%= f.label :password %> <br> <%= f.password_field :password %> </p> <p><%= f.submit "Login" %></p> <h2>Or log in via:</h2> <% Reflex::OAuthServer.get_providers.each do |provider| %> <%= f.submit provider, :name => "react_provider" %> <br> <% end %> <% end %>
Ofcourse connecting an existing user to a social network should be as easy and straightforward as authenticating a new user. And luckilly it is!
Modify your user update method to accept a block:
# app/controllers/users_controller.rb def update @user = User.find(params[:id]) @user.attributes = params[:attributes] @user.save do |result| if result flash[:notice] = "User updated successfully!" redirect_to(@user) else render :action => :edit end end end
And add the react providers in the view:
# app/views/users/edit.html.erb <% form_for(@user) do |f| %> <%= f.error_messages %> <p> Connect to a provider: <br> <% Reflex::OAuthServer.get_providers.each do |provider| %> <%= f.submit provider, :name => "react_provider" %> <% end %> </p> <p><%= f.submit "Save" %></p> <% end %>
The reflex gem is far from finished. Here are some of the upcoming features:
-
Easily access the React API — If a user connected to, for example twitter, then it should be a matter of calling: ‘@user.twitter.update_status(“I’ve connected my Twitter profile”)‘
-
Better support for connecting a previously connected user
-
Fork the project.
-
Make your feature addition or bug fix.
-
Add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
-
Send me a pull request. Bonus points for topic branches.
Copyright © 2010 Tom-Eric Gerritsen. See LICENSE for details.