Example #1
0
        public async Task <IActionResult> RegisterAsync([FromBody] RegisterRequest request)
        {
            var registerResult = await _mediator.Send(request);

            var response = new AbstractAnswer
            {
                Success = registerResult.Success,
                Errors  = registerResult.Errors,
            };

            if (!registerResult.Success)
            {
                return(Ok(response));
            }

            await _mediator.Send(new AddUserCommand
            {
                UserToAdd = registerResult.Data,
            });

            var tokenAnswer = await GenerateToken(registerResult.Data.Id);

            var principal = GeneratePrincipal(registerResult.Data.Id, tokenAnswer);

            await HttpContext.SignInAsync(principal);

            _logger.LogInformation(
                $"user {request.Login} registered and entered with refresh id {tokenAnswer}");

            return(Ok(response));
        }
Example #2
0
        public async Task <IActionResult> LoginAsync([FromBody] LoginRequest request)
        {
            var loginResult = await _mediator.Send(request);

            var response = new AbstractAnswer
            {
                Success = loginResult.Success,
                Errors  = loginResult.Errors,
            };

            if (!loginResult.Success)
            {
                return(Ok(response));
            }

            var generateTokenResult = await GenerateToken(loginResult.Data);

            var principal = GeneratePrincipal(loginResult.Data, generateTokenResult);

            _logger.LogInformation(
                $"user {request.Login} login and entered with refresh id {generateTokenResult}");

            await HttpContext.SignInAsync(principal);

            return(Ok(response));
        }