Merge branch 'chore/backend-integration' of https://github.com/hoppscotch/self-hosted into chore/backend-integration
This commit is contained in:
42
packages/hoppscotch-backend/src/guards/gql-auth.guard.ts
Normal file
42
packages/hoppscotch-backend/src/guards/gql-auth.guard.ts
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
27
packages/hoppscotch-backend/src/user/user.model.ts
Normal file
27
packages/hoppscotch-backend/src/user/user.model.ts
Normal 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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user