Skip to content

Commit

Permalink
chore: cherry pick to upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Mar 5, 2024
1 parent 6362cc1 commit 6e40b28
Showing 1 changed file with 18 additions and 20 deletions.
38 changes: 18 additions & 20 deletions packages/backend/server/src/core/auth/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,31 +49,29 @@ export class TokenService {
keep?: boolean;
} = {}
) {
return await this.db.$transaction(async tx => {
const record = await tx.verificationToken.findUnique({
where: {
type_token: {
token,
type,
},
const record = await this.db.verificationToken.findUnique({
where: {
type_token: {
token,
type,
},
});
},
});

if (!record) {
return null;
}
if (!record) {
return null;
}

const expired = record.expiresAt <= new Date();
const valid =
!expired && (!record.credential || record.credential === credential);
const expired = record.expiresAt <= new Date();
const valid =
!expired && (!record.credential || record.credential === credential);

// always revoke expired token
if (expired || (valid && !keep)) {
await this.revokeToken(type, token, tx);
}
// always revoke expired token
if (expired || (valid && !keep)) {
await this.revokeToken(type, token, this.db);
}

return valid ? record : null;
});
return valid ? record : null;
}

async revokeToken(type: TokenType, token: string, tx?: Transaction) {
Expand Down

0 comments on commit 6e40b28

Please sign in to comment.