Skip to content

Commit 6d6977e

Browse files
Handle the click of the link from email
1 parent 4503573 commit 6d6977e

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

Webgentle.BookStore/Webgentle.BookStore/Controllers/AccountController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public async Task<IActionResult> Login(SignInModel signInModel, string returnUrl
7878
{
7979
ModelState.AddModelError("", "Invalid credentials");
8080
}
81-
81+
8282
}
8383

8484
return View(signInModel);
@@ -118,5 +118,23 @@ public async Task<IActionResult> ChangePassword(ChangePasswordModel model)
118118
}
119119
return View(model);
120120
}
121+
122+
[HttpGet("confirm-email")]
123+
public async Task<IActionResult> ConfirmEmail(string uid, string token)
124+
{
125+
126+
if (!string.IsNullOrEmpty(uid) && !string.IsNullOrEmpty(token))
127+
{
128+
token = token.Replace(' ', '+');
129+
var result = await _accountRepository.ConfirmEmailAsync(uid, token);
130+
if (result.Succeeded)
131+
{
132+
ViewBag.IsSuccess = true;
133+
}
134+
}
135+
136+
return View();
137+
138+
}
121139
}
122140
}

Webgentle.BookStore/Webgentle.BookStore/Repository/AccountRepository.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class AccountRepository : IAccountRepository
1717
private readonly IEmailService _emailService;
1818
private readonly IConfiguration _configuration;
1919

20-
public AccountRepository(UserManager<ApplicationUser> userManager,
20+
public AccountRepository(UserManager<ApplicationUser> userManager,
2121
SignInManager<ApplicationUser> signInManager,
2222
IUserService userService,
2323
IEmailService emailService,
@@ -66,7 +66,13 @@ public async Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model)
6666
{
6767
var userId = _userService.GetUserId();
6868
var user = await _userManager.FindByIdAsync(userId);
69-
return await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
69+
return await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
70+
}
71+
72+
73+
public async Task<IdentityResult> ConfirmEmailAsync(string uid, string token)
74+
{
75+
return await _userManager.ConfirmEmailAsync(await _userManager.FindByIdAsync(uid), token);
7076
}
7177

7278
private async Task SendEmailConfirmationEmail(ApplicationUser user, string token)
@@ -80,7 +86,7 @@ private async Task SendEmailConfirmationEmail(ApplicationUser user, string token
8086
PlaceHolders = new List<KeyValuePair<string, string>>()
8187
{
8288
new KeyValuePair<string, string>("{{UserName}}", user.FirstName),
83-
new KeyValuePair<string, string>("{{Link}}",
89+
new KeyValuePair<string, string>("{{Link}}",
8490
string.Format(appDomain + confirmationLink, user.Id, token))
8591
}
8692
};

Webgentle.BookStore/Webgentle.BookStore/Repository/IAccountRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ public interface IAccountRepository
1313
Task SignOutAsync();
1414

1515
Task<IdentityResult> ChangePasswordAsync(ChangePasswordModel model);
16+
17+
Task<IdentityResult> ConfirmEmailAsync(string uid, string token);
1618
}
1719
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
@{
3+
ViewData["Title"] = "Confirm email";
4+
}
5+
<div class="container">
6+
<div class="row text-center">
7+
<div class="col-md-4">
8+
@if (ViewBag.IsSuccess == true)
9+
{
10+
<div class="alert alert-success" role="alert">
11+
<p>Your email has been verified successfully.</p>
12+
</div>
13+
}
14+
else
15+
{
16+
<p>Something went wrong or your token has been expired.</p>
17+
}
18+
</div>
19+
</div>
20+
</div>

0 commit comments

Comments
 (0)