예제 #1
0
        public async Task <Result <bool> > LogoutByTokenAsync(RequestModel request)
        {
            var accessToken = await _claimsProvider.GetAccessTokenAsync();

            var sessionByToken = await _db.Sessions.AsNoTracking().FirstOrDefaultAsync(x => x.Token == accessToken);

            if (sessionByToken == null)
            {
                return(new Result <bool>("Session not found"));
            }
            if (request.UserId != _claimsProvider.GetValueByType <int>(ClaimTypes.NameIdentifier))
            {
                return(new Result <bool>("Access denited"));
            }
            if (!sessionByToken.IsActive)
            {
                return(new Result <bool>("Session is already unactive"));
            }

            sessionByToken.IsActive           = false;
            sessionByToken.DateUnActive       = DateTime.Now;
            sessionByToken.UnActiveFromDevice = sessionByToken.Device;

            _db.Sessions.Update(sessionByToken);
            await _db.SaveChangesAsync();

            _tokenManager.RemoveToken(accessToken);

            return(new Result <bool>(true));
        }