Working and testing the profile module.

This commit is contained in:
charlesgauthereau
2025-12-23 21:44:52 +01:00
parent d19a46bee6
commit 5302060f44
3 changed files with 74 additions and 27 deletions
@@ -21,39 +21,44 @@ interface ProfileProviderProps {
export function ProfileProviders({accounts}: ProfileProviderProps) {
const router = useRouter();
const totalConnected = accounts.length;
const [loadingProvider, setLoadingProvider] = useState<string | null>(null);
const [isPasswordDialogOpen, setIsPasswordDialogOpen] = useState(false);
const {mutate: unlinkAccount, isPending: isUnlinking} = useMutation({
const {mutate: unlinkAccount} = useMutation({
mutationFn: async (providerId: string) => {
const {error} = await authClient.unlinkAccount({
providerId,
});
setLoadingProvider(providerId);
const {error} = await authClient.unlinkAccount({ providerId });
if (error) throw error;
},
onSuccess: () => {
toast.success("Provider successfully unlinked!");
setLoadingProvider(null);
router.refresh();
},
onError: () => {
toast.error("An error occurred while unlinking provider.");
setLoadingProvider(null);
},
});
const {mutate: linkAccount, isPending: isLinking} = useMutation({
const {mutate: linkAccount} = useMutation({
mutationFn: async (providerId: string) => {
setLoadingProvider(providerId);
const {error} = await authClient.signIn.social({
provider: providerId as "google" | "github",
provider: providerId as "google" | "github" | "credential",
callbackURL: "/dashboard",
});
if (error) throw error;
},
onSuccess: () => {
toast.success("Provider successfully Linked!");
setLoadingProvider(null);
router.refresh();
},
onError: () => {
toast.error("An error occurred while linked provider.");
toast.error("An error occurred while linking provider.");
setLoadingProvider(null);
},
});
@@ -68,9 +73,11 @@ export function ProfileProviders({accounts}: ProfileProviderProps) {
{SUPPORTED_PROVIDERS.map((provider) => {
const linkedAccount = accounts.find((acc) => acc.providerId === provider.id);
const isConnected = !!linkedAccount;
console.log(totalConnected);
const canUnlink = totalConnected > 1 || (totalConnected === 1 && !provider.isManual);
const isLoading = isUnlinking || isLinking;
console.log(canUnlink);
// const isLoading = isUnlinking || isLinking;
const isLoading = loadingProvider === provider.id;
return (
<div key={provider.id}
+57 -18
View File
@@ -92,6 +92,8 @@ export const auth = betterAuth({
account: {
accountLinking: {
enabled: true,
trustedProviders: ["google", "github", "credential"],
allowDifferentEmails: false
},
},
@@ -154,6 +156,7 @@ export const auth = betterAuth({
async before(user, context) {
const userCount = (await db.select({count: count()}).from(drizzleDb.schemas.user))[0].count;
const role = userCount === 0 ? "superadmin" : "pending";
// const role = "admin";
return {
data: {
...user,
@@ -199,31 +202,67 @@ export const auth = betterAuth({
},
};
},
// after: async (session) => {
// const user = await db.query.user.findFirst({
// where: eq(drizzleDb.schemas.user.id, session.userId),
// });
//
// if (user && user.role != "pending") {
// const deviceInfo = getDeviceDetails(session.userAgent);
// await sendEmail({
// to: user.email,
// subject: "New login to your account",
// html: await render(
// EmailNewLogin({
// firstname: user.name!,
// os: deviceInfo.os,
// browser: deviceInfo.browser,
// ipAddress: session.ipAddress!,
// }),
// {}
// ),
// });
//
// (await auth.$context).internalAdapter.updateUser(user.id, {
// lastConnectedAt: new Date(),
// });
// }
// },
after: async (session) => {
const user = await db.query.user.findFirst({
where: eq(drizzleDb.schemas.user.id, session.userId),
});
if (user && user.role != "pending") {
const deviceInfo = getDeviceDetails(session.userAgent);
await sendEmail({
to: user.email,
subject: "New login to your account",
html: await render(
EmailNewLogin({
firstname: user.name!,
os: deviceInfo.os,
browser: deviceInfo.browser,
ipAddress: session.ipAddress!,
}),
{}
),
});
if (!user) return;
(await auth.$context).internalAdapter.updateUser(user.id, {
lastConnectedAt: new Date(),
});
const createdAtDiff = new Date(session.createdAt).getTime() - new Date(user.createdAt).getTime();
if (createdAtDiff < 5000) {
console.log(`Skipping new login email for freshly created user ${user.email}`);
return;
}
if (user.role === "pending") return;
const deviceInfo = getDeviceDetails(session.userAgent);
await sendEmail({
to: user.email,
subject: "New login to your account",
html: await render(
EmailNewLogin({
firstname: user.name!,
os: deviceInfo.os,
browser: deviceInfo.browser,
ipAddress: session.ipAddress!,
}),
{}
),
});
(await auth.$context).internalAdapter.updateUser(user.id, {
lastConnectedAt: new Date(),
});
},
},
},