2025-04-12 18:46:36 -04:00
|
|
|
const { Keyv } = require('keyv');
|
2023-03-16 16:22:08 -04:00
|
|
|
const express = require('express');
|
2023-03-21 19:44:31 -04:00
|
|
|
const { MeiliSearch } = require('meilisearch');
|
2025-04-17 03:07:43 +02:00
|
|
|
const { Conversation } = require('~/models/Conversation');
|
2023-12-14 07:49:27 -05:00
|
|
|
const requireJwtAuth = require('~/server/middleware/requireJwtAuth');
|
2025-04-17 03:07:43 +02:00
|
|
|
const { Message } = require('~/models/Message');
|
2023-12-14 07:49:27 -05:00
|
|
|
const { isEnabled } = require('~/server/utils');
|
|
|
|
|
const keyvRedis = require('~/cache/keyvRedis');
|
|
|
|
|
|
|
|
|
|
const router = express.Router();
|
feat: Auth and User System (#205)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* fix: issue with api error watcher (#12)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Update README.md
* Update LOCAL_INSTALL structure
* Add local testing instructions
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Fixup: LOCAL_INSTALL.md PS instructions (#200) (#30)
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
* feat: send user with completion to protect against abuse (#31)
* Fixup: LOCAL_INSTALL.md PS instructions (#200)
* server-side JWT auth implementation
* move oauth routes and strategies, fix bugs
* backend modifications for wiring up the frontend login and reg forms
* Add frontend data services for login and registration
* Add login and registration forms
* Implment auth context, functional client side auth
* protect routes with jwt auth
* finish local strategy (using local storage)
* Start setting up google auth
* disable token refresh, remove old auth middleware
* refactor client, add ApiErrorBoundary context
* disable google and facebook strategies
* fix: fix presets not displaying specific to user
* fix: fix issue with browser refresh
* fix: casing issue with User.js (#11)
* delete user.js to be renamed
* fix: fix casing issue with User.js
* comment out api error watcher temporarily
* feat: add google auth social login
* fix: make google login url dynamic based on dev/prod
* fix: bug where UI is briefly displayed before redirecting to login
* fix: fix cookie expires value for local auth
* Only load google strategy if client id and secret are provided
* Update .env.example files with new params
* fix issue with not redirecting to register form
* only show google login button if value is set in .env
* cleanup log messages
* Add label to button for google login on login form
* doc: fix client/server url values in .env.example
* feat: add error message details to registration failure
* Restore preventing paste on confirm password
* auto-login user after registering
* feat: forgot password (#24)
* make login/reg pages look like openai's
* add password reset data services
* new form designs similar to openai, add password reset pages
* add api's for password reset
* email utils for password reset
* remove bcrypt salt rounds from process.env
* refactor: restructure api auth code, consolidate routes (#25)
* add api's for password reset
* remove bcrypt salt rounds from process.env
* refactor: consolidate auth routes, use controller pattern
* refactor: code cleanup
* feat: migrate data to first user (#26)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes after refactor (#27)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: issue with auto-login when logging out then logging in with new browser window (#28)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: Update README and .env.example files with user system information (#29)
* refactor: use /api for auth routes
* fix: use user id instead of username
* feat: migrate data to first user on register
* fix: fix social login routes
* fix: fix issue with auto-login in new tab
* doc: update README and .env.example files
* Send user id to openai to protect against abuse
* add meilisearch to gitignore
* Remove webpack
---------
Co-authored-by: alfredo-f <alfredo.fomitchenko@mail.polimi.it>
---------
Co-authored-by: Danny Avila <110412045+danny-avila@users.noreply.github.com>
Co-authored-by: Alfredo Fomitchenko <alfredo.fomitchenko@mail.polimi.it>
2023-05-07 10:04:51 -07:00
|
|
|
|
2023-10-11 17:05:47 -04:00
|
|
|
const expiration = 60 * 1000;
|
|
|
|
|
const cache = isEnabled(process.env.USE_REDIS)
|
|
|
|
|
? new Keyv({ store: keyvRedis })
|
|
|
|
|
: new Keyv({ namespace: 'search', ttl: expiration });
|
|
|
|
|
|
|
|
|
|
router.use(requireJwtAuth);
|
2023-03-16 16:22:08 -04:00
|
|
|
|
|
|
|
|
router.get('/sync', async function (req, res) {
|
|
|
|
|
await Message.syncWithMeili();
|
2023-03-17 22:20:36 -04:00
|
|
|
await Conversation.syncWithMeili();
|
2023-03-16 19:38:16 -04:00
|
|
|
res.send('synced');
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-18 18:40:53 -04:00
|
|
|
router.get('/test', async function (req, res) {
|
|
|
|
|
const { q } = req.query;
|
2023-05-18 11:09:31 -07:00
|
|
|
const messages = (
|
|
|
|
|
await Message.meiliSearch(q, { attributesToHighlight: ['text'] }, true)
|
|
|
|
|
).hits.map((message) => {
|
|
|
|
|
const { _formatted, ...rest } = message;
|
|
|
|
|
return { ...rest, searchResult: true, text: _formatted.text };
|
|
|
|
|
});
|
2023-03-18 18:40:53 -04:00
|
|
|
res.send(messages);
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-21 19:31:57 -04:00
|
|
|
router.get('/enable', async function (req, res) {
|
2023-03-21 19:44:31 -04:00
|
|
|
let result = false;
|
|
|
|
|
try {
|
2023-03-21 20:06:27 -04:00
|
|
|
const client = new MeiliSearch({
|
|
|
|
|
host: process.env.MEILI_HOST,
|
2023-07-14 09:36:49 -04:00
|
|
|
apiKey: process.env.MEILI_MASTER_KEY,
|
2023-03-21 20:06:27 -04:00
|
|
|
});
|
|
|
|
|
|
2023-03-21 19:44:31 -04:00
|
|
|
const { status } = await client.health();
|
|
|
|
|
result = status === 'available' && !!process.env.SEARCH;
|
|
|
|
|
return res.send(result);
|
2023-03-21 20:06:27 -04:00
|
|
|
} catch (error) {
|
2023-03-21 19:44:31 -04:00
|
|
|
return res.send(false);
|
|
|
|
|
}
|
2023-03-21 19:31:57 -04:00
|
|
|
});
|
|
|
|
|
|
2023-03-16 16:22:08 -04:00
|
|
|
module.exports = router;
|