Created
April 23, 2014 08:58
-
-
Save tudorconstantin/11207833 to your computer and use it in GitHub Desktop.
Perl ~ Facebook authentication with Mojolicious in less than 100 LOC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Mojolicious::Lite; | |
use Net::Facebook::Oauth2; | |
my $config = { | |
facebook => { | |
#get the app id and app secret from the settings page of your facebook application | |
#you create your own fb app from https://developers.facebook.com/ (accessing Apps -> Create a new app) | |
app_id => '', | |
secret => '', | |
redirect_url => 'http://mojo.dev:3000/redirect_from_fb', | |
}, | |
}; | |
#this plugin provides the $self->get_token('facebook') helper method | |
plugin 'o_auth2', { | |
facebook => { | |
key => $config->{facebook}->{app_id}, | |
secret => $config->{facebook}->{secret}, | |
}}; | |
get '/' => sub { | |
my $self = shift; | |
return $self->render('index_unauthenticated') unless $self->session->{user}->{fb_username}; | |
return $self->render('index_authenticated'); | |
} => 'index'; | |
get authenticate => sub { | |
my $self = shift; | |
my $url = $self->get_authorize_url( | |
'facebook', | |
#these are the things the app will be able to do with user's facebook account | |
#find out more about them on https://developers.facebook.com/docs/facebook-login/permissions/ | |
scope => 'offline_access manage_pages publish_stream', | |
redirect_uri => $config->{facebook}->{redirect_url}, | |
); | |
$self->redirect_to( $url->to_abs->to_string ); | |
} => 'authenticate'; | |
get redirect_from_fb => sub { | |
my $self = shift; | |
#we'll use this token on every call to the fb api | |
#note that this is user specific - you can save it in the DB for later use, although you'll get it with every login | |
my $token = $self->get_token('facebook'); | |
my $fb = Net::Facebook::Oauth2->new( | |
application_id => $config->{facebook}->{app}->{id}, | |
application_secret => $config->{facebook}->{app}->{secret}, | |
access_token => $token | |
); | |
my $response = $fb->get( 'https://graph.facebook.com/me/', | |
{ access_token => $token } ); | |
$self->session->{user} = { | |
fb_username => $response->as_hash->{username}, | |
}; | |
$self->redirect_to('index'); | |
} => 'redirect_from_fb'; | |
app->start; | |
__DATA__ | |
@@ index_unauthenticated.html.ep | |
% layout 'default'; | |
<%= link_to 'Click here to authenticate with FB' => 'authenticate'; %> | |
@@ index_authenticated.html.ep | |
% layout 'default'; | |
Hello <%= session->{user}->{fb_username} %> | |
<%= link_to 'Click here to authenticate with FB' => 'authenticate'; %> | |
@@ layouts/default.html.ep | |
<!DOCTYPE html> | |
<html> | |
<body><%= content %></body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
just tested your script and its not working out of the box. Had to modify the code a little bit -> https://gist.github.com/bert2002/239ce332a6b7973bb324