public async Task <LoginResponseModel> ProcessRequest(LinkedInWithPhoneRequestModel model) { var profile = await GetProfile(model.Token, model.RedirectUri); var userWithGoogle = _unitOfWork.Repository <ApplicationUser>().Get(x => x.LinkedInId == profile.Id) .Include(x => x.VerificationTokens) .FirstOrDefault(); // If there is such user in DB - just return if (userWithGoogle != null) { var loginResponse = await _jwtService.BuildLoginResponse(userWithGoogle); return(loginResponse); } else if (userWithGoogle == null && model.PhoneNumber != null) { var existingUser = _unitOfWork.Repository <ApplicationUser>().Find(x => x.PhoneNumber == model.PhoneNumber); if (existingUser != null) { existingUser.LinkedInId = profile.Id; _unitOfWork.Repository <ApplicationUser>().Update(existingUser); _unitOfWork.SaveChanges(); var loginResponse = await _jwtService.BuildLoginResponse(existingUser); return(loginResponse); } else { // In other case create VerificationCode with user data and send core to user try { var data = JsonConvert.SerializeObject(new RegisterWithSocialsUsingPhoneInternalModel { PhoneNumber = model.PhoneNumber, SocialId = profile.Id }, new JsonSerializerSettings { Formatting = Formatting.Indented }); await _smsService.SendVerificationCodeAsync(model.PhoneNumber, VerificationCodeType.ConfirmLinkedIn, data); } catch { throw new CustomException(HttpStatusCode.BadRequest, "phoneNumber", "Error while sending message"); } throw new CustomException(HttpStatusCode.NoContent, "phoneNumber", "Verification code sent"); } } else { throw new CustomException(HttpStatusCode.BadRequest, "token", "There is no user with such google id"); } }
public async Task <IActionResult> LinkedIn([FromBody] LinkedInWithPhoneRequestModel model) { var response = await _linkedInService.ProcessRequest(model); return(Json(new JsonResponse <LoginResponseModel>(response))); }