Migrate frontend from MongoDB/Firebase to Odoo 19 backend
- Remove all MongoDB and Firebase dependencies - Add Odoo proxy layer (src/lib/odoo.ts) for JWT-authenticated API forwarding - Rewrite auth routes (login, logout, register, reset) to use Odoo endpoints - Add catch-all API route ([...path].ts) to proxy remaining endpoints to Odoo - Rewrite special-case routes for file uploads and binary responses - Delete ~85 legacy API routes now handled by catch-all proxy - Replace *.be.ts MongoDB utilities with stub implementations for SSR compat - Update Dockerfile: remove MONGODB_URI, switch from yarn to npm - Update session.ts to store Odoo JWT token Made-with: Cursor
This commit is contained in:
@@ -1,34 +1,46 @@
|
||||
import {NextApiRequest, NextApiResponse} from "next";
|
||||
import {getAuth, signInWithEmailAndPassword} from "firebase/auth";
|
||||
import {app} from "@/firebase";
|
||||
import {sessionOptions} from "@/lib/session";
|
||||
import {withIronSessionApiRoute} from "iron-session/next";
|
||||
import {User} from "@/interfaces/user";
|
||||
import client from "@/lib/mongodb";
|
||||
|
||||
const auth = getAuth(app);
|
||||
import {ODOO_URL} from "@/lib/odoo";
|
||||
|
||||
export default withIronSessionApiRoute(login, sessionOptions);
|
||||
|
||||
async function login(req: NextApiRequest, res: NextApiResponse) {
|
||||
const {email, password} = req.body as {email: string; password: string};
|
||||
|
||||
signInWithEmailAndPassword(auth, email.toLowerCase(), password)
|
||||
.then(async (userCredentials) => {
|
||||
const userId = userCredentials.user.uid;
|
||||
|
||||
const db = client.db(process.env.MONGODB_DB);
|
||||
const user = await db.collection("users").findOne<User>({id: userId});
|
||||
|
||||
if (!user) return res.status(401).json({error: 401, message: "User does not exist!"});
|
||||
|
||||
req.session.user = {...user, id: userId};
|
||||
await req.session.save();
|
||||
|
||||
res.status(200).json({user: {...user, id: userId}});
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
res.status(401).json({error});
|
||||
try {
|
||||
const resp = await fetch(`${ODOO_URL}/api/login`, {
|
||||
method: "POST",
|
||||
headers: {"Content-Type": "application/json"},
|
||||
body: JSON.stringify({email: email.toLowerCase(), password}),
|
||||
});
|
||||
|
||||
const data = await resp.json();
|
||||
|
||||
if (!resp.ok) {
|
||||
return res.status(resp.status).json(data);
|
||||
}
|
||||
|
||||
const statusMap: Record<string, string> = {
|
||||
active: "active",
|
||||
inactive: "disabled",
|
||||
suspended: "disabled",
|
||||
};
|
||||
|
||||
const user = {
|
||||
...data.user,
|
||||
id: String(data.user.id),
|
||||
status: statusMap[data.user.status] || data.user.status || "active",
|
||||
permissions: Array.isArray(data.user.permissions) ? data.user.permissions : [],
|
||||
};
|
||||
|
||||
req.session.user = user;
|
||||
req.session.token = data.token;
|
||||
await req.session.save();
|
||||
|
||||
res.status(200).json({user});
|
||||
} catch (error) {
|
||||
console.error("Login error:", error);
|
||||
res.status(500).json({error: "Login failed"});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user