generated from fahricansecer/boilerplate-be
@@ -146,10 +146,12 @@ export class AuthService {
|
||||
if (user.email === 'admin@contentgen.ai') {
|
||||
const hasAdminRole = user.roles.some((ur) => ur.role.name === 'admin');
|
||||
if (!hasAdminRole) {
|
||||
const adminRole = await this.prisma.role.findUnique({ where: { name: 'admin' } });
|
||||
const adminRole = await this.prisma.role.findUnique({
|
||||
where: { name: 'admin' },
|
||||
});
|
||||
if (adminRole) {
|
||||
await this.prisma.userRole.create({
|
||||
data: { userId: user.id, roleId: adminRole.id }
|
||||
data: { userId: user.id, roleId: adminRole.id },
|
||||
});
|
||||
// Refresh user object
|
||||
const refreshedUser = await this.prisma.user.findUnique({
|
||||
@@ -157,17 +159,25 @@ export class AuthService {
|
||||
include: {
|
||||
roles: {
|
||||
include: {
|
||||
role: { include: { permissions: { include: { permission: true } } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
role: {
|
||||
include: { permissions: { include: { permission: true } } },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
if (refreshedUser) {
|
||||
// Grant 999999 credits if not granted
|
||||
const existingGrant = await this.prisma.creditTransaction.findFirst({
|
||||
where: { userId: refreshedUser.id, type: 'grant', description: 'Admin başlangıç kredisi — sınırsız' },
|
||||
});
|
||||
const existingGrant = await this.prisma.creditTransaction.findFirst(
|
||||
{
|
||||
where: {
|
||||
userId: refreshedUser.id,
|
||||
type: 'grant',
|
||||
description: 'Admin başlangıç kredisi — sınırsız',
|
||||
},
|
||||
},
|
||||
);
|
||||
if (!existingGrant) {
|
||||
await this.prisma.creditTransaction.create({
|
||||
data: {
|
||||
@@ -179,7 +189,9 @@ export class AuthService {
|
||||
},
|
||||
});
|
||||
}
|
||||
return this.generateTokens(refreshedUser as unknown as UserWithRoles);
|
||||
return this.generateTokens(
|
||||
refreshedUser as unknown as UserWithRoles,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -297,13 +309,13 @@ export class AuthService {
|
||||
};
|
||||
|
||||
const isAdmin = roles.includes('admin');
|
||||
const accessExpiration = isAdmin
|
||||
? '7d'
|
||||
const accessExpiration = isAdmin
|
||||
? '7d'
|
||||
: this.configService.get('JWT_ACCESS_EXPIRATION', '15m');
|
||||
|
||||
// Generate access token
|
||||
const accessToken = this.jwtService.sign(payload, {
|
||||
expiresIn: accessExpiration as any,
|
||||
expiresIn: accessExpiration,
|
||||
});
|
||||
|
||||
// Generate refresh token
|
||||
|
||||
Reference in New Issue
Block a user