implemented login

This commit is contained in:
2024-01-06 10:50:27 +01:00
parent 60a89b88ac
commit 52d4c35c2e
14 changed files with 257 additions and 35 deletions

View File

@@ -4,7 +4,7 @@ import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import clientPromise from './mongodb';
import { BillAttachment, Bill, BillingLocation } from './db-types';
import { BillAttachment, BillingLocation } from './db-types';
import { ObjectId } from 'mongodb';
export type State = {

12
app/lib/global.d.ts vendored
View File

@@ -1,8 +1,8 @@
import { MongoClient } from "mongodb";
declare global {
namespace globalThis {
/** global Mongo Client used in development */
var _mongoClientPromise: Promise<MongoClient>
}
}
// declare global {
// namespace globalThis {
// /** global Mongo Client used in development */
// var _mongoClientPromise: Promise<MongoClient>
// }
// }

21
app/lib/loginActions.ts Normal file
View File

@@ -0,0 +1,21 @@
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';
export async function authenticate(
prevState: string | undefined,
formData: FormData,
) {
try {
await signIn('credentials', formData);
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return 'Invalid credentials.';
default:
return 'Something went wrong.';
}
}
throw error;
}
}

View File

@@ -1,25 +0,0 @@
import { Client, QueryResult, QueryResultRow } from 'pg';
const client = new Client({
// connectionString: process.env.DATABASE_URL,
host: process.env.POSTGRES_HOST,
user: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB
});
client.connect();
/** an adapter function which simulates @vercel/postgres `sql` function */
export function sql<T extends QueryResultRow>(strings: TemplateStringsArray, ...values: any[]): Promise<QueryResult<T>> {
// string values need to be wrapped in single quotes
const fixedValues = values.map((value) => {
if (typeof value === 'string') {
return `'${value}'`;
}
return value;
});
const query = String.raw(strings, ...fixedValues);
return client.query<T>(query);
}

6
app/lib/types/User.ts Normal file
View File

@@ -0,0 +1,6 @@
export type User = {
id: string;
name: string;
email: string;
password: string;
};