feat: Implement users/me API endpoint

This commit is contained in:
Faruk AYDIN
2024-02-13 02:04:50 +01:00
parent 95f89ba03e
commit 9f0e0ca656
7 changed files with 93 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
import { renderObject } from '../../../../helpers/renderer.js';
export default async (request, response) => {
renderObject(response, request.currentUser);
};

View File

@@ -0,0 +1,26 @@
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../../../../app.js';
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id';
import { createUser } from '../../../../../test/factories/user';
import userPayload from '../../../../../test/payloads/user';
describe('GET /api/v1/users/me', () => {
let role, currentUser, token;
beforeEach(async () => {
currentUser = await createUser();
role = await currentUser.$relatedQuery('role');
token = createAuthTokenByUserId(currentUser.id);
});
it('should return current user info', async () => {
const response = await request(app)
.get('/api/v1/users/me')
.set('Authorization', token)
.expect(200);
const expectedPayload = userPayload(currentUser, role);
expect(response.body).toEqual(expectedPayload);
});
});

View File

@@ -28,6 +28,14 @@ export const isAuthenticated = async (_parent, _args, req) => {
} }
}; };
export const authenticateUser = async (request, response, next) => {
if (await isAuthenticated(null, null, request)) {
next();
} else {
return response.status(401).end();
}
};
const isAuthenticatedRule = rule()(isAuthenticated); const isAuthenticatedRule = rule()(isAuthenticated);
export const authenticationRules = { export const authenticationRules = {

View File

@@ -143,6 +143,17 @@ class User extends Base {
}, },
}); });
$formatJson(json) {
json = super.$formatJson(json);
delete json.password;
delete json.deletedAt;
delete json.resetPasswordToken;
delete json.resetPasswordTokenSentAt;
return json;
}
login(password) { login(password) {
return bcrypt.compare(password, this.password); return bcrypt.compare(password, this.password);
} }

View File

@@ -0,0 +1,9 @@
import { Router } from 'express';
import { authenticateUser } from '../../../helpers/authentication.js';
import getCurrentUserAction from '../../../controllers/api/v1/users/get-current-user.js';
const router = Router();
router.get('/me', authenticateUser, getCurrentUserAction);
export default router;

View File

@@ -4,6 +4,7 @@ import webhooksRouter from './webhooks.js';
import paddleRouter from './paddle.ee.js'; import paddleRouter from './paddle.ee.js';
import healthcheckRouter from './healthcheck.js'; import healthcheckRouter from './healthcheck.js';
import automatischRouter from './api/v1/automatisch.js'; import automatischRouter from './api/v1/automatisch.js';
import usersRouter from './api/v1/users.js';
const router = Router(); const router = Router();
@@ -12,5 +13,6 @@ router.use('/webhooks', webhooksRouter);
router.use('/paddle', paddleRouter); router.use('/paddle', paddleRouter);
router.use('/healthcheck', healthcheckRouter); router.use('/healthcheck', healthcheckRouter);
router.use('/api/v1/automatisch', automatischRouter); router.use('/api/v1/automatisch', automatischRouter);
router.use('/api/v1/users', usersRouter);
export default router; export default router;

View File

@@ -0,0 +1,32 @@
const userPayload = (currentUser, role) => {
return {
data: {
createdAt: currentUser.createdAt.toISOString(),
email: currentUser.email,
fullName: currentUser.fullName,
id: currentUser.id,
permissions: [],
role: {
createdAt: role.createdAt.toISOString(),
description: null,
id: role.id,
isAdmin: role.isAdmin,
key: role.key,
name: role.name,
updatedAt: role.updatedAt.toISOString(),
},
roleId: role.id,
trialExpiryDate: currentUser.trialExpiryDate.toISOString(),
updatedAt: currentUser.updatedAt.toISOString(),
},
meta: {
count: 1,
currentPage: null,
isArray: false,
totalPages: null,
type: 'User',
},
};
};
export default userPayload;