Skip to content

Commit

Permalink
feat(core): Implement authenticate mutation for Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jun 23, 2020
1 parent 5890e97 commit 357f878
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
18 changes: 17 additions & 1 deletion packages/core/src/api/resolvers/admin/auth.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { Args, Context, Mutation, Query, Resolver } from '@nestjs/graphql';
import { LoginResult, MutationLoginArgs, Permission } from '@vendure/common/lib/generated-types';
import {
LoginResult,
MutationAuthenticateArgs,
MutationLoginArgs,
Permission,
} from '@vendure/common/lib/generated-types';
import { Request, Response } from 'express';

import { ConfigService } from '../../../config/config.service';
Expand Down Expand Up @@ -35,6 +40,17 @@ export class AuthResolver extends BaseAuthResolver {
return super.login(args, ctx, req, res, 'admin');
}

@Mutation()
@Allow(Permission.Public)
authenticate(
@Args() args: MutationAuthenticateArgs,
@Ctx() ctx: RequestContext,
@Context('req') req: Request,
@Context('res') res: Response,
): Promise<LoginResult> {
return this.createAuthenticatedSession(ctx, args, req, res, 'shop');
}

@Mutation()
@Allow(Permission.Public)
logout(
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/api/schema/admin-api/auth.api.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ type Query {
}

type Mutation {
login(username: String!, password: String!, rememberMe: Boolean): LoginResult!
login(username: String!, password: String!, rememberMe: Boolean): LoginResult! @deprecated(reason: "Use `authenticate` mutation with the 'native' strategy instead.")
authenticate(input: AuthenticationInput!, rememberMe: Boolean): LoginResult!
logout: Boolean!
}

# Populated at run-time
input AuthenticationInput
10 changes: 10 additions & 0 deletions packages/core/src/service/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ export class AuthService {
if (!user) {
throw new UnauthorizedError();
}
if (!user.roles || !user.roles[0]?.channels) {
const userWithRoles = await this.connection
.getRepository(User)
.createQueryBuilder('user')
.leftJoinAndSelect('user.roles', 'role')
.leftJoinAndSelect('role.channels', 'channel')
.where('user.id = :userId', { userId: user.id })
.getOne();
user.roles = userWithRoles?.roles || [];
}

if (this.configService.authOptions.requireVerification && !user.verified) {
throw new NotVerifiedError();
Expand Down

0 comments on commit 357f878

Please sign in to comment.