Example #1
0
        public HttpResponseMessage Login(StandartAuthRequest request)
        {
            try
            {
                // Check request and request props is not null
                CheckVal.IsNull(request, nameof(request));
                CheckVal.IsNull(request.Password, nameof(request.Password));
                // Find User
                var user = CheckData.UserExist(_context, false, email: request.Email, name: request.Name);
                if (user == null)
                {
                    throw new ApiException(ApiResult.Validation, ErrorType.UserWithEmailorNameNotFound,
                                           request.Email ?? request.Name);
                }
                // Check if User is Blocked
                CheckData.IsUserBlocked(_context, null, user);
                // Check if User Account Exist
                var account = CheckData.AccountExist(_context, Provider.Standart, user.Id);
                // Check password
                BaseAuth.CheckPassword(user, request.Password);


                var token = BaseAuth.CreateToken(user.Id);

                var userDto = _mapper.Map <User, UserDTO>(user);

                // Generate AuthenticationToken
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Ok, new LoginResult(token, userDto))));
            }
            catch (ApiException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ex.ApiResult, null, new ErrorResponse(ex.ErrorType, ex.Message, ex))));
            }
            catch (EntitySqlException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Sql, null, new ErrorResponse(ErrorType.None, ex.Message, ex))));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <LoginResult>(ApiResult.Unknown, null,
                                                                         new ErrorResponse(ErrorType.Internal, ex.Message, ex))));
            }
        }
Example #2
0
        public HttpResponseMessage Registration(StandartAuthRequest request)
        {
            try
            {
                // Check request and request props is not null
                CheckVal.IsNull(request, "request");
                CheckVal.IsNull(request.Email, nameof(request.Email));
                CheckVal.IsNull(request.Name, nameof(request.Name));
                CheckVal.IsNull(request.Password, nameof(request.Password));
                // We use lowercased User Names
                request.Email = request.Email.ToLower();
                request.Name  = request.Name.ToLower();
                // Validate request props
                CheckVal.EmailCheck(request.Email);

                CheckVal.NameCheck(request.Name);
                CheckVal.PasswordCheck(request.Password);
                // Check if User Already Exist
                CheckData.UserExist(_context, true, email: request.Email, name: request.Name);
                var newUser = BaseAuth.CreateUser(_context, Provider.Standart, request);
                return(Request.CreateResponse(HttpStatusCode.Created,
                                              new Response <string>(ApiResult.Created, newUser.Id)));
            }
            catch (ApiException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <string>(ex.ApiResult, null, new ErrorResponse(ex.ErrorType, ex.Message, ex))));
            }
            catch (EntitySqlException ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <string>(ApiResult.Sql, null, new ErrorResponse(ErrorType.None, ex.Message, ex))));
            }
            catch (Exception ex)
            {
                return(Request.CreateResponse(HttpStatusCode.OK,
                                              new Response <string>(ApiResult.Unknown, null, new ErrorResponse(ErrorType.Internal, ex.Message, ex))));
            }
        }