aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/server/repositories/refresh-token.ts
blob: e92a7441fb61625f9b19a6ec10694103eff06ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { and, eq, gt } from "drizzle-orm";
import { db, refreshTokens } from "../db/index.js";
import type { RefreshTokenRepository } from "./types.js";

export const refreshTokenRepository: RefreshTokenRepository = {
	async findValidToken(tokenHash) {
		const [token] = await db
			.select({
				id: refreshTokens.id,
				userId: refreshTokens.userId,
				expiresAt: refreshTokens.expiresAt,
			})
			.from(refreshTokens)
			.where(
				and(
					eq(refreshTokens.tokenHash, tokenHash),
					gt(refreshTokens.expiresAt, new Date()),
				),
			)
			.limit(1);
		return token;
	},

	async create(data) {
		await db.insert(refreshTokens).values({
			userId: data.userId,
			tokenHash: data.tokenHash,
			expiresAt: data.expiresAt,
		});
	},

	async deleteById(id) {
		await db.delete(refreshTokens).where(eq(refreshTokens.id, id));
	},
};