public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { var request = model as LoginRequestModel; var user = _unitOfWork.Repository <ApplicationUser>().Get(x => x.Email == request.Email) .Include(x => x.UserRoles) .ThenInclude(x => x.Role) .FirstOrDefault(); if (user == null || !await _userManager.CheckPasswordAsync(user, request.Password) || !user.UserRoles.Any(x => x.Role.Name == Role.User)) { throw new CustomException(HttpStatusCode.BadRequest, "credentials", "Invalid credentials"); } if (!string.IsNullOrEmpty(request.Email) && !user.EmailConfirmed) { throw new CustomException(HttpStatusCode.BadRequest, "email", "Email is not confirmed"); } if (user.IsDeleted) { throw new CustomException(HttpStatusCode.BadRequest, "general", "Your account was deleted by admin, to know more please contact administration."); } if (!user.IsActive) { throw new CustomException(HttpStatusCode.MethodNotAllowed, "general", "Your account was blocked. For more information please email to following address: "); } return(await _jwtService.BuildLoginResponse(user, request.AccessTokenLifetime)); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { var request = model as FacebookWithPhoneRequestModel; var profile = await _facebookService.GetProfile(request.Token); var userWithFacebook = _unitOfWork.Repository <ApplicationUser>().Get(x => x.FacebookId == profile.Id) .Include(x => x.VerificationTokens) .FirstOrDefault(); // If there is such user in DB - just return if (userWithFacebook != null) { var loginResponse = await _jwtService.BuildLoginResponse(userWithFacebook); return(loginResponse); } else if (userWithFacebook == null && request.PhoneNumber != null) { // Check if there is such user in DB, if so - add to it facebook id var existingUser = _unitOfWork.Repository <ApplicationUser>().Find(x => x.PhoneNumber == request.PhoneNumber); if (existingUser != null) { existingUser.FacebookId = 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 RegisterWithFacebookUsingPhoneInternalModel { PhoneNumber = request.PhoneNumber, FacebookId = profile.Id }, new JsonSerializerSettings { Formatting = Formatting.Indented }); await _smsService.SendVerificationCodeAsync(request.PhoneNumber, VerificationCodeType.ConfirmFacebook, 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 facebook id"); } }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { var request = model as LoginWithPhoneRequestModel; var user = _unitOfWork.Repository <ApplicationUser>().Find(x => x.PhoneNumber == request.PhoneNumber); if (user == null || !await _userManager.CheckPasswordAsync(user, request.Password)) { throw new CustomException(HttpStatusCode.BadRequest, "credentials", "Invalid credentials"); } if (!user.PhoneNumberConfirmed) { throw new CustomException(HttpStatusCode.BadRequest, "phoneNumber", "PhoneNumber is not confirmed"); } if (user.IsDeleted) { throw new CustomException(HttpStatusCode.BadRequest, "general", "Your account was deleted by admin, to know more please contact administration."); } if (!user.IsActive) { throw new CustomException(HttpStatusCode.MethodNotAllowed, "general", "Your account was blocked. For more information please email to following address: "); } return(await _jwtService.BuildLoginResponse(user, request.AccessTokenLifetime)); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { var request = model as AdminLoginRequestModel; var user = _unitOfWork.Repository <ApplicationUser>().Get(x => x.Email == request.Email) .TagWith(nameof(BridgeAdminImplementation) + "_GetAdmin") .Include(x => x.UserRoles) .ThenInclude(x => x.Role) .FirstOrDefault(); if (user == null || !await _userManager.CheckPasswordAsync(user, request.Password) || !user.UserRoles.Any(x => x.Role.Name == Role.Admin || x.Role.Name == Role.SuperAdmin)) { throw new CustomException(HttpStatusCode.BadRequest, "general", "Invalid credentials"); } return(await _jwtService.BuildLoginResponse(user, request.AccessTokenLifetime)); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { return(await _implementation(BridgeType.FacebookPhone).Login(model)); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { return(await _implementation(BridgeType.UserEmail).Login(model)); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { return((await _implementation(BridgeType.Admin).Login(model)) as LoginResponseModel); }
public async Task <BridgeLoginResponseModel> Login(BridgeLoginRequestModel model) { var request = model as FacebookWithEmailRequestModel; var profile = await _facebookService.GetProfile(request.Token); var userWithFacebook = _unitOfWork.Repository <ApplicationUser>().Get(x => x.FacebookId == profile.Id) .Include(x => x.VerificationTokens) .FirstOrDefault(); var email = profile?.Email ?? request.Email; // If there is such user in DB - just return if (userWithFacebook != null) { var loginResponse = await _jwtService.BuildLoginResponse(userWithFacebook); return(loginResponse); } else if (userWithFacebook == null && email != null) { // Check if there is such user in DB, if so - add to it facebook id var existingUser = _unitOfWork.Repository <ApplicationUser>().Find(x => x.Email == email); if (existingUser != null) { existingUser.FacebookId = profile.Id; _unitOfWork.Repository <ApplicationUser>().Update(existingUser); _unitOfWork.SaveChanges(); var loginResponse = await _jwtService.BuildLoginResponse(existingUser); return(loginResponse); } else { // In other case - create new user var user = CreateUserWithEmail(new RegisterWithFacebookUsingEmailInternalModel { Email = email, FacebookId = profile.Id }); var result = await _userManager.CreateAsync(user); if (!result.Succeeded) { throw new CustomException(HttpStatusCode.BadRequest, "general", result.Errors.FirstOrDefault().Description); } result = await _userManager.AddToRoleAsync(user, Role.User); if (!result.Succeeded) { throw new CustomException(HttpStatusCode.BadRequest, "general", result.Errors.FirstOrDefault().Description); } var loginResponse = await _jwtService.BuildLoginResponse(user); return(loginResponse); } } else { throw new CustomException(HttpStatusCode.BadRequest, "token", "There is no user with such facebook id"); } }