Skip to content

Commit 8c8a874

Browse files
committed
Adjust authentication queries to reference Session ALS & currentUser global
1 parent 4787762 commit 8c8a874

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

src/components/authentication/authentication.gel.repository.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,32 +165,26 @@ export class AuthenticationGelRepository
165165
},
166166
);
167167

168-
async getCurrentPasswordHash(session: Session) {
169-
return await this.db.run(this.getCurrentPasswordHashQuery, {
170-
userId: session.userId,
171-
});
168+
async getCurrentPasswordHash() {
169+
return await this.db.run(this.getCurrentPasswordHashQuery, {});
172170
}
173-
private readonly getCurrentPasswordHashQuery = e.params(
174-
{ userId: e.uuid },
175-
({ userId }) => {
176-
const user = e.cast(e.User, userId);
177-
const identity = e.select(e.Auth.Identity, () => ({
178-
filter_single: { user },
179-
}));
180-
return identity.passwordHash;
181-
},
182-
);
171+
private readonly getCurrentPasswordHashQuery = e.params({}, () => {
172+
const user = e.global.currentUser;
173+
const identity = e.select(e.Auth.Identity, () => ({
174+
filter_single: { user },
175+
}));
176+
return identity.passwordHash;
177+
});
183178

184-
async updatePassword(newPasswordHash: string, session: Session) {
179+
async updatePassword(newPasswordHash: string) {
185180
await this.db.run(this.updatePasswordQuery, {
186-
userId: session.userId,
187181
passwordHash: newPasswordHash,
188182
});
189183
}
190184
private readonly updatePasswordQuery = e.params(
191-
{ userId: e.uuid, passwordHash: e.str },
192-
({ userId, passwordHash }) => {
193-
const user = e.cast(e.User, userId);
185+
{ passwordHash: e.str },
186+
({ passwordHash }) => {
187+
const user = e.global.currentUser;
194188
const identity = e.assert_exists(
195189
e.select(e.Auth.Identity, () => ({
196190
filter_single: { user },

src/components/authentication/authentication.repository.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ export class AuthenticationRepository {
222222
return result?.roles ?? [];
223223
}
224224

225-
async getCurrentPasswordHash(session: Session) {
225+
async getCurrentPasswordHash() {
226226
const result = await this.db
227227
.query()
228228
.match([
@@ -236,10 +236,7 @@ export class AuthenticationRepository {
236236
return result?.passwordHash ?? null;
237237
}
238238

239-
async updatePassword(
240-
newPasswordHash: string,
241-
session: Session,
242-
): Promise<void> {
239+
async updatePassword(newPasswordHash: string): Promise<void> {
243240
await this.db
244241
.query()
245242
.match([

src/components/authentication/authentication.service.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ export class AuthenticationService {
9292
throw new UnauthenticatedException('Invalid credentials');
9393
}
9494

95-
const userId = await this.repo.connectSessionToUser(input, session);
95+
const userId = await this.repo.connectSessionToUser(
96+
input,
97+
this.sessionHost.current,
98+
);
9699

97100
if (!userId) {
98101
throw new ServerException('Login failed');
@@ -261,16 +264,16 @@ export class AuthenticationService {
261264
if (!oldPassword)
262265
throw new InputException('Old Password Required', 'oldPassword');
263266

264-
const hash = await this.repo.getCurrentPasswordHash(session);
267+
const hash = await this.repo.getCurrentPasswordHash();
265268

266269
if (!(await this.crypto.verify(hash, oldPassword))) {
267270
throw new UnauthenticatedException('Invalid credentials');
268271
}
269272

270273
const newPasswordHash = await this.crypto.hash(newPassword);
271-
await this.repo.updatePassword(newPasswordHash, session);
274+
await this.repo.updatePassword(newPasswordHash);
272275

273-
await this.repo.deactivateAllOtherSessions(session);
276+
await this.repo.deactivateAllOtherSessions(this.sessionHost.current);
274277
}
275278

276279
async forgotPassword(email: string): Promise<void> {
@@ -306,7 +309,7 @@ export class AuthenticationService {
306309
await this.repo.updatePasswordViaEmailToken(emailToken, pash);
307310
await this.repo.deactivateAllOtherSessionsByEmail(
308311
emailToken.email,
309-
session,
312+
this.sessionHost.current,
310313
);
311314
await this.repo.removeAllEmailTokensForEmail(emailToken.email);
312315
}

0 commit comments

Comments
 (0)