Fix jwt-token
This commit is contained in:
@@ -93,9 +93,13 @@ describe('Authentication Middleware', () => {
|
|||||||
const user = await createTestUser({ username: 'testuser', role: 'admin' });
|
const user = await createTestUser({ username: 'testuser', role: 'admin' });
|
||||||
console.log('Created user:', user.toJSON()); // Debug log
|
console.log('Created user:', user.toJSON()); // Debug log
|
||||||
|
|
||||||
|
// Get the actual string value of the UUID
|
||||||
|
const userId = user.getDataValue('id') || user.id;
|
||||||
|
console.log('User ID for JWT:', userId, typeof userId);
|
||||||
|
|
||||||
const token = jwt.sign(
|
const token = jwt.sign(
|
||||||
{
|
{
|
||||||
userId: user.id, // Use the UUID directly
|
userId: userId,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
role: user.role,
|
role: user.role,
|
||||||
email: user.email
|
email: user.email
|
||||||
@@ -113,7 +117,7 @@ describe('Authentication Middleware', () => {
|
|||||||
await authenticateToken(req, res, next);
|
await authenticateToken(req, res, next);
|
||||||
|
|
||||||
expect(req.user).to.exist;
|
expect(req.user).to.exist;
|
||||||
expect(req.user.id).to.equal(user.id);
|
expect(req.user.id).to.equal(userId);
|
||||||
expect(req.user.username).to.equal(user.username);
|
expect(req.user.username).to.equal(user.username);
|
||||||
expect(req.user.role).to.equal(user.role);
|
expect(req.user.role).to.equal(user.role);
|
||||||
expect(next.errors).to.have.length(0);
|
expect(next.errors).to.have.length(0);
|
||||||
@@ -122,9 +126,13 @@ describe('Authentication Middleware', () => {
|
|||||||
it('should handle token with tenantId', async () => {
|
it('should handle token with tenantId', async () => {
|
||||||
const tenant = await createTestTenant({ slug: 'test-tenant' });
|
const tenant = await createTestTenant({ slug: 'test-tenant' });
|
||||||
const user = await createTestUser({ username: 'testuser', tenant_id: tenant.id });
|
const user = await createTestUser({ username: 'testuser', tenant_id: tenant.id });
|
||||||
|
|
||||||
|
// Get the actual string value of the UUID
|
||||||
|
const userId = user.getDataValue('id') || user.id;
|
||||||
|
|
||||||
const token = jwt.sign(
|
const token = jwt.sign(
|
||||||
{
|
{
|
||||||
userId: user.id.toString(), // Convert UUID to string
|
userId: userId,
|
||||||
username: user.username,
|
username: user.username,
|
||||||
role: user.role,
|
role: user.role,
|
||||||
tenantId: tenant.slug
|
tenantId: tenant.slug
|
||||||
@@ -172,8 +180,15 @@ describe('Authentication Middleware', () => {
|
|||||||
username: 'inactive',
|
username: 'inactive',
|
||||||
is_active: false
|
is_active: false
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Get the actual string value of the UUID
|
||||||
|
const userId = user.getDataValue('id') || user.id;
|
||||||
|
|
||||||
const token = jwt.sign(
|
const token = jwt.sign(
|
||||||
{ userId: user.id.toString(), username: user.username }, // Convert UUID to string
|
{
|
||||||
|
userId: userId,
|
||||||
|
username: user.username
|
||||||
|
},
|
||||||
process.env.JWT_SECRET || 'test-secret',
|
process.env.JWT_SECRET || 'test-secret',
|
||||||
{ expiresIn: '1h' }
|
{ expiresIn: '1h' }
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user