mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00
feat: bun api support 🥟 (#1021)
* chore: update bun lockfile * feat: backend api bun support, jose used in bun runtime * fix: add missing await for signPayload call
This commit is contained in:
parent
c0e2c58c03
commit
e7ca40b5ab
11 changed files with 128 additions and 29 deletions
|
@ -4,6 +4,7 @@ const {
|
|||
resetPassword,
|
||||
setAuthTokens,
|
||||
} = require('../services/AuthService');
|
||||
const jose = require('jose');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const Session = require('../../models/Session');
|
||||
const User = require('../../models/User');
|
||||
|
@ -76,7 +77,13 @@ const refreshController = async (req, res) => {
|
|||
}
|
||||
|
||||
try {
|
||||
const payload = jwt.verify(refreshToken, process.env.JWT_REFRESH_SECRET);
|
||||
let payload;
|
||||
if (typeof Bun !== 'undefined') {
|
||||
const secret = new TextEncoder().encode(process.env.JWT_REFRESH_SECRET);
|
||||
({ payload } = await jose.jwtVerify(refreshToken, secret));
|
||||
} else {
|
||||
payload = jwt.verify(refreshToken, process.env.JWT_REFRESH_SECRET);
|
||||
}
|
||||
const userId = payload.id;
|
||||
const user = await User.findOne({ _id: userId });
|
||||
if (!user) {
|
||||
|
@ -99,7 +106,7 @@ const refreshController = async (req, res) => {
|
|||
const token = await setAuthTokens(userId, res, session._id);
|
||||
const userObj = user.toJSON();
|
||||
res.status(200).send({ token, user: userObj });
|
||||
} else if (payload.exp > Date.now() / 1000) {
|
||||
} else if (payload.exp < Date.now() / 1000) {
|
||||
res.status(403).redirect('/login');
|
||||
} else {
|
||||
res.status(401).send('Refresh token expired or not found for this user');
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue