public async Task <IActionResult> Register(UserDTO userDTO)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string UserName = userDTO.UserName.ToLower();

            if (await _accountService.UserExists(UserName))
            {
                return(BadRequest("User Name is already exists"));
            }
            var user = _mapper.Map <User>(userDTO);

            await _accountService.Register(user, userDTO.Password);

            var token  = _jWTService.GenerateJWTToken(user);
            var userdt = _mapper.Map <UserDetailsDTO>(user);

            return(Ok(new
            {
                token = token,
                user = userdt
            }));
        }
Exemple #2
0
        public async Task <Either <Exception, LoggedUser> > SingInUser(LoginModel model)
        {
            var user = _authdbContext.Users.FirstOrDefault(x => x.Email == model.Email);

            if (user is null)
            {
                return(new Exception("Nie ma konta z takim adresem email"));
            }

            var signInResult = await _signInManager.CheckPasswordSignInAsync(user, model.Password, false);

            if (signInResult.Succeeded)
            {
                try
                {
                    var userClaims = _authdbContext.UserClaims.Find(x => x.UserId == user.Id).Select(x => x.ToClaim()).FirstOrDefault();
                    var userModel  = new LoggedUser()
                    {
                        FirstName = user.FirstName,
                        LastName  = user.LastName,
                        Id        = user.Id.ToString(),
                        Username  = user.Email,
                        Email     = user.Email,
                        Token     = await _jwtService.GenerateJWTToken(user),
                        Claim     = new Dictionary <string, string>()
                        {
                            { userClaims.Type, userClaims.Value }, { ClaimTypes.Sid, user.Id.ToString() }
                        }
                    };

                    return(userModel);
                }
                catch (Exception e)
                {
                    return(new Exception("To hasło nie pasuje do tego adresu email"));
                }
            }
            else
            {
                return(new Exception("To hasło nie pasuje do tego adresu email"));
            }
        }