Exemplo n.º 1
0
        public async Task <IActionResult> Login([FromBody] LoginApi loginApi)
        {
            var request = GetRequest("oauth/token");

            request.AddJsonBody(new
            {
                grant_type    = "password",
                username      = loginApi.Email,
                password      = loginApi.Password,
                realm         = _auth0.ConnectionDb,
                client_id     = _auth0.ClientId,
                client_secret = _auth0.ClientSecret
            });
            var result = await GetResponse <TokenResultApi>(request);

            _logger.LogDebug($@"Login with following values against auth0.com. 
                Domain: {_auth0.Domain}, Audience: {_auth0.Audience}, ConnectionDb: '{_auth0.ConnectionDb}', ClientId: '{_auth0.ClientId}'.");

            if (!result.IsSuccessful)
            {
                _logger.LogError($"Login against auth0 failed. Reason: {result.Content}.");
                if (result.ErrorException != null)
                {
                    _logger.LogError(result.ErrorException, $"ErrorMessage: {result.ErrorMessage}.");
                }
                return(Unauthorized());
            }

            return(Ok(result.Data));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register([FromBody] LoginApi loginApi)
        {
            Guid userId = Guid.NewGuid();

            var request = GetRequest("dbconnections/signup");

            request.AddJsonBody(new
            {
                client_id     = _auth0.ClientId,
                email         = loginApi.Email,
                password      = loginApi.Password,
                connection    = _auth0.ConnectionDb,
                user_metadata = new
                {
                    exero_user_id = userId.ToString(),
                    //admin = false
                }
            });
            var result = await GetResponse <SignupResultApi>(request);

            if (!result.IsSuccessful)
            {
                _logger.LogError($"Register against auth0 failed. Reason: {result.Content}.");
                if (result.ErrorException != null)
                {
                    _logger.LogError(result.ErrorException, $"ErrorMessage: {result.ErrorMessage}.");
                }
                return(BadRequest("Failed to register."));
            }

            var user = new User
            {
                Id    = userId,
                Email = loginApi.Email
            };
            await _userRepository.Add(user);

            return(Ok(result.Data));
        }