Skip to content

Instantly share code, notes, and snippets.

@esteedqueen
Forked from chocnut/Gemfile
Created May 23, 2016 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esteedqueen/0016b1eece72670f4238c405611f8b66 to your computer and use it in GitHub Desktop.
Save esteedqueen/0016b1eece72670f4238c405611f8b66 to your computer and use it in GitHub Desktop.
iOS AC + Social(Facebook) Framework & Rails(omniauth-facebook)
gem 'devise'
gem 'omniauth-facebook'
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
skip_before_filter :verify_authenticity_token
def facebook
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
Rails.application.routes.draw do
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
end
#import <UIKit/UIKit.h>
#import <Accounts/Accounts.h>
#import <Social/Social.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) ACAccountStore *accountStore;
@property (nonatomic, strong) ACAccountCredential *accountCredential;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)signInFacebook:(id)sender {
self.accountStore = [[ACAccountStore alloc] init];
ACAccountType *fbAccountType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
id options = @{
ACFacebookAppIdKey: @"myAPikeyhere",
ACFacebookPermissionsKey: @[@"email", @"read_friendlists"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
};
[self.accountStore requestAccessToAccountsWithType:fbAccountType
options:options
completion:^(BOOL granted, NSError *error) {
if (granted) {
ACAccount *fbAccount = [[self.accountStore accountsWithAccountType:fbAccountType] lastObject];
ACAccountCredential *facebookCredential = [fbAccount credential];
NSString *token = [facebookCredential oauthToken];
[self authenticate:token];
} else{
NSLog(@"Not granted: %@", error);
}
}];
}
- (void)authenticate:(NSString *)token {
NSURL *url = [NSURL URLWithString:@"http://localhost:3000/users/auth/facebook/callback"];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
NSString *params = [NSString stringWithFormat:@"access_token=%@", token];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURLResponse *res;
NSError *err;
[NSURLConnection sendSynchronousRequest:req returningResponse:&res error:&err];
if (!err) {
NSLog(@"The user is logged in on the server side too");
} else {
NSLog(@"Error occurred. %@", err);
}
});
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment