feat(backend): add add email address endpoint
This commit is contained in:
		
							parent
							
								
									7f971e8549
								
							
						
					
					
						commit
						5b17c716cb
					
				
					 6 changed files with 114 additions and 3 deletions
				
			
		|  | @ -183,10 +183,40 @@ public class EmailAuthController( | |||
| 
 | ||||
|     [HttpPost("add")] | ||||
|     [Authorize("*")] | ||||
|     public async Task<IActionResult> AddEmailAddressAsync() | ||||
|     public async Task<IActionResult> AddEmailAddressAsync([FromBody] AddEmailAddressRequest req) | ||||
|     { | ||||
|         _logger.Information("beep"); | ||||
|         var emails = await db | ||||
|             .AuthMethods.Where(m => m.UserId == CurrentUser!.Id && m.AuthType == AuthType.Email) | ||||
|             .ToListAsync(); | ||||
|         if (emails.Count > AuthUtils.MaxAuthMethodsPerType) | ||||
|         { | ||||
|             throw new ApiError.BadRequest( | ||||
|                 "Too many email addresses, maximum of 3 per account.", | ||||
|                 "email", | ||||
|                 null | ||||
|             ); | ||||
|         } | ||||
| 
 | ||||
|         var validPassword = await authService.ValidatePasswordAsync(CurrentUser!, req.Password); | ||||
|         if (!validPassword) | ||||
|         { | ||||
|             throw new ApiError.Forbidden("Invalid password"); | ||||
|         } | ||||
| 
 | ||||
|         var state = await keyCacheService.GenerateRegisterEmailStateAsync( | ||||
|             req.Email, | ||||
|             userId: CurrentUser!.Id | ||||
|         ); | ||||
| 
 | ||||
|         var emailExists = await db | ||||
|             .AuthMethods.Where(m => m.AuthType == AuthType.Email && m.RemoteId == req.Email) | ||||
|             .AnyAsync(); | ||||
|         if (emailExists) | ||||
|         { | ||||
|             return NoContent(); | ||||
|         } | ||||
| 
 | ||||
|         mailService.QueueAddEmailAddressEmail(req.Email, state, CurrentUser.Username); | ||||
|         return NoContent(); | ||||
|     } | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue