Last active
December 11, 2016 19:49
-
-
Save chocnut/ae6f415accd95c8ef415 to your computer and use it in GitHub Desktop.
iOS AC + Social(Facebook) Framework & Rails(omniauth-facebook)
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
gem 'devise' | |
gem 'omniauth-facebook' |
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
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 |
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
Rails.application.routes.draw do | |
devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" } | |
end |
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
#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 |
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
#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