Store users in s3

This commit is contained in:
2023-11-20 16:58:56 -07:00
parent 725cabefe6
commit 4823f7f109
6 changed files with 74 additions and 16 deletions

View File

@@ -1,8 +1,9 @@
import { Module } from '@nestjs/common';
import { UsersService } from './users.service';
import { MinioService } from 'src/minio/minio.service';
@Module({
providers: [UsersService],
exports: [UsersService]
providers: [UsersService, MinioService],
exports: [UsersService],
})
export class UsersModule {}

View File

@@ -1,18 +1,43 @@
import { Injectable } from '@nestjs/common';
require('dotenv').config()
import { Injectable, Logger } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { Cron } from '@nestjs/schedule';
import { MinioService } from 'src/minio/minio.service';
require('dotenv').config();
// This should be a real class/interface representing a user entity
export type User = any;
export type User = {
userId: number;
username: string;
password: string;
};
@Injectable()
export class UsersService {
private readonly users = [
{
userId: 1,
username: 'admin',
password: process.env.ADMIN_PASS,
},
];
private users: User[] = [];
private readonly bucket: string;
private readonly logger: Logger = new Logger(UsersService.name);
constructor(
private readonly minioService: MinioService,
private readonly configService: ConfigService,
) {
this.bucket = configService.get<string>('S3_BUCKET', 'api.us.dev');
this.refreshUsers();
}
@Cron('* * * * *')
async refreshUsers(): Promise<void> {
this.logger.verbose('Refreshing users');
const buffer = await this.minioService.getBuffer(this.bucket, 'users.json');
this.users = JSON.parse(buffer.toString());
if (this.users === undefined) {
this.logger.error('Users undefined');
}
if (this.users.length === 0) {
this.logger.error('Users length is 0');
}
this.logger.verbose(`Refreshed with ${this.users.length} users`);
}
async findOne(username: string): Promise<User | undefined> {
return this.users.find((user) => user.username === username);