예제 #1
0
        public async Task <IActionResult> Auth([FromBody] TokenRequestModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            try
            {
                var jwtToken = await _authSvc.Auth(model);

                if (jwtToken.ResponseInfo.StatusCode == HttpStatusCode.Unauthorized)
                {
                    _authSvc.DeleteAllCookies(_cookiesToDelete);
                    return(Unauthorized(new { LoginError = jwtToken.ResponseInfo.Message }));
                }
                if (jwtToken.ResponseInfo.StatusCode == HttpStatusCode.InternalServerError)
                {
                    _authSvc.DeleteAllCookies(_cookiesToDelete);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
                if (jwtToken.ResponseInfo.StatusCode == HttpStatusCode.BadRequest)
                {
                    _authSvc.DeleteAllCookies(_cookiesToDelete);
                    return(BadRequest(new { LoginError = jwtToken.ResponseInfo.Message }));
                }

                if (!jwtToken.TwoFactorLoginOn)
                {
                    return(Ok(jwtToken));
                }

                // Update the Response Message
                jwtToken.ResponseInfo.Message = "Auth Code Required";

                var twoFactorCodeModel = await _userSvc.GenerateTwoFactorCodeAsync(true, jwtToken.UserId);

                if (twoFactorCodeModel == null)
                {
                    _authSvc.DeleteAllCookies(_cookiesToDelete);
                    return(BadRequest("Error"));
                }

                if (twoFactorCodeModel.AuthCodeRequired)
                {
                    _authSvc.DeleteAllCookies(_cookiesToDelete);
                    return(Unauthorized(new
                    {
                        LoginError = jwtToken.ResponseInfo.Message,
                        Expiry = twoFactorCodeModel.ExpiryDate,
                        twoFactorToken = twoFactorCodeModel.Token,
                        UserId = twoFactorCodeModel.UserId
                    }));
                }
            }
            catch (Exception ex)
            {
                Log.Error("An error occurred while seeding the database  {Error} {StackTrace} {InnerException} {Source}",
                          ex.Message, ex.StackTrace, ex.InnerException, ex.Source);
            }

            return(Unauthorized());
        }