/// <summary> /// Log in or sign up an user by google or facebook /// </summary> /// <param name="socialUser">The info to log a user</param> /// See <see cref="Areas.Identity.Models.UserMediaLog"/> to know the param structure /// <returns>The IActionResult of the socialLog action</returns> /// See <see cref="Areas.Identity.Models.UserSession"/> to know the return structure public async Task <IActionResult> socialLog([FromBody] UserMediaLog socialUser) { if (socialUser.socialProvider == "FACEBOOK") { return(await doSocialLog(socialUser, false)); } else if (socialUser.socialProvider == "GOOGLE") { return(await doSocialLog(socialUser, true)); } return(BadRequest(new { error = "InvalidSocialToken" })); }
/// <summary> /// Add a user to the database /// </summary> /// <param name="socialUser">The info of the user to add</param> /// See <see cref="Areas.Identity.Models.UserMediaLog"/> to know the param structure /// <returns>The user who has been added</returns> private User addSocialUser(UserMediaLog socialUser) { User newUser = new User { email = socialUser.email, nickname = socialUser.firstName, password = PasswordHasher.hashPassword(socialUser.password), tokenValidation = null, role = RoleManager.getNormalUser(_context), profileImg = getImage(socialUser.urlImage) }; _context.User.Add(newUser); _context.SaveChanges(); return(newUser); }
// // ──────────────────────────────────────────────────────────────────────────────────── // :::::: P R I V A T E F U N C T I O N S : : : : : : : : // ──────────────────────────────────────────────────────────────────────────────────── // /// <summary> /// Do the social log on google and facebook /// </summary> /// <param name="socialUser">The info to log/sign the user</param> /// See <see cref="Areas.Identity.Models.UserMediaLog"/> to know param structure /// <param name="isGoogleType">True if the log/sign is to Google, false if is a Facebook log/sign</param> /// <returns>The IActionResult of the social log</returns> /// See <see cref="Areas.Identity.Models.UserSession"/> to know the return structure private async Task <IActionResult> doSocialLog(UserMediaLog socialUser, Boolean isGoogleType) { try { if (isGoogleType && !await verifyGoogleToken(socialUser.authToken, socialUser.id)) { return(BadRequest(new { error = "InvalidSocialToken" })); } if (!isGoogleType && !await verifyFacebookToken(socialUser.authToken, socialUser.id)) { return(BadRequest(new { error = "InvalidSocialToken" })); } User user = new User(); if (!existsUser(socialUser.email, ref user)) //The new user doesn't exists { //The new user doesn't exist but his password isn't correct or is null if (!PasswordHasher.validPassword(socialUser.password)) { //The user is trying to log without signUp first return(BadRequest(new { error = "NotSocialSignYet" }));//No registrado } //The new user doesn't exist and his password is correct and != null user = addSocialUser(socialUser); Home.Util.GroupNew.launch(user, null, null, Home.Models.TypeGroupNew.WELCOME, false, _context); } else //The new user already exists { //The new user already exists but he has sent a new password (wtf?) if (PasswordHasher.validPassword(socialUser.password) || socialUser.password != null) { if (user.dateDeleted != null) { return(BadRequest(new { error = "DeleteRequested" })); } //The user is trying to reSignUp again return(BadRequest(new { error = "EmailAlreadyExistsError" })); } if (!user.open) { return(BadRequest(new { error = "YoureBanned" })); } if (user.dateDeleted != null) { //The user asked for delete the account, but he has log in to reset the delete request ResetDelete.reset(user, _context); Home.Util.GroupNew.launch(user, null, null, Home.Models.TypeGroupNew.WELCOMEBACK, false, _context); } //Here the user already exists and doesn't send a password, so is // trying to do a normal logIn } if (AdminPolicy.isAdmin(user, _context)) { return(BadRequest("notAllowed")); } UserSession session = MakeUserSession.getUserSession(_context, user, socialUser.provider); if (session == null) { return(StatusCode(500)); } return(Ok(session)); } catch (Exception) { return(StatusCode(500)); } }