public async Task <UserSession> Add(UserSession element) { var map = _mapper.Map <UserSessionEntity>(element); var entity = await _userSessionRepository.Add(map); return(_mapper.Map <UserSession>(entity)); }
public UserSession LogIn(string email, string password) { User user = null; try { user = userRepository.GetUserByEmailAndPassword(email, password); } catch (ClientException e) { throw new ClientBusinessLogicException(e.Message); } catch (ServerException e) { throw new ServerBusinessLogicException(e.Message); } UserSession userSession = sessionRepository.GetUserSessionByUserId(user.Id); if (userSession == null) { Guid token = Guid.NewGuid(); userSession = new UserSession() { User = user, Token = token.ToString() }; sessionRepository.Add(userSession); } return(userSession); }
public async Task <UserSession> CreateSessionForExternalUser(ExternalUserDetails externalUserDetails) { var user = await _userBusinessLogic.FindByEmail(externalUserDetails.Email); if (user == null) { user = _userBusinessLogic.CreateUser(externalUserDetails); } else { if (user.Type != externalUserDetails.UserType) { throw new InvalidOperationException("Multiple sign-in providers are not supported for the same user."); } // Check number of active sessions var activeSessionsCount = _userSessionRepository.GetActiveUserSessionsCount(user.Id); if (activeSessionsCount >= ApplicationConstants.Sessions.MaxActiveSessions) { throw new InvalidOperationException($"Max number of active sessions reached for user '{user.Id}'."); } // Update user information user.FirstName = externalUserDetails.FirstName; user.LastName = externalUserDetails.LastName; user.DisplayName = externalUserDetails.DisplayName; user.PictureUrl = externalUserDetails.PictureUrl; } var session = new UserSession { Token = Guid.NewGuid(), // TODO: use cryptographically random tokens State = UserSessionState.Active, CreationDate = DateTime.UtcNow, UserId = user.Id }; _userSessionRepository.Add(session); return(session); }