Added manaual email login

This commit is contained in:
Khalim Conn-Kowlessar 2024-09-10 18:44:03 +01:00
parent 31a1bf77a8
commit fdada1af06
3 changed files with 52 additions and 5 deletions

View file

@ -1,6 +1,7 @@
import NextAuth, { NextAuthOptions } from "next-auth";
import GoogleProvider from "next-auth/providers/google";
import AzureADB2CProvider from "next-auth/providers/azure-ad-b2c";
import CredentialsProvider from "next-auth/providers/credentials";
import { db } from "@/app/db/db";
import { user as userTable, User } from "@/app/db/schema/users";
@ -42,10 +43,43 @@ export const AuthOptions: NextAuthOptions = {
authorization: {
params: {
scope: "openid profile offline_access",
prompt: "login", //
prompt: "login",
},
},
}),
CredentialsProvider({
name: "Email Login",
credentials: {
email: {
label: "Email",
type: "email",
},
},
async authorize(credentials, req) {
if (!credentials || !credentials.email) {
throw new Error("Email is required");
}
const { email } = credentials;
// Query the database to find the user by email
const dbUser = await db
.select()
.from(userTable)
.where(eq(userTable.email, email));
// If the email exists, return the user object (no password check)
if (dbUser.length === 1) {
return {
id: dbUser[0].id.toString(), // Convert bigint to string to avoid serialization issues
email: dbUser[0].email,
dbId: dbUser[0].id.toString(), // Ensure dbId is added and is a string
};
}
return null;
},
}),
],
pages: {
signIn: "/",

View file

@ -1,3 +1,3 @@
export default function Beta() {
return <div>This application is not ready for general usage</div>;
return <div>You do not have access to this application currently</div>;
}

View file

@ -2,16 +2,24 @@ import { getServerSession } from "next-auth/next";
import { AuthOptions } from "./api/auth/[...nextauth]/route";
import GoogleSignInButton from "./components/signin/GoogleSignInButton";
import MicrosoftSignInButton from "./components/signin/MicrosoftSignInButton";
import EmailSignInButton from "./components/signin/CredentialsButton";
import { redirect } from "next/navigation";
import Image from "next/image";
export default async function Home() {
export default async function Home({
searchParams,
}: {
searchParams: { error?: string };
}) {
const session = await getServerSession(AuthOptions);
if (session?.user) {
redirect("/home");
}
// Extract the error parameter from the searchParams object
const error = searchParams.error;
return (
<div className="flex min-h-screen">
{/* Left Half */}
@ -23,7 +31,7 @@ export default async function Home() {
{/* Right Half */}
<section className="w-1/2 flex items-center justify-center">
<div className="w-full max-w-lg p-8 rounded-lg flex flex-col items-center justify-center">
<div className="w-full max-w-lg p-8 rounded-lg flex flex-col items-center justify-center">
<Image
className="mb-8"
src="/HestiaLogoWhite.png"
@ -37,10 +45,15 @@ export default async function Home() {
<div className="text-brandmidblue text-lg mb-4">
Start managing your portfolios
</div>
<div className="mb-2 min-w-[19rem]">
{/* This width has been manually set to align the buttons but should be improved */}
<EmailSignInButton error={error} />
</div>
<div className="text-md"> Sign in with a Social Account</div>
<div className="mb-2">
<MicrosoftSignInButton />
</div>
<GoogleSignInButton />
</div>
</section>