BinaryStudio.ClientManager.WebUi.Controllers.AuthController.GoogleAuth C# (CSharp) Method

GoogleAuth() public method

Renders information received from authentication service.
public GoogleAuth ( string code, string error ) : System.Web.Mvc.ActionResult
code string
error string
return System.Web.Mvc.ActionResult
        public ActionResult GoogleAuth(string code, string error)
        {
            UserInfo userInfo;
            try
            {
                userInfo = googleClient.GetUserInfo(googleClient.GetAccessToken(code, error));
            }
            catch
            {
                return RedirectToAction("LogOn");
            }

            var user = repository.Query<User>(x => x.RelatedPerson, x => x.Teams)
                .SingleOrDefault(x => x.GoogleId == userInfo.Id);

            if (null == user)
            {
                user = new User
                            {
                                GoogleId = userInfo.Id,
                                RelatedPerson = repository.Query<Person>().SingleOrDefault(x => x.Email == userInfo.Email)
                            };
                if (null == user.RelatedPerson)
                {
                    var person = new Person
                                     {
                                         Email = userInfo.Email,
                                         FirstName = userInfo.FirstName,
                                         LastName = userInfo.LastName,
                                         PhotoUri = userInfo.PhotoUri,
                                         Role = PersonRole.Employee,
                                         CreationDate = DateTime.Now
                                     };
                    user.RelatedPerson = person;
                    repository.Save(person);
                }
                repository.Save(user);
            }

            appContext.User = user;

            return RedirectToRoute("Default");
        }