mirror of
https://github.com/Portabase/portabase.git
synced 2026-07-14 11:16:13 +02:00
Working and testing the profile module.
This commit is contained in:
@@ -81,6 +81,7 @@ export const SUPPORTED_PROVIDERS: AuthProviderConfig[] = [
|
|||||||
{
|
{
|
||||||
id: "google",
|
id: "google",
|
||||||
icon: Chrome,
|
icon: Chrome,
|
||||||
|
isManual: true,
|
||||||
credentials: {
|
credentials: {
|
||||||
clientId: process.env.GOOGLE_CLIENT_ID || "",
|
clientId: process.env.GOOGLE_CLIENT_ID || "",
|
||||||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "",
|
||||||
|
|||||||
@@ -21,39 +21,44 @@ interface ProfileProviderProps {
|
|||||||
export function ProfileProviders({accounts}: ProfileProviderProps) {
|
export function ProfileProviders({accounts}: ProfileProviderProps) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const totalConnected = accounts.length;
|
const totalConnected = accounts.length;
|
||||||
|
const [loadingProvider, setLoadingProvider] = useState<string | null>(null);
|
||||||
|
|
||||||
const [isPasswordDialogOpen, setIsPasswordDialogOpen] = useState(false);
|
const [isPasswordDialogOpen, setIsPasswordDialogOpen] = useState(false);
|
||||||
|
|
||||||
const {mutate: unlinkAccount, isPending: isUnlinking} = useMutation({
|
const {mutate: unlinkAccount} = useMutation({
|
||||||
mutationFn: async (providerId: string) => {
|
mutationFn: async (providerId: string) => {
|
||||||
const {error} = await authClient.unlinkAccount({
|
setLoadingProvider(providerId);
|
||||||
providerId,
|
const {error} = await authClient.unlinkAccount({ providerId });
|
||||||
});
|
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
toast.success("Provider successfully unlinked!");
|
toast.success("Provider successfully unlinked!");
|
||||||
|
setLoadingProvider(null);
|
||||||
router.refresh();
|
router.refresh();
|
||||||
},
|
},
|
||||||
onError: () => {
|
onError: () => {
|
||||||
toast.error("An error occurred while unlinking provider.");
|
toast.error("An error occurred while unlinking provider.");
|
||||||
|
setLoadingProvider(null);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const {mutate: linkAccount, isPending: isLinking} = useMutation({
|
const {mutate: linkAccount} = useMutation({
|
||||||
mutationFn: async (providerId: string) => {
|
mutationFn: async (providerId: string) => {
|
||||||
|
setLoadingProvider(providerId);
|
||||||
const {error} = await authClient.signIn.social({
|
const {error} = await authClient.signIn.social({
|
||||||
provider: providerId as "google" | "github",
|
provider: providerId as "google" | "github" | "credential",
|
||||||
callbackURL: "/dashboard",
|
callbackURL: "/dashboard",
|
||||||
});
|
});
|
||||||
if (error) throw error;
|
if (error) throw error;
|
||||||
},
|
},
|
||||||
onSuccess: () => {
|
onSuccess: () => {
|
||||||
toast.success("Provider successfully Linked!");
|
toast.success("Provider successfully Linked!");
|
||||||
|
setLoadingProvider(null);
|
||||||
router.refresh();
|
router.refresh();
|
||||||
},
|
},
|
||||||
onError: () => {
|
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) => {
|
{SUPPORTED_PROVIDERS.map((provider) => {
|
||||||
const linkedAccount = accounts.find((acc) => acc.providerId === provider.id);
|
const linkedAccount = accounts.find((acc) => acc.providerId === provider.id);
|
||||||
const isConnected = !!linkedAccount;
|
const isConnected = !!linkedAccount;
|
||||||
|
console.log(totalConnected);
|
||||||
const canUnlink = totalConnected > 1 || (totalConnected === 1 && !provider.isManual);
|
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 (
|
return (
|
||||||
<div key={provider.id}
|
<div key={provider.id}
|
||||||
|
|||||||
+57
-18
@@ -92,6 +92,8 @@ export const auth = betterAuth({
|
|||||||
account: {
|
account: {
|
||||||
accountLinking: {
|
accountLinking: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
|
trustedProviders: ["google", "github", "credential"],
|
||||||
|
allowDifferentEmails: false
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -154,6 +156,7 @@ export const auth = betterAuth({
|
|||||||
async before(user, context) {
|
async before(user, context) {
|
||||||
const userCount = (await db.select({count: count()}).from(drizzleDb.schemas.user))[0].count;
|
const userCount = (await db.select({count: count()}).from(drizzleDb.schemas.user))[0].count;
|
||||||
const role = userCount === 0 ? "superadmin" : "pending";
|
const role = userCount === 0 ? "superadmin" : "pending";
|
||||||
|
// const role = "admin";
|
||||||
return {
|
return {
|
||||||
data: {
|
data: {
|
||||||
...user,
|
...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) => {
|
after: async (session) => {
|
||||||
const user = await db.query.user.findFirst({
|
const user = await db.query.user.findFirst({
|
||||||
where: eq(drizzleDb.schemas.user.id, session.userId),
|
where: eq(drizzleDb.schemas.user.id, session.userId),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (user && user.role != "pending") {
|
if (!user) 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, {
|
const createdAtDiff = new Date(session.createdAt).getTime() - new Date(user.createdAt).getTime();
|
||||||
lastConnectedAt: new Date(),
|
|
||||||
});
|
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(),
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user