Esempio n. 1
0
        public override async Task <object> MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context)
        {
            var username = inputs.Get <string>("username");
            var email    = inputs.Get <string>("email");
            var password = inputs.Get <string>("password");
            var user     = new ApplicationUser
            {
                UserName = username,
                Email    = email
            };

            var result = await _userManager.CreateAsync(user, password);

            if (result.Succeeded)
            {
                var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                var emailConfirmationToken = System.Web.HttpUtility.UrlEncode(code);

                _logger.LogInformation(3, "User created a new account with password.");

                return(new
                {
                    user,
                    emailConfirmationToken
                });
            }

            foreach (var identityError in result.Errors)
            {
                _validationProvider.AddError("_error", identityError.Description);
            }

            return(null);
        }
Esempio n. 2
0
        public override async Task <object> MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context)
        {
            var username   = inputs.Get <string>("username");
            var rememberMe = inputs.Get <bool>("rememberMe");
            var password   = inputs.Get <string>("password");

            var result = await _signInManager.PasswordSignInAsync(username, password, rememberMe, false);

            if (result.Succeeded)
            {
                _logger.LogInformation(1, "User logged in.");

                var user = new ApplicationUser
                {
                    UserName = username,
                };

                return(new
                {
                    user
                });
            }

            _validationProvider.AddError("_error", "Invalid login attempt.");

            return(null);
        }
        public override async Task <object> MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context)
        {
            var userName = inputs.Get <string>("userName");

            // Get the information about the user from the external login provider
            var info = await _signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                _validationProvider.AddError("_error", "Unsuccessful login with service.");

                return(null);
            }
            var email = info.Principal.FindFirstValue(ClaimTypes.Email);

            var user = new ApplicationUser
            {
                UserName = userName,
                Email    = email
            };

            var result = await _userManager.CreateAsync(user);

            if (result.Succeeded)
            {
                result = await _userManager.AddLoginAsync(user, info);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, true);

                    _logger.LogInformation(6, $"User created an account using {info.LoginProvider} provider.");
                }
            }

            foreach (var identityError in result.Errors)
            {
                _validationProvider.AddError("_error", identityError.Description);
            }

            return(null);
        }
Esempio n. 4
0
        public override async Task <object> MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context)
        {
            var userId   = inputs.Get <string>("userId");
            var token    = inputs.Get <string>("token");
            var password = inputs.Get <string>("password");
            var user     = await _userManager.FindByIdAsync(userId);

            var result = await _userManager.ResetPasswordAsync(user, token, password);

            foreach (var identityError in result.Errors)
            {
                _validationProvider.AddError("_error", identityError.Description);
            }

            return(null);
        }
Esempio n. 5
0
        public override async Task <object> MutateAndGetPayload(MutationInputs inputs, ResolveFieldContext <object> context)
        {
            var email = inputs.Get <string>("email");

            var user = await _userManager.FindByEmailAsync(email);

            if (user == null)
            {
                _validationProvider.AddError("_error", "We couldn't find that email address");

                return(null);
            }

            var code = await _userManager.GeneratePasswordResetTokenAsync(user);

            var passwordResetToken = System.Web.HttpUtility.UrlEncode(code);

            return(new
            {
                user,
                passwordResetToken
            });
        }
Esempio n. 6
0
        public AppQuery(ILiveStreamService liveStreamService, IValidationProvider validationProvider,
                        IGenreService genreService, ILoggerFactory loggerFactory, IQuoteService quoteService,
                        SignInManager <ApplicationUser> signInManager, UserManager <ApplicationUser> userManager)
        {
            var logger = loggerFactory.CreateLogger <AppQuery>();

            Field <QuotePayload>()
            .Name("quote")
            .Resolve(c => quoteService.GetRandomQuote());

            Connection <LiveStreamPayload>()
            .Name("liveStreams")
            .Argument <StringGraphType>("genre", "The genre that the live stream belongs to")
            .Argument <StringGraphType>("searchQuery", "The search query to filter the liveStreams against")
            .Argument <FilterInput>("filter", "The filters to apply to the live streams")
            .Resolve(c =>
            {
                var genre       = c.GetArgument <string>("genre");
                var searchQuery = c.GetArgument <string>("searchQuery");
                var filter      = c.GetArgument <Filter.Filter>("filter");
                var offset      = ConnectionUtils.OffsetOrDefault(c.After, 0);
                var page        = ((offset + 1) / c.First.Value) + 2;

                if (filter.Newest)
                {
                    var liveStreams = liveStreamService.GetLiveStreams(c.First.Value * page, genre, searchQuery);

                    return(ConnectionUtils.ToConnection(liveStreams, c));
                }

                return(liveStreamService.GetPopularLiveStreams(page, genre, searchQuery)
                       .ContinueWith(x => ConnectionUtils.ToConnection(x.Result, c)));
            });

            Field <ListGraphType <GenrePayload> >("genres",
                                                  resolve: c => genreService.GetGenres());

            Field <AccountPayload>()
            .Name("user")
            .Resolve(c => c.UserContext.As <Context>().CurrentUser);

            Field <ExternalLoginCallbackPayload>()
            .Name("externalLoginCallback")
            .ResolveAsync(async c =>
            {
                var info = await signInManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    validationProvider.AddError("_error", "Could not get external login information");

                    return(null);
                }

                // Sign in the user with this external login provider if the user already has a login.
                var result           = await signInManager.ExternalLoginSignInAsync(info.LoginProvider, info.ProviderKey, true);
                ApplicationUser user = null;

                if (result.Succeeded)
                {
                    user = await signInManager.UserManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey);

                    logger.LogInformation(5, $"User logged in with {info.LoginProvider} provider.");
                }
                var userName = info.Principal.FindFirstValue(ClaimTypes.Name);

                // If the user does not have an account, then ask the user to create an account.
                return(new
                {
                    user,
                    loginProvider = info.LoginProvider,
                    userName
                });
            });


            Field <BooleanGraphType>()
            .Name("confirmEmail")
            .Argument <NonNullGraphType <StringGraphType> >("userId", "The id of the user")
            .Argument <NonNullGraphType <StringGraphType> >("token", "The unique code to verify the email")
            .ResolveAsync(async c =>
            {
                var userId = c.GetArgument <string>("userId");
                var token  = c.GetArgument <string>("token");

                var user = await userManager.FindByIdAsync(userId);

                if (user == null)
                {
                    validationProvider.AddError("_error", "No user could be found");

                    return(null);
                }
                var result = await userManager.ConfirmEmailAsync(user, token);

                if (!result.Succeeded)
                {
                    validationProvider.AddError("_error", "Could not confirm your email. Please try again.");
                }

                return(null);
            });

            Field <ListGraphType <LoginProvidersPayload> >()
            .Name("loginProviders")
            .ResolveAsync(async c =>
            {
                var providers = await signInManager.GetExternalAuthenticationSchemesAsync();

                return(providers);
            });
        }