2021-08-12 15:04:37 +02:00
|
|
|
import NotFoundError from '../../lib/error/notfound-error';
|
2021-04-16 15:29:23 +02:00
|
|
|
import {
|
2021-08-12 15:04:37 +02:00
|
|
|
IResetQuery,
|
2021-04-16 15:29:23 +02:00
|
|
|
IResetToken,
|
|
|
|
IResetTokenCreate,
|
|
|
|
IResetTokenQuery,
|
2021-08-12 15:04:37 +02:00
|
|
|
IResetTokenStore,
|
|
|
|
} from '../../lib/types/stores/reset-token-store';
|
2021-04-16 15:29:23 +02:00
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
export default class FakeResetTokenStore implements IResetTokenStore {
|
2021-04-16 15:29:23 +02:00
|
|
|
data: IResetToken[];
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.data = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async getActive(token: string): Promise<IResetToken> {
|
2021-08-12 15:04:37 +02:00
|
|
|
const row = this.data.find((tokens) => tokens.token === token);
|
2021-04-16 15:29:23 +02:00
|
|
|
if (!row) {
|
|
|
|
throw new NotFoundError();
|
|
|
|
}
|
|
|
|
return row;
|
|
|
|
}
|
|
|
|
|
|
|
|
async insert(newToken: IResetTokenCreate): Promise<IResetToken> {
|
|
|
|
const token = {
|
|
|
|
userId: newToken.user_id,
|
|
|
|
token: newToken.reset_token,
|
|
|
|
expiresAt: newToken.expires_at,
|
|
|
|
createdBy: newToken.created_by,
|
|
|
|
createdAt: new Date(),
|
|
|
|
};
|
|
|
|
this.data.push(token);
|
|
|
|
return Promise.resolve(token);
|
|
|
|
}
|
|
|
|
|
2021-08-12 15:04:37 +02:00
|
|
|
async delete(token: string): Promise<void> {
|
2021-04-16 15:29:23 +02:00
|
|
|
this.data.splice(
|
2021-08-12 15:04:37 +02:00
|
|
|
this.data.findIndex((t) => t.token === token),
|
2021-04-16 15:29:23 +02:00
|
|
|
1,
|
|
|
|
);
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteExpired(): Promise<void> {
|
|
|
|
throw new Error('Not implemented in mock');
|
|
|
|
}
|
2021-08-12 15:04:37 +02:00
|
|
|
|
|
|
|
async deleteAll(): Promise<void> {
|
|
|
|
this.data = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteFromQuery(query: IResetTokenQuery): Promise<void> {
|
|
|
|
this.data = this.data.filter(
|
|
|
|
(t) => t.userId !== query.user_id && t.token !== query.reset_token,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(): void {}
|
|
|
|
|
|
|
|
async exists(token: string): Promise<boolean> {
|
|
|
|
return this.data.some((f) => f.token === token);
|
|
|
|
}
|
|
|
|
|
|
|
|
async expireExistingTokensForUser(user_id: number): Promise<void> {
|
|
|
|
this.data
|
|
|
|
.filter((f) => f.userId === user_id)
|
|
|
|
.forEach((t) => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
t.expiresAt = new Date();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async get(token: string): Promise<IResetToken> {
|
|
|
|
return this.data.find((t) => t.token === token);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getActiveTokens(): Promise<IResetToken[]> {
|
|
|
|
const now = new Date();
|
|
|
|
return this.data.filter((t) => t.expiresAt > now);
|
|
|
|
}
|
|
|
|
|
|
|
|
async getAll(): Promise<IResetToken[]> {
|
|
|
|
return this.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async useToken(token: IResetQuery): Promise<boolean> {
|
|
|
|
if (this.exists(token.token)) {
|
|
|
|
const d = this.data.find(
|
|
|
|
(t) => t.usedAt === null && t.token === token.token,
|
|
|
|
);
|
|
|
|
d.usedAt = new Date();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-04-16 15:29:23 +02:00
|
|
|
}
|