<commit_before>package facebook

import (
	"errors"
	"fmt"
	"log"
	"net/http"

	"github.com/RichardKnop/recall/accounts"
	"github.com/RichardKnop/recall/oauth"
	"github.com/RichardKnop/recall/response"
)

var (
	// ErrAccountMismatch ...
	ErrAccountMismatch = errors.New("Account mismatch")
)

// Handles requests to login with Facebook access token (POST /v1/facebook/login)
func (s *Service) loginHandler(w http.ResponseWriter, r *http.Request) {
	// Get the authenticated account from the request context
	authenticatedAccount, err := accounts.GetAuthenticatedAccount(r)
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Parse the form so r.Form becomes available
	if err := r.ParseForm(); err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Get the scope string
	scope, err := s.GetAccountsService().GetOauthService().GetScope(r.Form.Get("scope"))
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Fetch the user data from facebook
	resp, err := s.adapter.GetMe(r.Form.Get("access_token"))
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Initialise variables with from facebook
	var (
		facebookID = fmt.Sprintf("%s", resp["id"])
		email      = fmt.Sprintf("%s", resp["email"])
		firstName  = fmt.Sprintf("%s", resp["first_name"])
		lastName   = fmt.Sprintf("%s", resp["last_name"])
		user       *accounts.User
	)

	logger.Info("Fetched Facebook user's data")
	logger.Infof("%v", resp)

	// There is an edge case where Facebook does not return a valid email
	// User could have registered with a phone number or have an unconfirmed
	// email address. In such rare case, default to <EMAIL>
	if resp["email"] == nil || email == "%!s(<nil>)" {
		email = fmt.Sprintf("<EMAIL>", facebookID)
	}

	// Get or create a new user based on facebook ID and other details
	user, err = s.GetAccountsService().GetOrCreateFacebookUser(
		authenticatedAccount,
		facebookID,
		&accounts.UserRequest{
			Email:     email,
			FirstName: firstName,
			LastName:  lastName,
		},
	)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
<commit_msg>Fixed logging bug.<commit_after>package facebook

import (
	"errors"
	"fmt"
	"log"
	"net/http"

	"github.com/RichardKnop/recall/accounts"
	"github.com/RichardKnop/recall/oauth"
	"github.com/RichardKnop/recall/response"
)

var (
	// ErrAccountMismatch ...
	ErrAccountMismatch = errors.New("Account mismatch")
)

// Handles requests to login with Facebook access token (POST /v1/facebook/login)
func (s *Service) loginHandler(w http.ResponseWriter, r *http.Request) {
	// Get the authenticated account from the request context
	authenticatedAccount, err := accounts.GetAuthenticatedAccount(r)
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Parse the form so r.Form becomes available
	if err := r.ParseForm(); err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// Get the scope string
	scope, err := s.GetAccountsService().GetOauthService().GetScope(r.Form.Get("scope"))
	if err != nil {
		response.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	// Fetch the user data from facebook
	resp, err := s.adapter.GetMe(r.Form.Get("access_token"))
	if err != nil {
		response.UnauthorizedError(w, err.Error())
		return
	}

	// Initialise variables with from facebook
	var (
		facebookID = fmt.Sprintf("%s", resp["id"])
		email      = fmt.Sprintf("%s", resp["email"])
		firstName  = fmt.Sprintf("%s", resp["first_name"])
		lastName   = fmt.Sprintf("%s", resp["last_name"])
		user       *accounts.User
	)

	log.Print("Fetched Facebook user's data")
	log.Printf("%v", resp)

	// There is an edge case where Facebook does not return a valid email
	// User could have registered with a phone number or have an unconfirmed
	// email address. In such rare case, default to {<EMAIL>
	if resp["email"] == nil || email == "%!s(<nil>)" {
		email = fmt.Sprintf("<EMAIL>", facebookID)
	}

	// Get or create a new user based on facebook ID and other details
	user, err = s.GetAccountsService().GetOrCreateFacebookUser(
		authenticatedAccount,
		facebookID,
		&accounts.UserRequest{
			Email:     email,
			FirstName: firstName,
			LastName:  lastName,
		},
	)
	if err != nil {
		response.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

{'fraction_non_alphanumeric': 0.13775971093044265, 'fraction_numerical': 0.00045167118337850043, 'mean_word_length': 3.9541387024608503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4287048', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1439, 'n_words': 390}