Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions plugins/apps-js/email/src/routes/email.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,25 @@ router.post('/webhook', webhookController.handleWebhook);
* @desc Generate and draft an email
* @access Private
*/
router.post('/draft', catchAsync(async (req, res) => {
router.post('/draft', auth, catchAsync(async (req, res) => {
const {
recipientEmail,
recipientName,
subject,
content,
userId,
userRequest,
tone = 'professional',
format = 'text'
} = req.body;
const userId = req.user?.id;
const requestedUserId = req.body.userId;

if (!recipientEmail) {
throw ErrorFactory.badRequest('Recipient email is required', 'missing_parameter');
}

if (!userId) {
throw ErrorFactory.badRequest('User ID is required', 'missing_parameter');
if (requestedUserId && requestedUserId !== userId) {
throw ErrorFactory.forbidden('Authenticated user does not match requested user');
}

// Either content or userRequest must be provided
Expand Down Expand Up @@ -106,17 +107,22 @@ router.post('/draft', catchAsync(async (req, res) => {
* @desc Send a drafted email
* @access Private
*/
router.post('/send', catchAsync(async (req, res) => {
router.post('/send', auth, catchAsync(async (req, res) => {
const {
recipientEmail,
subject,
content,
userId,
format = 'text'
} = req.body;
const userId = req.user?.id;
const requestedUserId = req.body.userId;

if (!recipientEmail || !subject || !content || !userId) {
throw ErrorFactory.badRequest('Recipient email, subject, content, and userId are required', 'missing_parameter');
if (!recipientEmail || !subject || !content) {
throw ErrorFactory.badRequest('Recipient email, subject, and content are required', 'missing_parameter');
}

if (requestedUserId && requestedUserId !== userId) {
throw ErrorFactory.forbidden('Authenticated user does not match requested user');
}

// Get authenticated user and send function
Expand Down Expand Up @@ -570,4 +576,4 @@ router.post('/search', auth, async (req, res) => {
}
});

module.exports = router;
module.exports = router;
6 changes: 3 additions & 3 deletions plugins/apps-js/public/email.html
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ <h3>3. Tips for Best Results</h3>
<section id="auth-section" class="login-info hidden">
<h2>Authentication</h2>
<p>To use Email Assistant, you need to be logged in with your email account.</p>
<p><a id="login-link" href="/api/email/auth/login" class="button">Login with Email</a></p>
<p><a id="login-link" href="/api/auth/login" class="button">Login with Email</a></p>
</section>

<section class="instructions">
Expand Down Expand Up @@ -685,7 +685,7 @@ <h3>📊 Presentation App</h3>
if (omiuid) {
setupNotice.classList.add('hidden');
authSection.classList.remove('hidden');
loginLink.href = `/api/email/auth/login?uid=${encodeURIComponent(omiuid)}`;
loginLink.href = `/api/auth/login?uid=${encodeURIComponent(omiuid)}`;
} else {
setupNotice.classList.remove('hidden');
authSection.classList.add('hidden');
Expand All @@ -694,4 +694,4 @@ <h3>📊 Presentation App</h3>
};
</script>
</body>
</html>
</html>
6 changes: 3 additions & 3 deletions plugins/apps-js/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ <h4>Presentation Generator Tips</h4>
<section id="auth-section" class="login-info hidden">
<h2>Authentication</h2>
<p>To use the Email Assistant, you need to be logged in with your email account.</p>
<p><a id="login-link" href="/api/email/auth/login" class="button">Login with Email</a></p>
<p><a id="login-link" href="/api/auth/login" class="button">Login with Email</a></p>
<div class="note" style="margin-top: 20px;">
<p><strong>Note:</strong> Presentation Generator doesn't require separate authentication - it works directly with your Omi device.</p>
</div>
Expand Down Expand Up @@ -746,7 +746,7 @@ <h3>📊 Presentation Interface</h3>
if (omiuid) {
setupNotice.classList.add('hidden');
authSection.classList.remove('hidden');
loginLink.href = `/api/email/auth/login?uid=${encodeURIComponent(omiuid)}`;
loginLink.href = `/api/auth/login?uid=${encodeURIComponent(omiuid)}`;
} else {
setupNotice.classList.remove('hidden');
authSection.classList.add('hidden');
Expand All @@ -755,4 +755,4 @@ <h3>📊 Presentation Interface</h3>
};
</script>
</body>
</html>
</html>
9 changes: 8 additions & 1 deletion plugins/apps-js/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ const {
} = require('./email/src/config/constants');

// Import services
const { checkSupabaseConnection } = require('./email/src/services/authService');
const { checkSupabaseConnection, getAuthenticatedUser } = require('./email/src/services/authService');
const { initializeDatabase } = require('./email/src/utils/dbUtils');
const { closeRedisConnection } = require('./email/src/utils/redisUtils');
const { draftEmail: sendEmailWithGmail } = require('./email/src/utils/emailUtils');
// Import routes
const authRouter = require('./email/src/routes/auth');
const emailRouter = require('./email/src/routes/email');
const deckRouter = require('./deck/src/routes/deck');

Expand Down Expand Up @@ -48,6 +50,10 @@ app.use(cookieParser());
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));

// Shared services used by route modules.
app.locals.getAuthenticatedUser = getAuthenticatedUser;
app.locals.sendEmail = sendEmailWithGmail;

// Rate limiting middleware
const requestCounts = new Map();

Expand Down Expand Up @@ -95,6 +101,7 @@ app.use((req, res, next) => {


// Routes
app.use('/api/auth', authRouter);
app.use('/api/email', emailRouter);
app.use('/api/deck', deckRouter);

Expand Down