Skip to content

Adds Email link sign-in #882

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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