Skip to content

Commit

Permalink
Adds Email link sign-in (firebase#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulb777 committed Mar 7, 2018
1 parent 0b8f216 commit 8ef0f14
Show file tree
Hide file tree
Showing 22 changed files with 1,397 additions and 18 deletions.
127 changes: 126 additions & 1 deletion Example/Auth/Sample/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
*/
static NSString *const kSignInGoogleButtonText = @"Sign in with Google";

/** @var kSignInWithEmailLink
@brief The text of the "Sign in with Email Link" button.
*/
static NSString *const kSignInWithEmailLink = @"Sign in with Email Link";

/** @var kSendEmailSignInLink
@brief The text of the "Send Email SignIn link" button
*/
static NSString *const kSendEmailSignInLink = @"Send Email Sign in Link";

/** @var kSignInAndRetrieveGoogleButtonText
@brief The text of the "Sign in with Google and retrieve data" button.
*/
Expand Down Expand Up @@ -279,6 +289,11 @@
*/
static NSString *const kGetProvidersForEmail = @"Get Provider IDs for Email";

/** @var kGetAllSignInMethodsForEmail
@brief The text of the "Get sign-in methods for Email" button.
*/
static NSString *const kGetAllSignInMethodsForEmail = @"Get Sign-in methods for Email";

/** @var kActionCodeTypeDescription
@brief The description of the "Action Type" entry.
*/
Expand Down Expand Up @@ -722,6 +737,10 @@ - (void)updateTable {
action:^{ [weakSelf createUserAuthDataResult]; }],
[StaticContentTableViewCell cellWithTitle:kSignInGoogleButtonText
action:^{ [weakSelf signInGoogle]; }],
[StaticContentTableViewCell cellWithTitle:kSignInWithEmailLink
action:^{ [weakSelf signInWithEmailLink]; }],
[StaticContentTableViewCell cellWithTitle:kSendEmailSignInLink
action:^{ [weakSelf sendEmailSignInLink]; }],
[StaticContentTableViewCell cellWithTitle:kSignInGoogleAndRetrieveDataButtonText
action:^{ [weakSelf signInGoogleAndRetrieveData]; }],
[StaticContentTableViewCell cellWithTitle:kSignInFacebookButtonText
Expand Down Expand Up @@ -754,6 +773,8 @@ - (void)updateTable {
action:^{ [weakSelf reloadUser]; }],
[StaticContentTableViewCell cellWithTitle:kGetProvidersForEmail
action:^{ [weakSelf getProvidersForEmail]; }],
[StaticContentTableViewCell cellWithTitle:kGetAllSignInMethodsForEmail
action:^{ [weakSelf getAllSignInMethodsForEmail]; }],
[StaticContentTableViewCell cellWithTitle:kUpdateEmailText
action:^{ [weakSelf updateEmail]; }],
[StaticContentTableViewCell cellWithTitle:kUpdatePasswordText
Expand Down Expand Up @@ -1657,7 +1678,7 @@ - (void)signInFacebookAndRetrieveData {
}

/** @fn signInEmailPassword
@brief Invoked when "sign in with Email/Password" row is pressed.
@brief Invoked when "Sign in with Email/Password" row is pressed.
*/
- (void)signInEmailPassword {
[self showTextInputPromptWithMessage:@"Email Address:"
Expand Down Expand Up @@ -1724,6 +1745,75 @@ - (void)signInEmailPasswordAuthDataResult {
}];
}

/** @fn signInWithEmailLink
@brief Invoked when "Sign in with email link" row is pressed.
*/
- (void)signInWithEmailLink {
[self showTextInputPromptWithMessage:@"Email Address:"
keyboardType:UIKeyboardTypeEmailAddress
completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
if (!userPressedOK || !email.length) {
return;
}
[self showTextInputPromptWithMessage:@"Email Sign-In Link:"
completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
if (!userPressedOK) {
return;
}
if ([[FIRAuth auth] isSignInWithEmailLink:link]) {
[self showSpinner:^{
[[AppManager auth] signInWithEmail:email
link:link
completion:^(FIRAuthDataResult *_Nullable authResult,
NSError *_Nullable error) {
[self hideSpinner:^{
if (error) {
[self logFailure:@"sign-in with Email/Sign-In failed" error:error];
} else {
[self logSuccess:@"sign-in with Email/Sign-In link succeeded."];
[self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
}
[self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
}];
}];
}];
} else {
[self log:@"The sign-in link is invalid"];
}
}];
}];
}

/** @fn sendEmailSignInLink
@brief Invoked when "Send email sign-in link" row is pressed.
*/
- (void)sendEmailSignInLink {
[self showTextInputPromptWithMessage:@"Email:"
completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
if (!userPressedOK) {
return;
}
[self showSpinner:^{
void (^requestEmailSignInLink)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
[[AppManager auth] sendSignInLinkToEmail:userInput
actionCodeSettings:[self actionCodeSettings]
completion:completion];
};
requestEmailSignInLink(^(NSError *_Nullable error) {
[self hideSpinner:^{
if (error) {
[self logFailure:@"Email Link request failed" error:error];
[self showMessagePrompt:error.localizedDescription];
return;
}
[self logSuccess:@"Email Link request succeeded."];
[self showMessagePrompt:@"Sent"];
}];
});
}];
}];
}

/** @fn signUpNewEmail
@brief Invoked if sign-in is attempted with new email/password.
@remarks Should only be called if @c FIRAuthErrorCodeInvalidEmail is encountered on attepmt to
Expand Down Expand Up @@ -2245,6 +2335,39 @@ - (void)getProvidersForEmail {
}];
}

/** @fn getAllSignInMethodsForEmail
@brief Prompts user for an email address, calls @c FIRAuth.getAllSignInMethodsForEmail:callback:
and displays the result.
*/
- (void)getAllSignInMethodsForEmail {
[self showTextInputPromptWithMessage:@"Email:"
completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
if (!userPressedOK || !userInput.length) {
return;
}

[self showSpinner:^{
[[AppManager auth] fetchSignInMethodsForEmail:userInput
completion:^(NSArray<NSString *> *_Nullable signInMethods,
NSError *_Nullable error) {
if (error) {
[self logFailure:@"get sign-in methods for email failed" error:error];
} else {
[self logSuccess:@"get sign-in methods for email succeeded."];
}
[self hideSpinner:^{
if (error) {
[self showMessagePrompt:error.localizedDescription];
return;
}
[self showMessagePrompt:[signInMethods componentsJoinedByString:@", "]];
}];
}];
}];
}];
}


/** @fn actionCodeRequestTypeString
@brief Returns a string description for the type of the next action code request.
*/
Expand Down Expand Up @@ -2486,6 +2609,8 @@ - (NSString *)nameForActionCodeOperation:(FIRActionCodeOperation)operation {
return @"Recover Email";
case FIRActionCodeOperationPasswordReset:
return @"Password Reset";
case FIRActionCodeOperationEmailLink:
return @"Email Sign-In Link";
case FIRActionCodeOperationUnknown:
return @"Unknown action";
}
Expand Down
Loading

0 comments on commit 8ef0f14

Please sign in to comment.