Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia
TypeScript apiapi-clientapi-restapi-testingdeveloper-toolsgraphqlhttphttp-clientpwarestrest-apispatestingtesting-toolstoolsvuevuejswebsocket
chore: added existing auth guard and user model
ankitsridhar16ab1f8437ea93
diff --git a/packages/hoppscotch-backend/src/guards/gql-auth.guard.ts b/packages/hoppscotch-backend/src/guards/gql-auth.guard.tsnew file mode 100644index 000000000..aabc77b10--- /dev/null+++ b/packages/hoppscotch-backend/src/guards/gql-auth.guard.ts@@ -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: '[email protected]',+ };++ ctx.user = authUser;++ return true;+ } else {+ return false;+ }+ } catch (e) {+ throw new Error(AUTH_FAIL);+ }+ }+}diff --git a/packages/hoppscotch-backend/src/user/user.model.ts b/packages/hoppscotch-backend/src/user/user.model.tsnew file mode 100644index 000000000..779dbc4db--- /dev/null+++ b/packages/hoppscotch-backend/src/user/user.model.ts@@ -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;+}