mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-09-10 11:41:04 +08:00
Ensuring the website uses the most specific auth type with the backend when fetching and interacting with tasks
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
import prisma from "src/lib/prismadb";
|
||||
import type { BackendUserCore } from "src/types/Users";
|
||||
|
||||
/**
|
||||
* Returns a `BackendUserCore` that can be used for interacting with the Backend service.
|
||||
*
|
||||
* @param {string} id The user's web auth id.
|
||||
*
|
||||
* @return {BackendUserCore} The most specific auth type and id for the user.
|
||||
*/
|
||||
const getBackendUserCore = async (id: string) => {
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
accounts: true,
|
||||
},
|
||||
});
|
||||
|
||||
// If there are no linked accounts, just use what we have locally.
|
||||
if (user.accounts.length === 0) {
|
||||
return {
|
||||
id: user.id,
|
||||
display_name: user.name,
|
||||
auth_method: "local",
|
||||
} as BackendUserCore;
|
||||
}
|
||||
|
||||
// Otherwise, use the first linked account that the user created.
|
||||
return {
|
||||
id: user.accounts[0].providerAccountId,
|
||||
display_name: user.name,
|
||||
auth_method: user.accounts[0].provider,
|
||||
} as BackendUserCore;
|
||||
};
|
||||
|
||||
export { getBackendUserCore };
|
||||
Reference in New Issue
Block a user