fix signin too relax bug
This commit is contained in:
@ -20,7 +20,8 @@ import { isLoggedIn, setAuthTokens, clearAuthTokens, getAccessToken, getRefreshT
|
|||||||
import { create } from "domain"
|
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)
|
// console.log(process.env.EMAIL_SERVER)
|
||||||
// For more information on each option (and a full list of options) go to
|
// For more information on each option (and a full list of options) go to
|
||||||
// https://next-auth.js.org/configuration/options
|
// https://next-auth.js.org/configuration/options
|
||||||
@ -170,38 +171,44 @@ export const authOptions: NextAuthOptions = {
|
|||||||
|
|
||||||
console.log("[nextauth] signIn:", account.provider, user.email)
|
console.log("[nextauth] signIn:", account.provider, user.email)
|
||||||
//if (account.provider === 'google' ) {
|
//if (account.provider === 'google' ) {
|
||||||
try {
|
|
||||||
// Check user in your database and assign roles
|
// Check user in your database and assign roles
|
||||||
const dbUser = await prisma.publisher.findUnique({
|
const dbUser = await prisma.publisher.findUnique({
|
||||||
where: { email: user.email }
|
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() }
|
||||||
});
|
});
|
||||||
|
return true;
|
||||||
if (dbUser) {
|
} else {
|
||||||
// Assign roles from your database to the session
|
//user nor found in our database. deny access, showing error message. logout and redirect to message page
|
||||||
user.role = dbUser.role;
|
//throw new Error(`Твоят имейл '${user.email}' не е регистриран в системата. Моля свържи се с нас за да те регистрираме ако искаш да ползваш този имейл.`);
|
||||||
user.id = dbUser.id;
|
throw new Error(`UserNotFound&email=${encodeURIComponent(user?.email)}`);
|
||||||
//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; // 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
|
// Persist the OAuth access_token to the token right after signin
|
||||||
async jwt({ token, user, account, profile, isNewUser }) {
|
async jwt({ token, user, account, profile, isNewUser }) {
|
||||||
//!console.log("[nextauth] JWT", token, user)
|
//!console.log("[nextauth] JWT", token, user)
|
||||||
|
@ -57,12 +57,15 @@ export default function SignIn({ csrfToken }) {
|
|||||||
<Layout>
|
<Layout>
|
||||||
<div className="page">
|
<div className="page">
|
||||||
<div className="signin">
|
<div className="signin">
|
||||||
<div className="min-h-screen flex flex-col items-center justify-center">
|
<div className="min-h-screen flex flex-col items-center justify-center bg-gray-100">
|
||||||
|
{/* Page Title */}
|
||||||
|
<h1 className="text-2xl font-bold text-gray-900 mt-6">Вход</h1>
|
||||||
|
|
||||||
{/* Section for Social Sign-On Providers */}
|
{/* Section for Social Sign-On Providers */}
|
||||||
<div className="mt-8 w-full max-w-xs px-4">
|
<div className="mt-8 w-full max-w-md px-4 py-8 bg-white shadow rounded-lg">
|
||||||
<h2 className="text-lg font-semibold text-gray-700 mb-4">Влез чрез социални медии</h2>
|
{/* <h2 className="text-center text-lg font-semibold text-gray-900 mb-4">Sign in with a Social Media Account</h2> */}
|
||||||
<button onClick={() => signIn('google', { callbackUrl: '/' })}
|
<button onClick={() => signIn('google', { callbackUrl: '/' })}
|
||||||
className="flex items-center justify-center w-full py-2 px-4 border border-gray-300 rounded shadow-sm text-sm text-gray-700 bg-white hover:bg-gray-50">
|
className="flex items-center justify-center w-full py-3 px-4 border border-gray-300 rounded-md shadow-sm text-sm font-medium text-gray-700 bg-white hover:bg-gray-50">
|
||||||
<img loading="lazy" height="24" width="24" alt="Google logo"
|
<img loading="lazy" height="24" width="24" alt="Google logo"
|
||||||
src="https://authjs.dev/img/providers/google.svg" className="mr-2" />
|
src="https://authjs.dev/img/providers/google.svg" className="mr-2" />
|
||||||
Влез чрез Google
|
Влез чрез Google
|
||||||
@ -76,40 +79,42 @@ export default function SignIn({ csrfToken }) {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
{/* Local Account Email and Password Form */}
|
{/* Local Account Email and Password Form */}
|
||||||
<form onSubmit={handleSubmit} className="w-full max-w-xs px-4">
|
<div className="w-full max-w-md mt-8 mb-8 px-4 py-8 bg-white shadow rounded-lg">
|
||||||
<h2 className="text-lg font-semibold text-gray-700 mb-4">Влез с локален акаунт</h2>
|
<h2 className="text-center text-lg font-semibold text-gray-900 mb-4">Влез с локален акаунт</h2>
|
||||||
<input name="csrfToken" type="hidden" defaultValue={csrfToken} />
|
<form onSubmit={handleSubmit}>
|
||||||
<div className="mb-4">
|
<input name="csrfToken" type="hidden" defaultValue={csrfToken} />
|
||||||
<label htmlFor="email" className="block text-sm font-medium text-gray-700">имейл</label>
|
<div className="mb-4">
|
||||||
<input
|
<label htmlFor="email" className="block text-sm font-medium text-gray-900">имейл</label>
|
||||||
id="email"
|
<input
|
||||||
type="text" // allow non-email addresses for username (admins)
|
id="email"
|
||||||
value={email}
|
type="text" // allow non-email addresses for username (admins)
|
||||||
onChange={(e) => setEmail(e.target.value)}
|
value={email}
|
||||||
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"
|
onChange={(e) => 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-blue-500 focus:border-blue-500 sm:text-sm"
|
||||||
</div>
|
/>
|
||||||
<div className="mb-6">
|
</div>
|
||||||
<label htmlFor="password" className="block text-sm font-medium text-gray-700">парола</label>
|
<div className="mb-6">
|
||||||
<input
|
<label htmlFor="password" className="block text-sm font-medium text-gray-900">парола</label>
|
||||||
id="password"
|
<input
|
||||||
type="password"
|
id="password"
|
||||||
value={password}
|
type="password"
|
||||||
onChange={(e) => setPassword(e.target.value)}
|
value={password}
|
||||||
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"
|
onChange={(e) => 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-blue-500 focus:border-blue-500 sm:text-sm"
|
||||||
</div>
|
/>
|
||||||
{error && <div className="text-red-500 text-sm">{error}</div>}
|
</div>
|
||||||
<button type="submit" className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700">
|
{error && <div className="text-red-500 text-sm text-center">{error}</div>}
|
||||||
Влез
|
<button type="submit" className="w-full flex justify-center py-3 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700">
|
||||||
</button>
|
Влез
|
||||||
{/* <button
|
</button>
|
||||||
|
{/* <button
|
||||||
type="button"
|
type="button"
|
||||||
className="mt-4 w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-blue-600 hover:text-blue-700 focus:outline-none"
|
className="mt-4 w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-blue-600 hover:text-blue-700 focus:outline-none"
|
||||||
onClick={() => router.push('/auth/reset-password')}>
|
onClick={() => router.push('/auth/reset-password')}>
|
||||||
Забравена парола?
|
Забравена парола?
|
||||||
</button> */}
|
</button> */}
|
||||||
</form>
|
</form>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -161,7 +161,7 @@ export const getServerSideProps = async (context) => {
|
|||||||
if (!session) {
|
if (!session) {
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: '/auth/login', // Adjust the login path as needed
|
destination: '/auth/signin', // Adjust the login path as needed
|
||||||
permanent: false,
|
permanent: false,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -10,14 +10,30 @@ export default function MessagePage() {
|
|||||||
warning: "text-yellow-500",
|
warning: "text-yellow-500",
|
||||||
info: "text-blue-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 (
|
return (
|
||||||
<Layout>
|
<Layout>
|
||||||
<div className="flex items-center justify-center min-h-screen">
|
<div className="flex items-center justify-center min-h-screen">
|
||||||
<div className="text-center">
|
<div className="text-center">
|
||||||
<h1 className={`text-2xl font-bold mb-4 ${messageStyles[type]}`}>{caption || 'Информация'}</h1>
|
<h1 className={`text-4xl font-bold mb-4 ${messageStyles[type]}`}>{caption || 'Информация'}</h1>
|
||||||
<p className="mb-6">
|
<p className="text-xl mb-6">
|
||||||
{message || 'Така ще получавате различни съобщения.'}
|
{message || 'Така ще получавате различни съобщения.'}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
Reference in New Issue
Block a user