Created
May 15, 2019 20:03
-
-
Save gzamudio/913250d0e0466bab1001bb13eb3c006f to your computer and use it in GitHub Desktop.
Email confirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace NetCoreBootstrap.Api.V1.Controllers | |
{ | |
[Route("/api/v1/[controller]/[action]")] | |
public class AccountApiController : Controller | |
{ | |
... | |
[HttpPost("SignIn")] | |
public async Task<object> SignIn([FromBody] UserVO userValueObject) | |
{ | |
object response; | |
try | |
{ | |
if (!string.IsNullOrEmpty(userValueObject.Email)) userValueObject.Email = userValueObject.Email.ToLower(); | |
var result = await SignInManager.PasswordSignInAsync(userValueObject.Email, userValueObject.Password, userValueObject.RememberMe, false); | |
if (result.Succeeded) | |
{ | |
var appUser = UserManager.Users.SingleOrDefault(r => r.Email == userValueObject.Email); | |
Response.StatusCode = StatusCodes.Status200OK; | |
var configVariables = new Dictionary<string, string> | |
{ | |
{ "key", Configuration["Jwt:Key"] }, | |
{ "expire", Configuration["Jwt:ExpireDays"] }, | |
{ "issuer", Configuration["Jwt:Issuer"] }, | |
}; | |
response = new UserVO | |
{ | |
Token = $"Bearer {AccountHelper.GenerateJwtToken(userValueObject.Email, appUser, configVariables)}", | |
Email = appUser.Email, | |
}; | |
} | |
else if (result.IsNotAllowed) | |
{ | |
Response.StatusCode = StatusCodes.Status401Unauthorized; | |
response = new { Message = Localizer["account_login_confirm_email"].Value }; | |
} | |
else | |
{ | |
Response.StatusCode = StatusCodes.Status400BadRequest; | |
if (string.IsNullOrEmpty(userValueObject.Email) || string.IsNullOrEmpty(userValueObject.Password)) | |
response = new { Message = Localizer["account_login_failed_empty_fields"].Value }; | |
else | |
response = new { Message = Localizer["account_login_failed"].Value }; | |
} | |
return Json(response); | |
} | |
catch (Exception e) | |
{ | |
Response.StatusCode = StatusCodes.Status400BadRequest; | |
return Json(new { Message = e.Message }); | |
} | |
} | |
[HttpGet("ConfirmEmail/{userId}/{token}")] | |
public async Task<IActionResult> ConfirmEmail(string userId, string token) | |
{ | |
var user = UserManager.FindByIdAsync(userId).Result; | |
var result = await UserManager.ConfirmEmailAsync(user, HttpUtility.UrlDecode(token)); | |
object response; | |
if (result.Succeeded) | |
{ | |
Response.StatusCode = StatusCodes.Status200OK; | |
response = new { Message = Localizer["account_email_confirmed"].Value }; | |
} | |
else | |
{ | |
Response.StatusCode = StatusCodes.Status400BadRequest; | |
response = new { Message = Localizer["account_email_not_confirmed"].Value, Errors = result.Errors }; | |
} | |
return Json(response); | |
} | |
private async Task SendConfirmationEmailAsync(User user) | |
{ | |
var token = await UserManager.GenerateEmailConfirmationTokenAsync(user); | |
var tokenHtml = HttpUtility.UrlEncode(token); | |
var callbackUrl = Configuration["AppUrl"] + this.Url.Action("ConfirmEmail", "AccountApi", new { userId = user.Id, token = tokenHtml }); | |
var subject = Localizer["account_email_subject"].Value; | |
var body = Localizer["account_email_body"].Value + $" <a href='http://{callbackUrl}'>here.</a>"; | |
Mailer.SendMail(user.Email, subject, body); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment