Exemple #1
0
        public async Task <IActionResult> Login([FromBody] LoginViewModel credentials)
        {
            if (!ModelState.IsValid)
            {
                return(new BadResponseResult(ModelState));
            }

            if ((credentials.GrantType == GrantType.Phone || credentials.GrantType == GrantType.Email) &&
                credentials.Password.IsNullOrEmpty())
            {
                ModelState.AddModelError("Password", "Password is null or empty, but grant type is not guest.");
                return(new BadResponseResult(ModelState));
            }
            var user = new UserDto {
                UserId = 0, RoleType = RoleType.Guest
            };

            switch (credentials.GrantType)
            {
            case GrantType.Guest: break;

            case GrantType.Phone:
                user = await _internalAuthService.GetUserByPhone(credentials.Phone, credentials.Password);

                if (user == null)
                {
                    return(new ResponseResult((int)HttpStatusCode.Forbidden, "Phone and(or) password is incorrect", new { Token = new Token(), User = new UserInfoViewModel() }));
                }
                break;

            case GrantType.Email:
                user = await _internalAuthService.GetUserByEmail(credentials.Email, credentials.Password);

                if (user == null)
                {
                    return(new ResponseResult((int)HttpStatusCode.Forbidden, "Email and(or) password is incorrect", new { Token = new Token(), User = new UserInfoViewModel() }));
                }
                break;

            default:
                ModelState.AddModelError("GrantType", "Sorry, we can not find such grant type.");
                return(new BadResponseResult(ModelState));
            }

            var result = await _commonAuthService.Login(user);

            return(new OkResponseResult(result));
        }