public dynamic post_signup_github(GithubSignUpInputModel model)
        {
            var accessToken = GetGithubAccessToken(model.Code);

            //get user details so that we can auto suggest repos etc
            var userDetailsRequest = new RestRequest("/user", Method.GET) { RequestFormat = DataFormat.Json };

            userDetailsRequest.AddParameter("access_token", accessToken);

            var client = new RestClient("https://api.github.com");

            var response = client.Execute<GithubUserResponse>(userDetailsRequest);

            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(response.StatusDescription);

            var githubUserId = response.Data.id;

            var account = Session.Query<UserAccount>().SingleOrDefault(u => u.Email == response.Data.email);

            //see if we alrady has this user (by email)
            if (account != null && account.GithubUserId != githubUserId)
            {
                if (account.GithubUserId != null)
                    throw new InvalidOperationException("Account with the same email already exists and is associated with another Github account");

                account.GithubUserId = response.Data.id;
                account.Status = UserAccountStatus.Verified;

                return account;
            }

            //see if we already has this user (by github id)
            account = Session.Query<UserAccount>().SingleOrDefault(u => u.GithubUserId == githubUserId);

            if (account != null)
                return account;

            var userId = response.Data.email.ToGuid();

            account = new UserAccount
            {
                Id = userId,
                UserName = response.Data.email,
                Email = response.Data.email,
                SignedUpAt = DateTime.UtcNow,
                Status = UserAccountStatus.Verified,
                GithubUserId = githubUserId,
                GravatarId = response.Data.gravatar_id
            };

            Session.Store(account);

            return account;
        }
Exemple #2
0
        public dynamic post_signup_github(GithubSignUpInputModel model)
        {
            var accessToken = GetGithubAccessToken(model.Code);

            //get user details so that we can auto suggest repos etc
            var userDetailsRequest = new RestRequest("/user", Method.GET)
            {
                RequestFormat = DataFormat.Json
            };

            userDetailsRequest.AddParameter("access_token", accessToken);

            var client = new RestClient("https://api.github.com");

            var response = client.Execute <GithubUserResponse>(userDetailsRequest);

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception(response.StatusDescription);
            }

            var githubUserId = response.Data.id;

            var account = Session.Query <UserAccount>().SingleOrDefault(u => u.Email == response.Data.email);


            //see if we alrady has this user (by email)
            if (account != null && account.GithubUserId != githubUserId)
            {
                if (account.GithubUserId != null)
                {
                    throw new InvalidOperationException("Account with the same email already exists and is associated with another Github account");
                }

                account.GithubUserId = response.Data.id;
                account.Status       = UserAccountStatus.Verified;

                return(account);
            }

            //see if we already has this user (by github id)
            account = Session.Query <UserAccount>().SingleOrDefault(u => u.GithubUserId == githubUserId);

            if (account != null)
            {
                return(account);
            }

            var userId = response.Data.email.ToGuid();

            account = new UserAccount
            {
                Id           = userId,
                UserName     = response.Data.email,
                Email        = response.Data.email,
                SignedUpAt   = DateTime.UtcNow,
                Status       = UserAccountStatus.Verified,
                GithubUserId = githubUserId,
                GravatarId   = response.Data.gravatar_id
            };

            Session.Store(account);



            return(account);
        }