diff --git a/app/login/page.tsx b/app/login/page.tsx index d8769aca3..3ac100f4c 100644 --- a/app/login/page.tsx +++ b/app/login/page.tsx @@ -1,7 +1,7 @@ 'use client'; import { Button, Input } from '@nextui-org/react'; -import { signIn } from 'next-auth/react'; +import { getSession, signIn } from 'next-auth/react'; import { useRouter } from 'next/navigation'; import { useState } from 'react'; @@ -15,60 +15,86 @@ export default function LoginPage() { event.preventDefault(); const res = await signIn('credentials', { username, password, redirect: false }); if (res?.ok) { - router.replace('/'); + const session = await getSession(); + const roles = session?.user?.roles ?? []; + + if (roles.includes('wcfm_vendor') || roles.includes('administrator')) { + window.location.href = `${process.env.NEXT_PUBLIC_WOOCOMMERCE}/sso-login?sso_token=${session!.user.token}`; + } else { + router.replace('/'); + } } else { setError('Invalid username or password'); } }; return ( -
-

Login

-
- {error &&

{error}

} -
-
- setUsername(e.target.value)} - required - /> -
-
- setPassword(e.target.value)} - required - /> -
-
- +
+

Login

+
+
+

+ Sei un cliente? +

+
+ {error &&

{error}

} + +
+ setUsername(e.target.value)} + required + /> +
+
+ setPassword(e.target.value)} + required + /> +
+
+ +
+ + Don't have an account?{' '} + + Sign up + + +
+
- - Don't have an account?{' '} - - Sign up - - - + {/* Box Venditore */} +
+

+ Sei un venditore? +

+

+ Accedi alla tua area riservata per gestire i tuoi prodotti e ordini. +

+ + + +
); diff --git a/app/page.tsx b/app/page.tsx index 98426efc1..a261a7155 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -21,7 +21,7 @@ import Bg2 from '../assets/images/slide-bg-02.webp'; async function Products({ category }: { category: Category }) { const products: Product[] = await woocommerce.get('products', { category: category.id.toString(), - author: 1, // Use admin user to get all products + author: 1 // Use admin user to get all products }); return ; diff --git a/app/profile/@user/page.tsx b/app/profile/@user/page.tsx index 98aa2239a..5aaf08087 100644 --- a/app/profile/@user/page.tsx +++ b/app/profile/@user/page.tsx @@ -1,63 +1,33 @@ -'use server'; - import { authOptions } from 'lib/auth/config'; import { woocommerce } from 'lib/woocomerce/woocommerce'; import { getServerSession } from 'next-auth'; import { getTranslations } from 'next-intl/server'; +import VendorArea from './vendor-area'; export default async function PersonalArea() { const session = await getServerSession(authOptions); const t = await getTranslations('ProfilePage'); + if (!session?.user?.customer_id) { return { status: 401, body: { error: 'User not logged' } }; } - const customer = await woocommerce.get('customers', { id: session?.user.customer_id }); + const user = session.user; + const { first_name, last_name, email } = await woocommerce.get('customers', { + id: user.customer_id + }); return (

{t('area')}

- - - - - - + {user.roles.includes('wcfm_vendor') && } + + + + + +
); diff --git a/app/profile/@user/vendor-area.tsx b/app/profile/@user/vendor-area.tsx new file mode 100644 index 000000000..a521a1150 --- /dev/null +++ b/app/profile/@user/vendor-area.tsx @@ -0,0 +1,21 @@ +'use client'; + +import { Button } from '@headlessui/react'; + +export default function VendorArea({ token }: { token: string }) { + const handleClick = () => { + window.open(`${process.env.NEXT_PUBLIC_WOOCOMMERCE}/sso-login?sso_token=${token}`); + }; + + return ( +
+

Sei un venditore

+

+ Accedi alla tua area riservata per gestire i tuoi prodotti e ordini. +

+ +
+ ); +} diff --git a/lib/woocomerce/storeApi.ts b/lib/woocomerce/storeApi.ts index 55efb5ed1..eb7e4f7c6 100644 --- a/lib/woocomerce/storeApi.ts +++ b/lib/woocomerce/storeApi.ts @@ -45,7 +45,7 @@ function createStoreApiClient({ method, url: baseURL + url, data, - headers, + headers }); return response; diff --git a/types/next-auth.d.ts b/types/next-auth.d.ts index d5568532c..69e72bccf 100644 --- a/types/next-auth.d.ts +++ b/types/next-auth.d.ts @@ -9,6 +9,7 @@ declare module 'next-auth' { user_email: string; user_nicename: string; user_display_name: string; + roles: string[]; }; } @@ -18,6 +19,7 @@ declare module 'next-auth' { user_email: string; user_nicename: string; user_display_name: string; + roles: string[]; } } @@ -29,6 +31,7 @@ declare module 'next-auth/jwt' { user_email: string; user_nicename: string; user_display_name: string; + roles: string[]; }; } }