feat: magic-link auth complete
This commit is contained in:
@@ -1,44 +1,11 @@
|
||||
import { CanActivate, Injectable, ExecutionContext } from '@nestjs/common';
|
||||
import { 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';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@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 = {
|
||||
id: 'aabb22ccdd',
|
||||
name: 'exampleUser',
|
||||
image: 'http://example.com/avatar',
|
||||
email: 'me@example.com',
|
||||
isAdmin: false,
|
||||
createdOn: new Date(),
|
||||
};
|
||||
|
||||
ctx.user = authUser;
|
||||
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (e) {
|
||||
throw new Error(AUTH_FAIL);
|
||||
}
|
||||
export class GqlAuthGuard extends AuthGuard('jwt') {
|
||||
getRequest(context: ExecutionContext) {
|
||||
const ctx = GqlExecutionContext.create(context);
|
||||
return ctx.getContext().req;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user