Userbin for Ruby
Userbin for Ruby adds user authentication, login flows and user management to your Rails, Sinatra or Rack app.
Userbin provides a set of login, signup, and password reset forms that drop right into your application without any need of styling or writing markup. Connect your users via traditional logins or third party social networks. We take care of linking accounts across networks, resetting passwords, and keeping everything safe and secure.
Create a free account at Userbin to start accepting users in your application.
Installation
-
Add the
userbingem to yourGemfileruby gem "userbin" -
Install the gem
shell bundle install -
Configure the Userbin module with the credentials you got from signing up.
In a Rails app, put the following code into a new file at
config/initializers/userbin.rb, and in Sinatra put it in your main application file and addrequire "userbin".ruby Userbin.configure do |config| config.app_id = "YOUR_APP_ID" config.api_secret = "YOUR_API_SECRET" endIf you don’t configure the
app_idandapi_secret, the Userbin module will read theUSERBIN_APP_IDandUSERBIN_API_SECRETenvironment variables. This may come in handy on Heroku. -
Implement getter and setter for your user model.
```ruby # will be called when accessing
current_userconfig.find_user = -> (userbin_id) do User.find_by_userbin_id(userbin_id) }will be called when a user signs up
config.create_user = -> (profile) do User.create! do |user| user.userbin_id = profile.id user.email = profile.email user.photo = profile.image end end ```
For more information about the available attributes in the profile see the Userbin profile documentation.
-
Migrate your users (or do it manually if not using Rails) and add a reference to the Userbin profile:
ruby rails g migration AddUserbinIdToUsers userbin_id:integer:index -
Rack/Sinatra apps only: Activate the Userbin Rack middleware
ruby use Userbin::Authentication
Usage
Authenticating
Rails
Userbin keeps track of the currently logged in user which can be accessed through current_user in controllers, views, and helpers. This automatically taps into libraries such as the authorization solution CanCan.
haml
- if logged_in?
= current_user.email
- else
Not logged in
To set up a controller with user authentication, just add this before_filter:
```ruby class ArticlesController < ApplicationController before_filter :authenticate_user!
def index current_user.articles end end ```
Sinatra
Helpers are accessed through the global Userbin object:
haml
- if Userbin.logged_in?
= Userbin.current_user.email
- else
Not logged in
To set up routes with user authentication, just wrap them like this:
ruby
authenticate_user do
get "/articles" do
"Restricted page that logged in users can access"
end
end
Forms
Ready-made forms
These are opened in a popup.
haml
%a{href: "/account", rel: "login"} Log in
%a{href: "/account", rel: "signup"} Sign up
%a{href: "/", rel: 'logout'} Log out
Social buttons
These require you to first configure your social apps in the dashboard.
haml
%a{href: "/account", rel: "facebook"} Connect with Facebook
%a{href: "/account", rel: "twitter"} Connect with Twitter
Custom forms
Only
haml
%form{action: "/account", name: "signup"}
%span.errors
.form-row
%label
%span E-mail
%input{name: "email", type: "text"}
.form-row
%label
%span Password
%input{name: "password", type: "password"}
%button{type: "submit"} Sign up
Further configuration
skip_script_injection
By default, the Userbin middleware will automatically insert a <script> tag before the closing </body> in your HTML files in order to handle forms, sessions and user tracking. This script loads everything asynchronously, so it won’t affect your page load speed. However if you want to have control of this procedure, set skip_script_injection to true and initialize the library yourself. To do that, checkout the Userbin.js configuration guide.
ruby
config.skip_script_injection = true
Admin dashboard
Your dashboard gives you access to a range of functionality:
- Configure the appearance of the login widget to feel more integrated with your service
- Connect 10+ OAuth providers like Facebook, Github and Google.
- Use Markdown to generate mobile-ready transactional emails
- Invite users to your application
- See who is logging in and when
- User management: block, remove and impersonate users
- Export all your user data from Userbin
Documentation
For complete documentation go to userbin.com/docs


