Skip to content

Commit 8ef0f14

Browse files
authored
Adds Email link sign-in (firebase#882)
1 parent 0b8f216 commit 8ef0f14

22 files changed

+1397
-18
lines changed

Example/Auth/Sample/MainViewController.m

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@
9191
*/
9292
static NSString *const kSignInGoogleButtonText = @"Sign in with Google";
9393

94+
/** @var kSignInWithEmailLink
95+
@brief The text of the "Sign in with Email Link" button.
96+
*/
97+
static NSString *const kSignInWithEmailLink = @"Sign in with Email Link";
98+
99+
/** @var kSendEmailSignInLink
100+
@brief The text of the "Send Email SignIn link" button
101+
*/
102+
static NSString *const kSendEmailSignInLink = @"Send Email Sign in Link";
103+
94104
/** @var kSignInAndRetrieveGoogleButtonText
95105
@brief The text of the "Sign in with Google and retrieve data" button.
96106
*/
@@ -279,6 +289,11 @@
279289
*/
280290
static NSString *const kGetProvidersForEmail = @"Get Provider IDs for Email";
281291

292+
/** @var kGetAllSignInMethodsForEmail
293+
@brief The text of the "Get sign-in methods for Email" button.
294+
*/
295+
static NSString *const kGetAllSignInMethodsForEmail = @"Get Sign-in methods for Email";
296+
282297
/** @var kActionCodeTypeDescription
283298
@brief The description of the "Action Type" entry.
284299
*/
@@ -722,6 +737,10 @@ - (void)updateTable {
722737
action:^{ [weakSelf createUserAuthDataResult]; }],
723738
[StaticContentTableViewCell cellWithTitle:kSignInGoogleButtonText
724739
action:^{ [weakSelf signInGoogle]; }],
740+
[StaticContentTableViewCell cellWithTitle:kSignInWithEmailLink
741+
action:^{ [weakSelf signInWithEmailLink]; }],
742+
[StaticContentTableViewCell cellWithTitle:kSendEmailSignInLink
743+
action:^{ [weakSelf sendEmailSignInLink]; }],
725744
[StaticContentTableViewCell cellWithTitle:kSignInGoogleAndRetrieveDataButtonText
726745
action:^{ [weakSelf signInGoogleAndRetrieveData]; }],
727746
[StaticContentTableViewCell cellWithTitle:kSignInFacebookButtonText
@@ -754,6 +773,8 @@ - (void)updateTable {
754773
action:^{ [weakSelf reloadUser]; }],
755774
[StaticContentTableViewCell cellWithTitle:kGetProvidersForEmail
756775
action:^{ [weakSelf getProvidersForEmail]; }],
776+
[StaticContentTableViewCell cellWithTitle:kGetAllSignInMethodsForEmail
777+
action:^{ [weakSelf getAllSignInMethodsForEmail]; }],
757778
[StaticContentTableViewCell cellWithTitle:kUpdateEmailText
758779
action:^{ [weakSelf updateEmail]; }],
759780
[StaticContentTableViewCell cellWithTitle:kUpdatePasswordText
@@ -1657,7 +1678,7 @@ - (void)signInFacebookAndRetrieveData {
16571678
}
16581679

16591680
/** @fn signInEmailPassword
1660-
@brief Invoked when "sign in with Email/Password" row is pressed.
1681+
@brief Invoked when "Sign in with Email/Password" row is pressed.
16611682
*/
16621683
- (void)signInEmailPassword {
16631684
[self showTextInputPromptWithMessage:@"Email Address:"
@@ -1724,6 +1745,75 @@ - (void)signInEmailPasswordAuthDataResult {
17241745
}];
17251746
}
17261747

1748+
/** @fn signInWithEmailLink
1749+
@brief Invoked when "Sign in with email link" row is pressed.
1750+
*/
1751+
- (void)signInWithEmailLink {
1752+
[self showTextInputPromptWithMessage:@"Email Address:"
1753+
keyboardType:UIKeyboardTypeEmailAddress
1754+
completionBlock:^(BOOL userPressedOK, NSString *_Nullable email) {
1755+
if (!userPressedOK || !email.length) {
1756+
return;
1757+
}
1758+
[self showTextInputPromptWithMessage:@"Email Sign-In Link:"
1759+
completionBlock:^(BOOL userPressedOK, NSString *_Nullable link) {
1760+
if (!userPressedOK) {
1761+
return;
1762+
}
1763+
if ([[FIRAuth auth] isSignInWithEmailLink:link]) {
1764+
[self showSpinner:^{
1765+
[[AppManager auth] signInWithEmail:email
1766+
link:link
1767+
completion:^(FIRAuthDataResult *_Nullable authResult,
1768+
NSError *_Nullable error) {
1769+
[self hideSpinner:^{
1770+
if (error) {
1771+
[self logFailure:@"sign-in with Email/Sign-In failed" error:error];
1772+
} else {
1773+
[self logSuccess:@"sign-in with Email/Sign-In link succeeded."];
1774+
[self log:[NSString stringWithFormat:@"UID: %@",authResult.user.uid]];
1775+
}
1776+
[self showTypicalUIForUserUpdateResultsWithTitle:@"Sign-In Error" error:error];
1777+
}];
1778+
}];
1779+
}];
1780+
} else {
1781+
[self log:@"The sign-in link is invalid"];
1782+
}
1783+
}];
1784+
}];
1785+
}
1786+
1787+
/** @fn sendEmailSignInLink
1788+
@brief Invoked when "Send email sign-in link" row is pressed.
1789+
*/
1790+
- (void)sendEmailSignInLink {
1791+
[self showTextInputPromptWithMessage:@"Email:"
1792+
completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
1793+
if (!userPressedOK) {
1794+
return;
1795+
}
1796+
[self showSpinner:^{
1797+
void (^requestEmailSignInLink)(void (^)(NSError *)) = ^(void (^completion)(NSError *)) {
1798+
[[AppManager auth] sendSignInLinkToEmail:userInput
1799+
actionCodeSettings:[self actionCodeSettings]
1800+
completion:completion];
1801+
};
1802+
requestEmailSignInLink(^(NSError *_Nullable error) {
1803+
[self hideSpinner:^{
1804+
if (error) {
1805+
[self logFailure:@"Email Link request failed" error:error];
1806+
[self showMessagePrompt:error.localizedDescription];
1807+
return;
1808+
}
1809+
[self logSuccess:@"Email Link request succeeded."];
1810+
[self showMessagePrompt:@"Sent"];
1811+
}];
1812+
});
1813+
}];
1814+
}];
1815+
}
1816+
17271817
/** @fn signUpNewEmail
17281818
@brief Invoked if sign-in is attempted with new email/password.
17291819
@remarks Should only be called if @c FIRAuthErrorCodeInvalidEmail is encountered on attepmt to
@@ -2245,6 +2335,39 @@ - (void)getProvidersForEmail {
22452335
}];
22462336
}
22472337

2338+
/** @fn getAllSignInMethodsForEmail
2339+
@brief Prompts user for an email address, calls @c FIRAuth.getAllSignInMethodsForEmail:callback:
2340+
and displays the result.
2341+
*/
2342+
- (void)getAllSignInMethodsForEmail {
2343+
[self showTextInputPromptWithMessage:@"Email:"
2344+
completionBlock:^(BOOL userPressedOK, NSString *_Nullable userInput) {
2345+
if (!userPressedOK || !userInput.length) {
2346+
return;
2347+
}
2348+
2349+
[self showSpinner:^{
2350+
[[AppManager auth] fetchSignInMethodsForEmail:userInput
2351+
completion:^(NSArray<NSString *> *_Nullable signInMethods,
2352+
NSError *_Nullable error) {
2353+
if (error) {
2354+
[self logFailure:@"get sign-in methods for email failed" error:error];
2355+
} else {
2356+
[self logSuccess:@"get sign-in methods for email succeeded."];
2357+
}
2358+
[self hideSpinner:^{
2359+
if (error) {
2360+
[self showMessagePrompt:error.localizedDescription];
2361+
return;
2362+
}
2363+
[self showMessagePrompt:[signInMethods componentsJoinedByString:@", "]];
2364+
}];
2365+
}];
2366+
}];
2367+
}];
2368+
}
2369+
2370+
22482371
/** @fn actionCodeRequestTypeString
22492372
@brief Returns a string description for the type of the next action code request.
22502373
*/
@@ -2486,6 +2609,8 @@ - (NSString *)nameForActionCodeOperation:(FIRActionCodeOperation)operation {
24862609
return @"Recover Email";
24872610
case FIRActionCodeOperationPasswordReset:
24882611
return @"Password Reset";
2612+
case FIRActionCodeOperationEmailLink:
2613+
return @"Email Sign-In Link";
24892614
case FIRActionCodeOperationUnknown:
24902615
return @"Unknown action";
24912616
}

0 commit comments

Comments
 (0)