Exemplo n.º 1
0
        public async Task <UserLogInModelOut> LogIn(UserLoginModelIn modelIn, UserTokenSessionModel userTokenSessionModel)
        {
            UserTokenSession userTokenSession = _mapper.Map <UserTokenSession>(userTokenSessionModel);

            UserLogInModelOut response = new UserLogInModelOut();

            User user = await _repos.Users.GetUserByEmailAndPassword(modelIn.Email, modelIn.Password);

            if (user == null)
            {
                response.AddError(CustomErrorEnum.UnsuccessfulLogIn);

                return(response);
            }

            response.Id              = user.Id;
            response.Email           = user.Email;
            response.EmailIsVerified = user.EmailIsVerified;
            response.PhoneNumber     = user.PhoneNumber;
            response.Token           = HashingUtilities.GetHashSHA512(Guid.NewGuid().ToString());

            userTokenSession.UserId         = user.Id;
            userTokenSession.Date           = DateTime.UtcNow;
            userTokenSession.LastUpdateDate = DateTime.UtcNow;
            userTokenSession.Token          = response.Token;

            await _repos.UserTokenSessions.Create(userTokenSession);

            if (await _repos.SaveAsync() == 0)
            {
                response.AddError(CustomErrorEnum.UnsuccessfulLogIn);

                return(response);
            }

            UserTokenSessionCacheModel clientTokenSessionCacheModel = _mapper.Map <UserTokenSessionCacheModel>(userTokenSession);

            _memoryCache.Set(response.Token, clientTokenSessionCacheModel, _cacheExpirationByMinutes.Value);

            return(response);
        }
Exemplo n.º 2
0
        public async Task LogOut(string token, UserLogOutModelOut response)
        {
            if (!_memoryCache.TryGetValue(token, out UserTokenSessionCacheModel userTokenSessionCacheModel))
            {
                response.AddError(CustomErrorEnum.TokenExpire);

                return;
            }

            UserTokenSession session = await _repos.UserTokenSessions.GetByIdAsync(userTokenSessionCacheModel.Id);

            session.CloseActionIsLogout = true;



            _repos.UserTokenSessions.Update(session);

            if (await _repos.SaveAsync() == 0)
            {
                response.AddError(CustomErrorEnum.UnsuccessfulLogOut);
            }
        }