GoodMoodProvider/APIGoodMoodProvider/APIGoodMoodProvider/Controllers/UserController.cs
/// <summary>
/// Register user and authenticate him and put token and refresh token in database
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
Register user and authenticate him and put token and refresh token in database
Register user and authenticate him and put token and refresh token in database
[AllowAnonymous]
        [HttpPost]
        [Route("Register")]
        public async Task<IActionResult> Register(RegistrationRequest request)
        {
            try
            {
                var users = await _unitOfWork.UserRepository.GetAllAsync();
                if (users.Any(u => u.Email == request.Email || u.Login == request.Login))
                    return BadRequest("Login or email is occupied");
                User newUser = new User()
                {
                    ID = new Guid(),
                    Login = request.Login,
                    Email = request.Email,
                    Password = _encrypter.EncryptString(request.Password)
                };
                await _unitOfWork.UserRepository.AddAsync(newUser);
                var defaultRole = await _context.Role.FirstOrDefaultAsync(r => r.Name == "User");
                await _unitOfWork.UserRoleRepository.AddAsync(new UserRole() { 
                    ID = new Guid(),
                    UserID = newUser.ID,
                    RoleID = defaultRole.ID
                });
                var response = _userHandler.Authenticate(newUser.ID);
                newUser.Token = response.Token;
                newUser.RefreshToken = response.RefreshToken;
                var query = new GetUserById(newUser.ID);
                User userWithRoles = await _mediator.Send(query);
                foreach (UserRole ur in newUser.UserRoles)
                {
                    var role = await _unitOfWork.RoleRepository.GetByIdAsync(ur.RoleID);
                    response.Roles.Add(role.Name);
                }
                await _unitOfWork.UserRepository.PutAsync(newUser);
                await _unitOfWork.SaveDBAsync();
                return Ok(response);
            }
            catch(Exception ex)
            {
                Log.Error($"{DateTime.Now}|Error|{ex}");
                return BadRequest(ex);
            }
        }
[ AllowAnonymous ] [ HttpPost ] [ Route ( " " ) ] public async Task < IActionResult > Register ( RegistrationRequest request ) { try { var users = await _unitOfWork . UserRepository . GetAllAsync ( ) ; if ( users . Any ( u => u . Email == request . Email || u . Login == request . Login ) ) return BadRequest ( " " ) ; User newUser = new User ( ) { ID = new Guid ( ) , Login = request . Login , Email = request . Email , Password = _encrypter . EncryptString ( request . Password ) } ; await _unitOfWork . UserRepository . AddAsync ( newUser ) ; var defaultRole = await _context . Role . FirstOrDefaultAsync ( r => r . Name == " " ) ; await _unitOfWork . UserRoleRepository . AddAsync ( new UserRole ( ) { ID = new Guid ( ) , UserID = newUser . ID , RoleID = defaultRole . ID } ) ; var response = _userHandler . Authenticate ( newUser . ID ) ; newUser . Token = response . Token ; newUser . RefreshToken = response . RefreshToken ; var query = new GetUserById ( newUser . ID ) ; User userWithRoles = await _mediator . Send ( query ) ; foreach ( UserRole ur in newUser . UserRoles ) { var role = await _unitOfWork . RoleRepository . GetByIdAsync ( ur . RoleID ) ; response . Roles . Add ( role . Name ) ; } await _unitOfWork . UserRepository . PutAsync ( newUser ) ; await _unitOfWork . SaveDBAsync ( ) ; return Ok ( response ) ; } catch ( Exception ex ) { Log . Error ( $" { DateTime . Now } |Error| { ex } " ) ; return BadRequest ( ex ) ; } }
Register user and authenticate him and put token and refresh token in database
Register user and authenticate him and put token and refresh token in database
None
request
RegistrationRequest
None