예제 #1
0
        /// <summary>
        ///     <inheritdoc />
        /// </summary>
        /// <param name="model"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task <User> FacebookLoginAsync(FacebookLoginViewModel model,
                                                            CancellationToken cancellationToken = default(CancellationToken))
        {
            // Find token information.
            var tokenInfo = await _externalAuthenticationService.GetFacebookTokenInfoAsync(model.AccessToken);

            if (tokenInfo == null || string.IsNullOrWhiteSpace(tokenInfo.AccessToken))
            {
                throw new ApiException(HttpMessages.FacebookCodeIsInvalid, HttpStatusCode.Forbidden);
            }

            // Get the profile information.
            var profile = await _externalAuthenticationService.GetFacebookBasicProfileAsync(tokenInfo.AccessToken);

            if (profile == null)
            {
                throw new ApiException(HttpMessages.GoogleCodeIsInvalid, HttpStatusCode.Forbidden);
            }


            // Find accounts by searching for email address.
            var accounts = _unitOfWork.Users.Search();

            accounts = accounts.Where(x => x.Email.Equals(profile.Email));

            // Get the first matched account.
            var account = await accounts.FirstOrDefaultAsync(cancellationToken);

            // Account is available in the system. Check its status.
            if (account != null)
            {
                // Prevent account from logging into system because it is pending.
                if (account.Status == UserStatus.Pending)
                {
                    throw new ApiException(HttpMessages.AccountIsPending, HttpStatusCode.Forbidden);
                }

                // Prevent account from logging into system because it is deleted.
                if (account.Status == UserStatus.Disabled)
                {
                    throw new ApiException(HttpMessages.AccountIsPending, HttpStatusCode.Forbidden);
                }
            }
            else
            {
                // Initialize account instance.
                account            = new User();
                account.Email      = profile.Email;
                account.Nickname   = profile.FullName;
                account.Role       = UserRole.User;
                account.JoinedTime = _baseTimeService.DateTimeUtcToUnix(DateTime.UtcNow);
                account.Type       = UserKind.Facebook;

                // Add account to database.
                _unitOfWork.Users.Insert(account);
                await _unitOfWork.CommitAsync(cancellationToken);
            }

            return(account);
        }