Example #1
0
        public LoginViewModel(INetworkActivityService networkActivity)
        {
            LoginCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var account = new Account();

                var connection = new Octokit.Connection(new Octokit.ProductHeaderValue("RepoStumble"));
                var client = new Octokit.OauthClient(connection);
                var token = await client.CreateAccessToken(new Octokit.OauthTokenRequest(ClientId, ClientSecret, LoginCode));

                connection.Credentials = new Octokit.Credentials(token.AccessToken);
                var githubClient = new Octokit.GitHubClient(connection);
                var info = await githubClient.User.Current();
                account.AvatarUrl = info.AvatarUrl;
                account.Username = info.Login;
                account.Fullname = info.Name;
                account.OAuth = token.AccessToken;
                account.Save();

                DismissCommand.ExecuteIfCan();
            });

            LoginCommand.IsExecuting.Skip(1).Where(x => x).Subscribe(x => networkActivity.PushNetworkActive());
            LoginCommand.IsExecuting.Skip(1).Where(x => !x).Subscribe(x => networkActivity.PopNetworkActive());
        }
Example #2
0
        public LoginViewModel(INetworkActivityService networkActivity)
        {
            LoginCommand = ReactiveCommand.CreateAsyncTask(async t =>
            {
                var account = new Account();

                var connection = new Octokit.Connection(new Octokit.ProductHeaderValue("RepoStumble"));
                var client     = new Octokit.OauthClient(connection);
                var token      = await client.CreateAccessToken(new Octokit.OauthTokenRequest(ClientId, ClientSecret, LoginCode));

                connection.Credentials = new Octokit.Credentials(token.AccessToken);
                var githubClient       = new Octokit.GitHubClient(connection);
                var info          = await githubClient.User.Current();
                account.AvatarUrl = info.AvatarUrl;
                account.Username  = info.Login;
                account.Fullname  = info.Name;
                account.OAuth     = token.AccessToken;
                account.Save();

                DismissCommand.ExecuteIfCan();
            });

            LoginCommand.IsExecuting.Skip(1).Where(x => x).Subscribe(x => networkActivity.PushNetworkActive());
            LoginCommand.IsExecuting.Skip(1).Where(x => !x).Subscribe(x => networkActivity.PopNetworkActive());
        }
        /// <inheritdoc/>
        public async Task <IConnection> LogInViaOAuth(HostAddress address, CancellationToken cancel)
        {
            var conns = await GetLoadedConnectionsInternal();

            if (conns.Any(x => x.HostAddress == address))
            {
                throw new InvalidOperationException($"A connection to {address} already exists.");
            }

            var client      = CreateClient(address);
            var oauthClient = new OauthClient(client.Connection);
            var user        = await loginManager.LoginViaOAuth(address, client, oauthClient, OpenBrowser, cancel);

            var connection = new Connection(address, user.Login, user, null);

            conns.Add(connection);
            await SaveConnections();

            await usageTracker.IncrementCounter(x => x.NumberOfLogins);

            await usageTracker.IncrementCounter(x => x.NumberOfOAuthLogins);

            return(connection);
        }