apps/api: fix get user after logout
This commit is contained in:
parent
9b35854864
commit
18927d3074
@ -13,23 +13,9 @@ export class AuthService {
|
||||
) {}
|
||||
|
||||
public async login(login: string, password: string) {
|
||||
const {
|
||||
displayName,
|
||||
department,
|
||||
title,
|
||||
mail,
|
||||
sAMAccountName: username,
|
||||
} = await this.ldapService.authenticate(login, password);
|
||||
const user = await this.ldapService.authenticate(login, password);
|
||||
const { username } = user;
|
||||
|
||||
const user = {
|
||||
username,
|
||||
domain: process.env.LDAP_DOMAIN,
|
||||
displayName,
|
||||
department,
|
||||
position: title,
|
||||
mail,
|
||||
domainName: `${process.env.LDAP_DOMAIN}\\${username}`,
|
||||
};
|
||||
await this.usersCache.addUser(username, user);
|
||||
|
||||
const payload: TokenPayload = {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import type { AuthenticationOptions } from 'ldap-authentication';
|
||||
import { authenticate } from 'ldap-authentication';
|
||||
import type { User } from '../types/user';
|
||||
import type { LdapUser } from './types/user';
|
||||
|
||||
@Injectable()
|
||||
@ -19,8 +20,24 @@ export class LdapService {
|
||||
verifyUserExists: password === undefined,
|
||||
};
|
||||
|
||||
const ldapUser: LdapUser = await authenticate(options);
|
||||
const {
|
||||
displayName,
|
||||
department,
|
||||
title,
|
||||
mail,
|
||||
sAMAccountName: username,
|
||||
}: LdapUser = await authenticate(options);
|
||||
|
||||
return ldapUser;
|
||||
const user: User = {
|
||||
username,
|
||||
domain: process.env.LDAP_DOMAIN,
|
||||
displayName,
|
||||
department,
|
||||
position: title,
|
||||
mail,
|
||||
domainName: `${process.env.LDAP_DOMAIN}\\${username}`,
|
||||
};
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { CACHE_MANAGER, Inject, Injectable } from '@nestjs/common';
|
||||
import { Cache } from 'cache-manager';
|
||||
import type { User } from './types/user';
|
||||
import type { User } from '../types/user';
|
||||
|
||||
@Injectable()
|
||||
export class UsersCache {
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
import { CacheModule, Module } from '@nestjs/common';
|
||||
import * as redisStore from 'cache-manager-ioredis';
|
||||
import type { RedisOptions } from 'ioredis';
|
||||
import { LdapModule } from '../ldap/ldap.module';
|
||||
import { UsersCache } from './users.cache';
|
||||
import { UsersController } from './users.controller';
|
||||
import { UsersService } from './users.service';
|
||||
@ -13,6 +14,7 @@ import { UsersService } from './users.service';
|
||||
port: Number.parseInt(process.env.REDIS_PORT, 10) || 6379,
|
||||
ttl: Number.parseInt(process.env.CACHE_TTL, 10),
|
||||
}),
|
||||
LdapModule,
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService, UsersCache],
|
||||
|
||||
@ -1,17 +1,30 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { LdapService } from '../ldap/ldap.service';
|
||||
import type { DecodedToken } from '../auth/types/jwt';
|
||||
import { UsersCache } from './users.cache';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly usersCache: UsersCache, private readonly jwtService: JwtService) {}
|
||||
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 user = await this.usersCache.getUser(username);
|
||||
const cachedUser = await this.usersCache.getUser(username);
|
||||
|
||||
return user;
|
||||
if (!cachedUser) {
|
||||
const user = await this.ldapService.authenticate(username);
|
||||
|
||||
await this.usersCache.addUser(username, user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
return cachedUser;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user