public async Task <LoginResponse> LoginAsync([FromBody] LoginRequest loginRequest)
        {
            var user = await _userCollection.GetUserByUsernameAsync(loginRequest.Username);

            string apiToken = user?.VerifyPassword(loginRequest.Password);

            if (apiToken == null)
            {
                throw new HttpException(HttpStatusCode.Unauthorized);
            }

            return(new LoginResponse
            {
                ApiToken = apiToken
            });
        }
Example #2
0
        public async Task <SignUpResponse> Create([FromBody] SignUpRequest request)
        {
            if (await _userCollection.GetUserByUsernameAsync(request.Username) != null)
            {
                throw new HttpException(HttpStatusCode.BadRequest, "A user already exists with this username");
            }

            var user = await _userCollection.CreateUserAsync(request.Username, request.Password);

            if (user == null)
            {
                throw new HttpException(HttpStatusCode.InternalServerError);
            }

            return(new SignUpResponse
            {
                ApiToken = user.VerifyPassword(request.Password)
            });
        }