Skip to content

Commit 75d9ada

Browse files
committed
Marking user disabled logs them out
1 parent 793302f commit 75d9ada

File tree

5 files changed

+46
-0
lines changed

5 files changed

+46
-0
lines changed

src/core/authentication/authentication.gel.repository.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,4 +280,18 @@ export class AuthenticationGelRepository
280280
set: { user: null },
281281
})),
282282
);
283+
284+
async deactivateAllSessions(user: ID<'User'>) {
285+
await this.db.run(this.deactivateAllSessionsQuery, { user });
286+
}
287+
private readonly deactivateAllSessionsQuery = e.params(
288+
{ user: e.uuid },
289+
($) => {
290+
const user = e.cast(e.User, $.user);
291+
return e.update(e.Auth.Session, (s) => ({
292+
filter: e.op(s.user, '=', user),
293+
set: { user: null },
294+
}));
295+
},
296+
);
283297
}

src/core/authentication/authentication.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AuthenticationGelRepository } from './authentication.gel.repository';
66
import { AuthenticationRepository } from './authentication.repository';
77
import { AuthenticationService } from './authentication.service';
88
import { CryptoService } from './crypto.service';
9+
import { DisablingUserLogsThemOutHandler } from './handlers/disabling-user-logs-them-out.handler';
910
import { Identity } from './identity.service';
1011
import { JwtService } from './jwt.service';
1112
import { LoginResolver } from './resolvers/login.resolver';
@@ -38,6 +39,8 @@ import { SessionManager } from './session/session.manager';
3839
splitDb(AuthenticationRepository, AuthenticationGelRepository),
3940
JwtService,
4041
CryptoService,
42+
43+
DisablingUserLogsThemOutHandler,
4144
],
4245
exports: [Identity],
4346
})

src/core/authentication/authentication.repository.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,18 @@ export class AuthenticationRepository {
360360
.run();
361361
}
362362

363+
async deactivateAllSessions(user: ID<'User'>) {
364+
await this.db
365+
.query()
366+
.match([
367+
node('user', 'User', { id: user }),
368+
relation('out', 'oldRel', 'token', ACTIVE),
369+
node('token', 'Token'),
370+
])
371+
.setValues({ 'oldRel.active': false })
372+
.run();
373+
}
374+
363375
@OnIndex()
364376
private createIndexes() {
365377
return [

src/core/authentication/authentication.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export class AuthenticationService {
102102
refresh && (await this.sessionManager.refreshCurrentSession());
103103
}
104104

105+
async logoutByUser(user: ID<'User'>) {
106+
await this.repo.deactivateAllSessions(user);
107+
}
108+
105109
async changePassword(
106110
oldPassword: string,
107111
newPassword: string,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { EventsHandler } from '~/core';
2+
import { UserUpdatedEvent } from '../../../components/user/events/user-updated.event';
3+
import { AuthenticationService } from '../authentication.service';
4+
5+
@EventsHandler(UserUpdatedEvent)
6+
export class DisablingUserLogsThemOutHandler {
7+
constructor(private readonly auth: AuthenticationService) {}
8+
async handle({ input, updated: user }: UserUpdatedEvent) {
9+
if (input.status === 'Disabled') {
10+
await this.auth.logoutByUser(user.id);
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)