This repository has been archived by the owner on Jul 12, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b44f2ce
commit 2e788a3
Showing
6 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
49 changes: 49 additions & 0 deletions
49
src/RookieShop.IdentityService/Pages/Account/Register/Index.cshtml
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
@page | ||
@model RookieShop.IdentityService.Pages.Account.Register.Index | ||
|
||
<div class="register-page"> | ||
<div class="lead"> | ||
<h1>Create a new account</h1> | ||
</div> | ||
|
||
<partial name="_ValidationSummary"/> | ||
|
||
<form asp-page="/Account/Register/Index"> | ||
<input type="hidden" asp-for="Input.ReturnUrl"/> | ||
<div class="form-group"> | ||
<label asp-for="Input.Username" class="col-md-2 control-label">Username</label> | ||
<div class="col-md-10"> | ||
<input asp-for="Input.Username" placeholder="example@gmail.com" class="form-control" autofocus/> | ||
<span asp-validation-for="Input.Username" class="text-danger"></span> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Input.PhoneNumber" class="col-md-2 control-label">Confirm Password</label> | ||
<div class="col-md-10"> | ||
<input asp-for="Input.PhoneNumber" placeholder="0123456789" class="form-control"/> | ||
<span asp-validation-for="Input.PhoneNumber" class="text-danger"></span> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Input.Password" class="col-md-2 control-label">Password</label> | ||
<div class="col-md-10"> | ||
<input asp-for="Input.Password" class="form-control"/> | ||
<span asp-validation-for="Input.Password" class="text-danger"></span> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<label asp-for="Input.ConfirmPassword" class="col-md-2 control-label">Confirm Password</label> | ||
<div class="col-md-10"> | ||
<input asp-for="Input.ConfirmPassword" class="form-control"/> | ||
<span asp-validation-for="Input.ConfirmPassword" class="text-danger"></span> | ||
</div> | ||
</div> | ||
<div class="form-group"> | ||
<div class="col-md-offset-2 col-md-10"> | ||
<button class="btn btn-primary" name="Input.Button" value="register">Register</button> | ||
<button class="btn btn-secondary" name="Input.Button" value="cancel">Cancel</button> | ||
</div> | ||
</div> | ||
</form> | ||
|
||
</div> |
66 changes: 66 additions & 0 deletions
66
src/RookieShop.IdentityService/Pages/Account/Register/Index.cshtml.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.RazorPages; | ||
using RookieShop.IdentityService.Models; | ||
|
||
namespace RookieShop.IdentityService.Pages.Account.Register; | ||
|
||
[SecurityHeaders] | ||
[AllowAnonymous] | ||
public sealed class Index(UserManager<ApplicationUser> userManager) : PageModel | ||
{ | ||
[BindProperty] public InputModel Input { get; set; } = default!; | ||
|
||
public async Task<IActionResult> OnGet(string? returnUrl) | ||
{ | ||
Input = new() | ||
{ | ||
ReturnUrl = returnUrl | ||
}; | ||
|
||
return Page(); | ||
} | ||
|
||
public async Task<IActionResult> OnPost() | ||
{ | ||
if (Input.Button != "register") | ||
return RedirectToPage("/Account/Login/Index", new { returnUrl = Input.ReturnUrl }); | ||
|
||
if (!ModelState.IsValid) | ||
return Page(); | ||
|
||
if (Input.Username is null || Input.Password is null || Input.PhoneNumber is null) | ||
return Page(); | ||
|
||
var user = await userManager.FindByNameAsync(Input.Username); | ||
|
||
if (user is not null) | ||
{ | ||
ModelState.AddModelError("Username", "Username is already taken"); | ||
return Page(); | ||
} | ||
|
||
user = new() | ||
{ | ||
UserName = Input.Username, | ||
PhoneNumber = Input.PhoneNumber | ||
}; | ||
|
||
var result = await userManager.CreateAsync(user, Input.Password); | ||
|
||
if (!result.Succeeded) | ||
{ | ||
foreach (var error in result.Errors) | ||
{ | ||
ModelState.AddModelError(string.Empty, error.Description); | ||
} | ||
|
||
return Page(); | ||
} | ||
|
||
await userManager.AddToRoleAsync(user, "user"); | ||
|
||
return RedirectToPage("/Account/Login/Index", new { returnUrl = Input.ReturnUrl }); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/RookieShop.IdentityService/Pages/Account/Register/InputModel.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace RookieShop.IdentityService.Pages.Account.Register; | ||
|
||
public sealed class InputModel | ||
{ | ||
[Required] | ||
[EmailAddress] | ||
[MaxLength(50)] | ||
public string? Username { get; set; } | ||
|
||
[Required] | ||
[RegularExpression(@"^\d{8,16}$", ErrorMessage = "Phone number must be 8-16 digits.")] | ||
public string? PhoneNumber { get; set; } | ||
|
||
[Required] | ||
[RegularExpression(@"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,15}$", | ||
ErrorMessage = | ||
"Password must be 8-15 characters and contain at least one uppercase letter, one lowercase letter, and one digit.")] | ||
public string? Password { get; set; } | ||
|
||
[Required] | ||
[Compare(nameof(Password))] | ||
public string? ConfirmPassword { get; set; } | ||
|
||
public string? ReturnUrl { get; set; } | ||
|
||
public string? Button { get; set; } | ||
} |