Exemplo n.º 1
0
        public async Task <ActionResult <UserJwtResponse> > CompleteFacebookAuthenticationAsync(
            [FromBody] CompleteFacebookAuthenticationRequest request
            )
        {
            var loginToken = HttpContext.Session.GetString(FacebookLoginTokenKey);

            if (loginToken != request.LoginToken)
            {
                return(BadRequest());
            }

            var accessToken = await _facebookClient.GetAccessTokenAsync(request.RedirectUri, request.Code);

            var profile = await _facebookClient.GetUserProfileAsync(accessToken);

            var currentUserId = HttpContext.Session.GetCurrentUserId();

            if (currentUserId.HasValue)
            {
                await _socialMediaUserLinkService.AssociateUserToFacebookIdAsync(currentUserId.Value, profile.Id);
            }

            var user = await _socialMediaUserLinkService.GetOrCreateUserFromFacebookAsync(profile.Name, profile.Id);

            HttpContext.Session.SetCurrentUserId(user.Id);
            var token = _jwtService.GenerateJwtToken(user.Id);

            return(new UserJwtResponse
            {
                Token = token,
                UserInfo = _mapper.Map <UserInfoResponse>(user)
            });
        }