diff --git a/pages/api/auth/[...nextauth].ts b/pages/api/auth/[...nextauth].ts index 4c6d35f..220d66e 100644 --- a/pages/api/auth/[...nextauth].ts +++ b/pages/api/auth/[...nextauth].ts @@ -20,7 +20,8 @@ import { isLoggedIn, setAuthTokens, clearAuthTokens, getAccessToken, getRefreshT import { create } from "domain" -console.log("appleID:", process.env.APPLE_APP_ID); +//console.log("appleID:", process.env.APPLE_APP_ID); + // console.log(process.env.EMAIL_SERVER) // For more information on each option (and a full list of options) go to // https://next-auth.js.org/configuration/options @@ -170,38 +171,44 @@ export const authOptions: NextAuthOptions = { console.log("[nextauth] signIn:", account.provider, user.email) //if (account.provider === 'google' ) { - try { - // Check user in your database and assign roles - const dbUser = await prisma.publisher.findUnique({ - where: { email: user.email } + + // Check user in your database and assign roles + const dbUser = await prisma.publisher.findUnique({ + where: { email: user.email } + }); + + if (dbUser) { + // Assign roles from your database to the session + user.role = dbUser.role; + user.id = dbUser.id; + //user.permissions = dbUser.permissions; + const session = { ...user }; + + await prisma.publisher.update({ + where: { id: dbUser.id }, + data: { lastLogin: new Date() } }); - - if (dbUser) { - // Assign roles from your database to the session - user.role = dbUser.role; - user.id = dbUser.id; - //user.permissions = dbUser.permissions; - const session = { ...user }; - - await prisma.publisher.update({ - where: { id: dbUser.id }, - data: { lastLogin: new Date() } - }); - return true; // Sign-in successful - } else { - // Optionally create a new user in your DB - // Or return false to deny access - //Let's customize the error message to give a better user experience - throw new Error(`Твоят имейл '${user.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`); - } - } catch (e) { - console.log(e); + return true; + } else { + //user nor found in our database. deny access, showing error message. logout and redirect to message page + //throw new Error(`Твоят имейл '${user.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`); + throw new Error(`UserNotFound&email=${encodeURIComponent(user?.email)}`); } - //} - return true; // Allow other providers or default behavior }, + // async redirect({ url, baseUrl, user }) { + // // Redirect based on the user or error + // console.log("[nextauth] redirect", url, baseUrl, user) + // if (user) { + // return url; + // } else if (url.includes('error=UserNotFound')) { + // // Redirect to a custom error page or display an error + // return `${baseUrl}/error=UserNotFound&mail=${encodeURIComponent(user?.email)}`; + // } + // return baseUrl; + // }, + // Persist the OAuth access_token to the token right after signin async jwt({ token, user, account, profile, isNewUser }) { //!console.log("[nextauth] JWT", token, user) diff --git a/pages/auth/signin.tsx b/pages/auth/signin.tsx index 3f32bac..d8e9a62 100644 --- a/pages/auth/signin.tsx +++ b/pages/auth/signin.tsx @@ -57,12 +57,15 @@ export default function SignIn({ csrfToken }) {
-
+
+ {/* Page Title */} +

Вход

+ {/* Section for Social Sign-On Providers */} -
-

Влез чрез социални медии

+
+ {/*

Sign in with a Social Media Account

*/}
{/* Local Account Email and Password Form */} -
-

Влез с локален акаунт

- -
- - setEmail(e.target.value)} - className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" - /> -
-
- - setPassword(e.target.value)} - className="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" - /> -
- {error &&
{error}
} - - {/* + {/* */} -
+ +
diff --git a/pages/cart/publishers/myschedule.tsx b/pages/cart/publishers/myschedule.tsx index d994f9a..10b953f 100644 --- a/pages/cart/publishers/myschedule.tsx +++ b/pages/cart/publishers/myschedule.tsx @@ -161,7 +161,7 @@ export const getServerSideProps = async (context) => { if (!session) { return { redirect: { - destination: '/auth/login', // Adjust the login path as needed + destination: '/auth/signin', // Adjust the login path as needed permanent: false, }, }; diff --git a/pages/message.tsx b/pages/message.tsx index a3502a3..b6cb192 100644 --- a/pages/message.tsx +++ b/pages/message.tsx @@ -10,14 +10,30 @@ export default function MessagePage() { warning: "text-yellow-500", info: "text-blue-500", }; - const { message, type = messageStyles.info, caption } = router.query; + let { message, type = messageStyles.info, caption } = router.query; + + + if (router.query.error) { + switch (router.query.error) { + case 'UserNotFound': + message = `Твоят имейл '${router.query.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`; + caption = 'Грешка'; + type = messageStyles.error; + break; + default: + message = 'Възникна грешка.'; + caption = 'Грешка'; + type = messageStyles.error; + break; + } + } return (
-

{caption || 'Информация'}

-

+

{caption || 'Информация'}

+

{message || 'Така ще получавате различни съобщения.'}