Evo.Auth/apps/api/src/users/users.service.ts
2023-08-01 15:23:30 +03:00

31 lines
817 B
TypeScript

import type { DecodedToken } from '../auth/types/jwt';
import { LdapService } from '../ldap/ldap.service';
import { UsersCache } from './users.cache';
import { Injectable } from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
@Injectable()
export class UsersService {
constructor(
private readonly usersCache: UsersCache,
private readonly jwtService: JwtService,
private readonly ldapService: LdapService
) {}
public async getUser(token: string) {
const { username } = this.jwtService.decode(token) as DecodedToken;
const cachedUser = await this.usersCache.getUser(username);
if (!cachedUser) {
const user = await this.ldapService.authenticate(username);
await this.usersCache.addUser(username, user);
return user;
}
return cachedUser;
}
}