Example #1
0
        public async void OnAuthorization(AuthorizationFilterContext context)
        {
            try
            {
                var userToken = context.HttpContext.Request.Headers["UserToken"];


                var user = await _context.Users.FirstAsync(x => x.Token == userToken);

                var authenticatedUser = await _magento.AuthenticateUser(user);

                if (authenticatedUser == null)
                {
                    context.Result = new UnauthorizedResult();
                }
            }
            catch (InvalidOperationException)
            {
                context.Result = new UnauthorizedResult();
            }
            catch (Exception)
            {
                context.Result = new StatusCodeResult((int)HttpStatusCode.InternalServerError);
            }
        }
Example #2
0
        public async Task <IActionResult> LogUser([FromBody] LoginCredentials credentials)
        {
            EntityEntry <User> loggedUser;

            try
            {
                var authenticatedUser = await _magento.AuthenticateUser(new Models.User {
                    Email = credentials.Email, Password = credentials.Password
                });

                if (authenticatedUser.Token == null)
                {
                    return(NoContent());
                }

                var user = await _dbContext.Users.FirstOrDefaultAsync(x => x.Email == credentials.Email);

                if (user == null)
                {
                    loggedUser = await _dbContext.Users.AddAsync(authenticatedUser);
                }
                else
                {
                    user.Name     = authenticatedUser.Name;
                    user.LastName = authenticatedUser.LastName;
                    user.Token    = authenticatedUser.Token;
                    user.Cpf      = authenticatedUser.Cpf;
                    user.Email    = authenticatedUser.Email;

                    loggedUser = _dbContext.Users.Update(user);
                }

                await _dbContext.SaveChangesAsync();

                return(Ok(loggedUser.Entity));
            }
            catch (Exception)
            {
                return(StatusCode((int)HttpStatusCode.InternalServerError));
            }
        }