The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Dancer::Plugin::Auth::Twitter - Authenticate with Twitter

SYNOPSIS

    package SomeDancerApp;
    use Dancer ':syntax';
    use Dancer::Plugin::Auth::Twitter;

    auth_twitter_init();

    before sub {
        if (not session('twitter_user')) {
            redirect auth_twitter_authenticate_url;
        }
    };

    get '/' => sub {
        "welcome, ".session('twitter_user')->{'screen_name'};
    };

    get '/fail' => sub { "FAIL" };

    ...

CONCEPT

This plugin provides a simple way to authenticate your users through Twitter's OAuth API. It provides you with a helper to build easily a redirect to the authentication URI, defines automatically a callback route handler and saves the authenticated user to your session when done.

PREREQUESITES

In order for this plugin to work, you need the following:

  • Twitter application

    Anyone can register a Twitter application at http://dev.twitter.com/. When done, make sure to configure the application as a Web application.

  • Configuration

    You need to configure the plugin first: copy your consumer_key and consumer_secret (provided by Twitter) to your Dancer's configuration under plugins/Auth::Twitter:

        # config.yml
        ...
        plugins:
          "Auth::Twitter":
            consumer_key:     "1234"
            consumer_secret:  "abcd"
            callback_url:     "http://localhost:3000/auth/twitter/callback"
            callback_success: "/"
            callback_fail:    "/fail"

    callback_success and callback_fail are optional and default to '/' and '/fail', respectively.

    Note that you also need to provide your callback url, whose route handler is automatically created by the plugin.

  • Session backend

    For the authentication process to work, you need a session backend, in order for the plugin to store the authenticated user's information.

    Use the session backend of your choice, it doesn't make a difference, see Dancer::Session for details about supported session engines, or http://search.cpan.org/search?query=Dancer-Session.

EXPORT

The plugin exports the following symbols to your application's namespace:

twitter

The plugin uses a Net::Twitter object to do its job. You can access this object with the twitter symbol, exported by the plugin.

auth_twitter_init

This function should be called before your route handlers, in order to initialize the underlying Net::Twitter object. It will read your configuration and create a new Net::Twitter instance.

auth_twitter_authenticate_url

This function returns an authenticate URI for redirecting unauthenticated users. You should use this in a before filter like the following:

    before sub {
        # we don't want to bounce for ever!
        return if request->path =~ m{/auth/twitter/callback};
    
        if (not session('twitter_user')) {
            redirect auth_twitter_authenticate_url();
        }
    };

When the user authenticate with Twitter's OAuth interface, she's going to be bounced back to /auth/twitter/callback.

ROUTE HANDLERS

The plugin defines the following route handler automatically

/auth/twitter/callback

This route handler is responsible for catching back a user that has just authenticated herself with Twitter's OAuth. The route handler saves tokens and user information in the session and then redirects the user to the URI specified by callback_success.

If the validation of the token returned by Twitter, for some reason, failed, the user will be redirect to the URI specified by callback_fail.

AUTHOR

Alexis Sukrieh, <sukria at sukria.net>

ACKNOWLEDGEMENTS

This plugin has been written as a port of Catalyst::Authentication::Credential::Twitter written by Jesse Stay.

This plugin was part of the Perl Dancer Advent Calendar 2010.

LICENSE AND COPYRIGHT

Copyright 2010 Alexis Sukrieh.

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.

1 POD Error

The following errors were encountered while parsing the POD:

Around line 193:

alternative text 'http://search.cpan.org/search?query=Dancer-Session' contains non-escaped | or /