//Function - Takes a user model, and provider and logs the user in public bool LogIn(SocialProviderUser model, Provider type) { //Gets the provider Id from the table string tableProviderId = GetProviderId(model.Id, type); //If the provider Id does not match the one in the table return an error if (tableProviderId != null && tableProviderId != model.ProviderId) { log.Error("The provided Id: " + model.ProviderId + " does not match the one in the table: " + tableProviderId); return(false); } //If the provider Id was not in the table - add it to the table if (tableProviderId == null) { SocialProviderAddRequest spModel = new SocialProviderAddRequest(); spModel.Id = model.Id; spModel.ProviderId = model.ProviderId; //Adds the provider Id into the db with the account Id and the provider Id InsertProviderAccount(spModel, type); } //Calls the login function with the passed in model which creates the cookie for logging in _userService.LogInSocial(model); return(true); }
public async Task <HttpResponseMessage> RegisterThirdParty(string provider, ThirdPartyCompleteModel model) { string errorMsg = "provider"; try { Provider type; //Checks the provider passed in through the api call and assigns the type and error message if linkedin, google, or facebook switch (provider) { case "linkedin": type = Provider.LinkedIn; errorMsg = "LinkedIn"; break; case "google": type = Provider.Google; errorMsg = "Google"; break; case "facebook": type = Provider.Facebook; errorMsg = "Facebook"; break; default: return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No provider exists with this name")); } //If Facebook login call the service to get information from facebook if (type == Provider.Facebook) { FacebookUserAuth userInfo = await _service.GetFBUserInfo(model.AccessToken); model.Email = userInfo.Email; model.ProviderId = userInfo.Id; } //Checks if the email exists in the database int accountId = _service.CheckEmail(model.Email); //If the email doesn't exist we need to register a new account if (accountId == 0) { //Pass the information and role into the register function, and get back the new account id accountId = Register(model, model.Role); //If something went wrong return an error if (accountId == -1) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Login Error")); } } SocialProviderUser spu = new SocialProviderUser(); spu.Email = model.Email; spu.Id = accountId; spu.ProviderId = model.ProviderId; //Send the email, account id, and provider id to the login function if (_service.LogIn(spu, type)) { //If the login was successful, get the user information and send it back ItemResponse <PersonViewModel> resp = new ItemResponse <PersonViewModel>(); resp.Item = _service.SelectPerson(spu.Id); return(Request.CreateResponse(HttpStatusCode.OK, resp)); } else { //If the login was unsuccessful, return an error return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong logging in with " + errorMsg)); } } catch (Exception ex) { //If an exception occurs log it and return an error log.Error("Error registering " + errorMsg + " account", ex); return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Something went wrong")); } }