Ensuring the website uses the most specific auth type with the backend when fetching and interacting with tasks

This commit is contained in:
Keith Stevens
2023-01-19 17:21:41 +09:00
parent d228ddae30
commit df1eca4eaf
5 changed files with 56 additions and 20 deletions
+38
View File
@@ -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 };