I’m not sure if I’ve talked at all about how much I love the programming framework Sinatra. It’s fun and “gettable”, especially for someone approaching programming from the perspective of a front-end developer.
But — like Rails — Sinatra doesn’t come with an out-of-the-box user authentication program. You have to code up the whole “create an account / sign in / sign out” bit for each app, or use a gem or plugin (most of which are optimized for Rails). I wanted something “Sinatra-y”, though — simple, un-fussy, and easily understandable. Nothing else felt right, so I made something to do the job. It uses the wonderful (and flexible) plugin OmniAuth, which means that as long as your user has an account at Twitter (or Facebook, or whatever provider you want), they can have an account at your app. And since all of the code (except for the black box code in OmniAuth) is in the single Ruby file, you can see what’s going on.
You can check it out at Github: OmniAuth for Sinatra. What you’ll see is that it’s simply a template for getting rolling with a new app. You need to get a set of OAuth keys from Twitter, but that’s all you need. It just works. Here’s the writeup from Github:
This is a template for launching Sinatra apps quickly and easily, using OmniAuth as the authentication protocol. It differs from most of the “use Twitter with Sinatra” code bases, as it incorporates sessions and a User model in the database. This means your user should remain logged in as long as your server stays live.
The instructions here assume you’re using Twitter, but you can use any auth provider handled by OmniAuth.
Most of the actual code here is simply a port/mashup of Ryan Bates’s example from RailsCasts #241: Simple OmniAuth and the OmniAuth readme.
Once you have your auth codes, getting a new Sinatra app with authentication working is simply a matter of copying the app.rb file from here into a new directory, pasting in your auth codes, and then starting in on your app’s code. It just works.
Getting Started Instructions
1. Install Omniauth and the other required gems
gem install omniauthYou’ll also need to install data_mapper and dm-sqlite-adapter if you don’t have them in your system.
2. Get your Twitter Auth Keys
At Twitter, register for a new app. Note the Consumer Key and Consumer Secret strings it gives you.
3. Enter your Auth Keys into the app.rb file
The file app.rb has a place for your CONSUMER_KEY and CONSUMER_SECRET. Just replace them, and you’re set. Fire up your local server (‘ruby app.rb’) and you can see your log-in setup.
A Minor Note
Note that you cannot use shotgun to test your app, as you’ll need sessions to persist in order to stay logged in, and shotgun restarts the server on every request. Starting your app with ‘ruby app.rb’ should work with no problems.
Feedback Is Awesome
I’d really love to hear any suggestions you have on how to improve this. On Twitter, I’m @charliepark. By e-mail, I’m charlie@monotask.com.
So there you go. Have fun!
