Exemple #1
0
        public async Task <IActionResult> FacebookLogin([FromBody] FacebookToken facebookToken)
        {
            var httpClient = httpClientFactory.CreateClient();

            httpClient.BaseAddress = new Uri("https://graph.facebook.com/v4.0/");
            var response = await httpClient.GetAsync($"me?access_token={facebookToken.Token}&fields=id,email,first_name,last_name");

            if (!response.IsSuccessStatusCode)
            {
                logger.Error("Error. Response was not success!");
                return(Conflict());
            }
            ;

            var result = await response.Content.ReadAsStringAsync();

            var facebookAccount = JsonConvert.DeserializeObject <FacebookAccount>(result);

            var user = authenticateService.FacebookLogin(facebookAccount);

            if (user == null)
            {
                var model        = mapper.Map <RegisterModel>(facebookAccount);
                var userRegister = mapper.Map <User>(model);
                authenticateService.RegisterFacebook(userRegister);
                user = authenticateService.FacebookLogin(facebookAccount);
            }

            return(UserResult(user));
        }