Fix graphql queries
This commit is contained in:
@ -1,5 +1,4 @@
|
||||
import { Controller, Post, Req, Res, UseGuards } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { Controller, Post, Req, Res } from '@nestjs/common';
|
||||
import { Request, Response } from 'express';
|
||||
import { AuthService } from './services/auth.service';
|
||||
|
||||
@ -8,13 +7,14 @@ export class AuthController {
|
||||
constructor(private authService: AuthService) {}
|
||||
|
||||
@Post()
|
||||
generateAccessToken(@Req() req: Request, @Res() res: Response) {
|
||||
async generateAccessToken(@Req() req: Request, @Res() res: Response) {
|
||||
const refreshToken = req.body.refreshToken;
|
||||
|
||||
if (!refreshToken) {
|
||||
return res.status(400).send('Refresh token not found');
|
||||
}
|
||||
|
||||
return res.send(this.authService.generateAccessToken(refreshToken));
|
||||
const token = await this.authService.generateAccessToken(refreshToken);
|
||||
return res.send({ accessToken: token });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,4 +2,4 @@ import { Injectable } from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
||||
export class JwtAuthGuard extends AuthGuard('jwt') {}
|
||||
|
||||
@ -17,10 +17,14 @@ export class AuthService {
|
||||
private configService: ConfigService,
|
||||
private userRepository: UserRepository,
|
||||
private workspaceRepository: WorkspaceRepository,
|
||||
private refreshTokenRepository: RefreshTokenRepository
|
||||
) {}
|
||||
private refreshTokenRepository: RefreshTokenRepository,
|
||||
) {}
|
||||
|
||||
async upsertUser(rawUser: { firstName: string, lastName: string, email: string }) {
|
||||
async upsertUser(rawUser: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
email: string;
|
||||
}) {
|
||||
if (!rawUser.email) {
|
||||
return;
|
||||
}
|
||||
@ -64,20 +68,36 @@ export class AuthService {
|
||||
return user;
|
||||
}
|
||||
|
||||
generateAccessToken(refreshToken: string) {
|
||||
const refreshTokenObject = this.refreshTokenRepository.findFirst({
|
||||
where: { id: refreshToken },
|
||||
async generateAccessToken(refreshToken: string): Promise<string | undefined> {
|
||||
const refreshTokenObject = await this.refreshTokenRepository.findFirst({
|
||||
where: { refreshToken: refreshToken },
|
||||
});
|
||||
|
||||
if (!refreshTokenObject) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload: JwtPayload = { username: 'Charles', sub: 1 };
|
||||
return {
|
||||
accessToken: this.jwtService.sign(payload),
|
||||
refreshToken: refreshToken,
|
||||
const user = await this.userRepository.findUnique({
|
||||
where: { id: refreshTokenObject.userId },
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return;
|
||||
}
|
||||
|
||||
const workspace = await this.workspaceRepository.findFirst({
|
||||
where: { WorkspaceMember: { every: { userId: user.id } } },
|
||||
});
|
||||
|
||||
if (!workspace) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload: JwtPayload = {
|
||||
userId: user.id,
|
||||
workspaceId: workspace.id,
|
||||
};
|
||||
return this.jwtService.sign(payload);
|
||||
}
|
||||
|
||||
async registerRefreshToken(user: User): Promise<RefreshToken> {
|
||||
@ -93,6 +113,8 @@ export class AuthService {
|
||||
}
|
||||
|
||||
computeRedirectURI(refreshToken: string): string {
|
||||
return `${this.configService.get<string>('FRONT_AUTH_CALLBACK_URL')}?refreshToken=${refreshToken}`;
|
||||
return `${this.configService.get<string>(
|
||||
'FRONT_AUTH_CALLBACK_URL',
|
||||
)}?refreshToken=${refreshToken}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||
import { Strategy } from 'passport-jwt';
|
||||
import { PassportStrategy } from '@nestjs/passport';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
|
||||
export type JwtPayload = { sub: number; username: string };
|
||||
export type JwtPayload = { userId: string; workspaceId: string };
|
||||
|
||||
@Injectable()
|
||||
export class JwtAuthStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
@ -16,7 +16,7 @@ export class JwtAuthStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
}
|
||||
return token;
|
||||
};
|
||||
|
||||
|
||||
super({
|
||||
jwtFromRequest: extractJwtFromCookie,
|
||||
ignoreExpiration: false,
|
||||
@ -25,6 +25,6 @@ export class JwtAuthStrategy extends PassportStrategy(Strategy, 'jwt') {
|
||||
}
|
||||
|
||||
async validate(payload: JwtPayload) {
|
||||
return { id: payload.sub, username: payload.username };
|
||||
return { userId: payload.userId, workspaceId: payload.workspaceId };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user