Skip to content

Commit

Permalink
fix(core): Improved resolution of Administrator.user
Browse files Browse the repository at this point in the history
Relates to #1489. The previous resolution method would fail when using a
non-native auth strategy that was not based on an email identifier.
  • Loading branch information
michaelbromley committed Jan 23, 2024
1 parent daa4731 commit c2a4685
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
3 changes: 1 addition & 2 deletions docs/docs/guides/core-concepts/auth/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -546,8 +546,7 @@ export class KeycloakAuthenticationStrategy implements AuthenticationStrategy<Ke
return this.externalAuthenticationService.createAdministratorAndUser(ctx, {
strategy: this.name,
externalIdentifier: userInfo.sub,
// identifier and emailAddress should be equal
identifier: userInfo.email,
identifier: userInfo.preferred_username,
emailAddress: userInfo.email,
firstName: userInfo.given_name,
lastName: userInfo.family_name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { Parent, ResolveField, Resolver } from '@nestjs/graphql';

import { EntityNotFoundError, InternalServerError } from '../../../common/error/errors';
import { TransactionalConnection } from '../../../connection/index';
import { Administrator } from '../../../entity/administrator/administrator.entity';
import { User } from '../../../entity/user/user.entity';
import { UserService } from '../../../service/services/user.service';
import { RequestContext } from '../../common/request-context';
import { Ctx } from '../../decorators/request-context.decorator';

@Resolver('Administrator')
export class AdministratorEntityResolver {
constructor(private userService: UserService) {}
constructor(private connection: TransactionalConnection) {}

@ResolveField()
async user(@Ctx() ctx: RequestContext, @Parent() administrator: Administrator): Promise<User> {
if (administrator.user) {
return administrator.user;
}
const user = await this.userService.getUserByEmailAddress(ctx, administrator.emailAddress);
if (!user) {
throw new EntityNotFoundError('User', '<not found>');
}
const { user } = await this.connection.getEntityOrThrow(ctx, Administrator, administrator.id, {
relations: {
user: { roles: true },
},
});
return user;
}
}

0 comments on commit c2a4685

Please sign in to comment.