Merge branch 'chore/backend-integration' of https://github.com/hoppscotch/self-hosted into chore/backend-integration

This commit is contained in:
Balu Babu
2022-12-07 20:33:08 +05:30
2 changed files with 69 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
import { CanActivate, Injectable, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { User } from '../user/user.model';
import { IncomingHttpHeaders } from 'http2';
import { AUTH_FAIL } from 'src/errors';
@Injectable()
export class GqlAuthGuard implements CanActivate {
// eslint-disable-next-line @typescript-eslint/no-empty-function
constructor() {}
async canActivate(context: ExecutionContext): Promise<boolean> {
try {
const ctx = GqlExecutionContext.create(context).getContext<{
reqHeaders: IncomingHttpHeaders;
user: User | null;
}>();
if (
ctx.reqHeaders.authorization &&
ctx.reqHeaders.authorization.startsWith('Bearer ')
) {
const idToken = ctx.reqHeaders.authorization.split(' ')[1];
const authUser: User = {
uid: 'aabb22ccdd',
displayName: 'exampleUser',
photoURL: 'http://example.com/avatar',
email: 'me@example.com',
};
ctx.user = authUser;
return true;
} else {
return false;
}
} catch (e) {
throw new Error(AUTH_FAIL);
}
}
}

View File

@@ -0,0 +1,27 @@
import { ObjectType, ID, Field } from '@nestjs/graphql';
@ObjectType()
export class User {
@Field(() => ID, {
description: 'Firebase UID of the user',
})
uid: string;
@Field({
nullable: true,
description: 'Displayed name of the user (if given)',
})
displayName?: string;
@Field({
nullable: true,
description: 'Email of the user (if given)',
})
email?: string;
@Field({
nullable: true,
description: 'URL to the profile photo of the user (if given)',
})
photoURL?: string;
}