Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

codeA guest post by Guy Royse, who works for Pillar Technology in Columbus, Ohio as an instructor, a consultant, and a software engineer. He has programmed in numerous languages — many of them semi-colon delimited — but has more recently been working with Ruby and JavaScript. He is also the chief organizer for the Columbus JavaScript User Group and is active in the local development community.

Meteor has excellent support for authentication. Meteor has built in capabilities to log users in with very little code for classic users, for password authentication and authentication using third party login services including Facebook, GitHub, Google, Meetup, Twitter, and Weibo. Today, I’m going to show you how to allow your users to login to your Meteor application using Twitter.

If you haven’t read them already, check out my Killing Dinosaurs with Meteor and More Killing Dinosaurs with Meteor posts.

Getting Started

First off, you will need an account on Twitter. Not a special developer account, just an account. So, if you don’t have a Twitter account, go to and get one now. Once you have an account you’ll need to to go to Twitter’s developers site and login.

Once you are logged in, you will see links to documentation, blogs, and all sorts of developer resources. These links, while prominent and useful, are not what you need to get started. Instead, you need to click on your profile image in the top right and select “My applications” from the drop-down.

If you have any applications you have linked to Twitter with in the past, they will be listed here. Otherwise this page will be a bit sparse. Regardless, there is a “Create New App” button that you want to click to create your application.

You are now asked for four bits of required information: name, description, website, and callback URL. The first three are intended to be information to the users of your application and will show up when they login. The last one, the callback URL, is not used by Meteor but is required for Twitter to work with Meteor. I used my personal website for all of the URLs I used. You should probably use yours instead of mine.

Once these are entered, carefully read the long box of legalese for as many seconds as it takes and check “Yes, I agree.” Then, click the friendly “Create your Twitter application” button at the bottom of the page. Excellent, you have created an application in Twitter.

Meteor requires two fields from Twitter to function. Twitter refers to these as the API key and the API secret, while Meteor calls them the consumer key and the secret. You can find these fields on the API Keys tab immediately after creating your application. Just click API Keys and make a note of them.

You now have everything you need from Twitter to allow integration authentication with your Meteor application. Now, on to creating that application.

Creating the Application

You’ll need to create a Meteor application in the usual way. Be sure after you create it to add the Twitter accounts package to your application using the following command.

You’ll also need some start up code to configure Twitter for your application.

When your Meteor application starts, it will remove the existing configuration (if it exists) and then insert a new configuration with your keys. Put this code in the server folder of your application so it is not served up to consumers. Your secret key should be, as the name implies, kept secret. I put mine in a file named startup.js.

You’ll also need to add some UI elements. You can use the {{currentUser}} value from within Handlebars to determine if someone is logged in by checking to see if it is defined. If it is defined, they are logged in and you can retrieve information about them.

This code displays a Sign In button if you are not logged in via the “login” template. If you are logged in, it displays a Sign Out button and assorted data gleaned from Twitter about you.

The JavaScript code to login and logout is about as simple as it gets. To login, simply call Meteor.loginWithTwitter() in any event handler. To logout, call Meteor.logout().


And that’s pretty much it. Run your application and click the Sign in button. You should be presented with a pop-up from Twitter asking you to authorize this application. Do so and you’ll be logged into the application, which will proceed to display information about your Twitter account.

Meteor is capable of a lot more than we have room for in this post. For more information, checkout and Safari Books Online (see below).

Not a subscriber? Sign up for a free trial.

Safari Books Online has the content you need

Instant Meteor JavaScript Framework Starter takes a behind the scenes look at Meteor, showing you not only the code, but the processes as well. Being completely different from your typical web framework, Meteor requires a dramatically new train of thought when constructing your apps, which we will explore throughout the course of this book.
Twitter Bootstrap Web Development How-To walks you step-by-step from Bootstrap basics to the creation of a fully-responsive, JavaScript-enhanced, multi-page website. After this quick and dirty Bootstrap bootcamp, you’ll not only be building serious sites, but you’ll be equipped to dive into the rest of what Twitter Bootstrap has to offer.
Smashing CoffeeScript is a full-color, practical book that explains the CoffeeScript language, syntax, and processes, and will soon have you producing concise and quality code. Ultimately, you’ll create RIAs and mobile apps faster, with less hassle.

Tags: application, authentication, Meteor, startup.js, twitter,

Comments are closed.