-
Notifications
You must be signed in to change notification settings - Fork 0
feat(identity): phone number confirmation flow (closes #174) #198
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| namespace SimpleModule.Users.Contracts; | ||
|
|
||
| public interface ISmsSender | ||
| { | ||
| Task SendVerificationCodeAsync( | ||
| ApplicationUser user, | ||
| string phoneNumber, | ||
| string code, | ||
| CancellationToken cancellationToken = default | ||
| ); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| using System.Security.Claims; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Identity; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Routing; | ||
| using SimpleModule.Core; | ||
| using SimpleModule.Core.Inertia; | ||
| using SimpleModule.Users.Contracts; | ||
|
|
||
| namespace SimpleModule.Users.Pages.Account.Manage; | ||
|
|
||
| public class ConfirmPhoneNumberEndpoint : IViewEndpoint | ||
| { | ||
| public const string Route = UsersConstants.Routes.ConfirmPhoneNumber; | ||
|
|
||
| public void Map(IEndpointRouteBuilder app) | ||
| { | ||
| app.MapPost( | ||
| Route, | ||
| async ( | ||
| [FromForm] string? phoneNumber, | ||
| [FromForm] string? code, | ||
| ClaimsPrincipal principal, | ||
| UserManager<ApplicationUser> userManager, | ||
| SignInManager<ApplicationUser> signInManager | ||
| ) => | ||
| { | ||
| var user = await userManager.GetUserAsync(principal); | ||
| if (user is null) | ||
| { | ||
| return TypedResults.Redirect("/Identity/Account/Login"); | ||
| } | ||
|
|
||
| var username = await userManager.GetUserNameAsync(user); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(phoneNumber) || string.IsNullOrWhiteSpace(code)) | ||
| { | ||
| return Inertia.Render( | ||
| "Users/Account/Manage/Index", | ||
| new | ||
| { | ||
| username, | ||
| phoneNumber = await userManager.GetPhoneNumberAsync(user), | ||
| isPhoneNumberConfirmed = await userManager.IsPhoneNumberConfirmedAsync( | ||
| user | ||
| ), | ||
| pendingPhoneNumber = phoneNumber, | ||
| statusMessage = "Error: Phone number and verification code are required.", | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| var result = await userManager.ChangePhoneNumberAsync(user, phoneNumber, code); | ||
| if (!result.Succeeded) | ||
|
Comment on lines
+54
to
+55
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed — 6 digits is too narrow a search space to leave unthrottled. Tracking in #199 alongside the SMS resend cooldown, so we add per-user attempt counters once for both email and phone code submission. Out of scope for this PR (phone parity to the existing untrottled email flow). Generated by Claude Code |
||
| { | ||
| return Inertia.Render( | ||
| "Users/Account/Manage/Index", | ||
| new | ||
| { | ||
| username, | ||
| phoneNumber = await userManager.GetPhoneNumberAsync(user), | ||
| isPhoneNumberConfirmed = await userManager.IsPhoneNumberConfirmedAsync( | ||
| user | ||
| ), | ||
| pendingPhoneNumber = phoneNumber, | ||
| statusMessage = "Error: Invalid or expired verification code.", | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| await signInManager.RefreshSignInAsync(user); | ||
|
|
||
| return Inertia.Render( | ||
| "Users/Account/Manage/Index", | ||
| new | ||
| { | ||
| username, | ||
| phoneNumber = await userManager.GetPhoneNumberAsync(user), | ||
| isPhoneNumberConfirmed = await userManager.IsPhoneNumberConfirmedAsync( | ||
| user | ||
| ), | ||
| statusMessage = "Your phone number has been verified.", | ||
| } | ||
| ); | ||
| } | ||
| ) | ||
| .RequireAuthorization() | ||
| .DisableAntiforgery(); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
ForcePhoneReverify_ValidUser_ClearsPhoneNumberConfirmedAndRedirectstoAdminUsersEndpointTests. It seeds a user with a confirmed phone, posts to/admin/users/{id}/force-phone-reverify, and asserts both the 302 redirect and thatPhoneNumberConfirmedflips tofalsewhile the number itself is preserved.Generated by Claude Code