Skip to content

Commit

Permalink
Added basic auth
Browse files Browse the repository at this point in the history
  • Loading branch information
FOSSforlife committed Apr 4, 2020
1 parent 1c8629e commit 09afb55
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/modules/user/user.resolver.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Arg, Args, Mutation, Query, Resolver, FieldResolver, Root, Ctx } from 'type-graphql';
import { Arg, Args, Mutation, Query, Resolver, FieldResolver, Root, Ctx, Authorized } from 'type-graphql';
import { Inject } from 'typedi';
import { Fields, StandardDeleteResponse, UserId, BaseContext } from 'warthog';

Expand Down Expand Up @@ -37,6 +37,7 @@ export class UserResolver {
return ctx.dataLoader.loaders.User.collection.load(user);
}

@Authorized('signedIn')
@Mutation(() => User)
async createUser(@Arg('data') data: UserCreateInput, @UserId() userId: string): Promise<User> {
return this.service.create(data, userId);
Expand Down
6 changes: 4 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';

import { BaseContext, Server } from 'warthog';
import { BaseContext, Server, authChecker } from 'warthog';

import { Logger } from './logger';

Expand All @@ -15,13 +15,15 @@ interface Context extends BaseContext {
export function getServer(AppOptions = {}, dbOptions = {}) {
return new Server<Context>(
{
authChecker,
// Inject a fake user. In a real app you'd parse a JWT to add the user
context: (request: any) => {
const userId = JSON.stringify(request.headers).length.toString();

return {
user: {
id: `user:${userId}`
id: `user:${userId}`,
permissions: ['signedIn']
}
};
},
Expand Down

0 comments on commit 09afb55

Please sign in to comment.