Skip to content

Instantly share code, notes, and snippets.

@cjazz
Created October 30, 2016 19:50
Show Gist options
  • Save cjazz/9ed32b49d16e9644d935c6d4ebb630bb to your computer and use it in GitHub Desktop.
Save cjazz/9ed32b49d16e9644d935c6d4ebb630bb to your computer and use it in GitHub Desktop.
Auth0 + Facebook & Twitter
//
// LoginVC.m
//
// Created by Adam Chin on 7/14/16.
// The following is an example of Auth0
// Logging into Email/Social services with a custom UI
// Interface Builder requirements are:
// 2 Social buttons
// Email/Password UITextFields
// button to fire didTapEmailLogin
//
#import "LoginVC.h"
#import "MBProgressHUD.h"
#import "TwitterAPIBundle.h"
#define USERPW_AUTHENTICATION_TYPE @"Username-Password-Authentication"
@interface LoginVC () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *signInButton;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property(weak, nonatomic) IBOutlet UITextField *emailField;
@property(weak, nonatomic) IBOutlet UITextField *passwordField;
@property (nonatomic, weak) IBOutlet UIButton *facebookButton;
@property (nonatomic, weak) IBOutlet UIButton *twitterButton;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *ActivityIndicator;
@property (nonatomic, weak) NSString *connectionType;
@end
@implementation LoginVC
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.ActivityIndicator.hidesWhenStopped = YES;
[self.emailField.layer addSublayer:[GlobalUI bottomBorderFor:self.emailField]];
[self.passwordField.layer addSublayer:[GlobalUI bottomBorderFor:self.passwordField]];
}
- (IBAction)cancel:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.emailField.delegate = self;
self.passwordField.delegate = self;
self.signInButton.layer.cornerRadius = 5;
self.signInButton.clipsToBounds = YES;
self.cancelButton.layer.cornerRadius = 5;
self.cancelButton.clipsToBounds = YES;
self.facebookButton.layer.cornerRadius = 5;
self.facebookButton.clipsToBounds = YES;
self.twitterButton.layer.cornerRadius = 5;
self.twitterButton.clipsToBounds = YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.emailField)
{
[textField resignFirstResponder];
[self.passwordField becomeFirstResponder];
} else if (textField == self.passwordField) {
// here you can define what happens
// when user presses return on the email field
[self didTapEmailLogin:self];
}
return YES;
}
- (IBAction)socialLogin:(id)sender {
[self.ActivityIndicator startAnimating];
if (sender == self.facebookButton)
{
self.connectionType = @"facebook";
A0Lock *lock = [[Auth0Instance sharedInstance] lock];
A0FacebookAuthenticator *facebook = [A0FacebookAuthenticator newAuthenticatorWithDefaultPermissions];
[lock registerAuthenticators:@[facebook]];
A0AuthParameters *params = [A0AuthParameters newDefaultParams];
[[lock identityProviderAuthenticator] authenticateWithConnectionName:self.connectionType
parameters:params
success:^(A0UserProfile * _Nonnull profile, A0Token * _Nonnull token) {
#ifdef DEBUG
NSLog(@"----------------------------------------------->");
NSLog(@" Auth0 Lock: Facebook Login success");
NSLog(@"Profile: %@",profile);
NSLog(@"Token: %@",token);
NSLog(@"----------------------------------------------->");
#endif
[self.ActivityIndicator stopAnimating];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeCustomView;
UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
hud.customView = [[UIImageView alloc] initWithImage:image];
hud.square = YES;
hud.label.text = NSLocalizedString(@"Success!", @"HUD done title");
[hud hideAnimated:YES afterDelay:2.f];
[self performSelector:@selector(dismissView) withObject:self afterDelay:2.0];
} failure:^(NSError * _Nonnull error) {
[self.ActivityIndicator stopAnimating];
NSLog(@"Facebook Login Failure");
}];
}
else if (sender == self.twitterButton)
{
self.connectionType = @"twitter";
NSString *twitterApiKey = [TwitterAPIBundle twitterAPIKey];
NSString *twitterApiSecret = [TwitterAPIBundle twitterSec];
A0TwitterAuthenticator *twitter = [A0TwitterAuthenticator
newAuthenticatorWithKey:twitterApiKey
andSecret:twitterApiSecret];
A0Lock *lock = [[Auth0Instance sharedInstance] lock];
[lock registerAuthenticators:@[twitter]];
A0AuthParameters *params = [A0AuthParameters newDefaultParams];
[[lock identityProviderAuthenticator] authenticateWithConnectionName:self.connectionType
parameters:params
success:^(A0UserProfile * _Nonnull profile, A0Token * _Nonnull token) {
#ifdef DEBUG
NSLog(@"----------------------------------------------->");
NSLog(@" Auth0 Lock: Twitter Login success");
NSLog(@"----------------------------------------------->");
#endif
[self.ActivityIndicator stopAnimating];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeCustomView;
UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
hud.customView = [[UIImageView alloc] initWithImage:image];
hud.square = YES;
hud.label.text = NSLocalizedString(@"Success!", @"HUD done title");
[hud hideAnimated:YES afterDelay:2.f];
[self performSelector:@selector(dismissView) withObject:self afterDelay:2.0];
} failure:^(NSError * _Nonnull error) {
[self.ActivityIndicator stopAnimating];
NSLog(@"Twitter login Failure");
}];
}
}
- (IBAction)didTapEmailLogin:(id)sender {
[self.ActivityIndicator startAnimating];
NSString *email = self.emailField.text;
NSString *password = self.passwordField.text;
A0Lock *lock = [[Auth0Instance sharedInstance] lock];
A0AuthParameters *params = [A0AuthParameters newDefaultParams];
params[A0ParameterConnection] = USERPW_AUTHENTICATION_TYPE;
A0APIClient *client = [lock apiClient];
[client loginWithEmail:email
passcode:password
parameters:params
success:^(A0UserProfile * _Nonnull profile, A0Token * _Nonnull tokenInfo) {
[self.ActivityIndicator stopAnimating];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeCustomView;
UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
hud.customView = [[UIImageView alloc] initWithImage:image];
hud.square = YES;
hud.label.text = NSLocalizedString(@"Success!", @"HUD done title");
[hud hideAnimated:YES afterDelay:2.f];
[self performSelector:@selector(dismissView) withObject:self afterDelay:2.0];
} failure:^(NSError * _Nonnull error) {
NSLog(@"Oops something went wrong: %@", error);
[self.ActivityIndicator stopAnimating];
// [self.spinner stopAnimating];
}];
}
- (IBAction)didRequestPasswordReset:(id)sender {
[self.ActivityIndicator startAnimating];
NSString *email = self.emailField.text;
A0Lock *lock = [[Auth0Instance sharedInstance] lock];
A0APIClient *client = [lock apiClient];
A0AuthParameters *params = [A0AuthParameters newDefaultParams];
params[A0ParameterConnection] = USERPW_AUTHENTICATION_TYPE;
[client requestChangePasswordForUsername:email
parameters:params
success:^{
[self.ActivityIndicator stopAnimating];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.mode = MBProgressHUDModeCustomView;
UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
hud.customView = [[UIImageView alloc] initWithImage:image];
hud.square = YES;
hud.label.text = NSLocalizedString(@"Check your email for instructions...", @"HUD done title");
[hud hideAnimated:YES afterDelay:4.5f];
// [self performSelector:@selector(dismissView) withObject:self afterDelay:2.0];
} failure:^(NSError * _Nonnull error) {
[self.ActivityIndicator stopAnimating];
NSLog(@"Oops something went wrong: %@", error);
}];
}
-(void)dismissView
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment