Skip to content

Commit 081af04

Browse files
add project files
1 parent a53cf01 commit 081af04

File tree

8 files changed

+111
-0
lines changed

8 files changed

+111
-0
lines changed

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/nextjs-dashboard.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auth.config.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { NextAuthConfig } from 'next-auth';
2+
3+
export const authConfig = {
4+
pages: {
5+
signIn: '/login',
6+
},
7+
callbacks: {
8+
authorized({ auth, request: { nextUrl } }) {
9+
const isLoggedIn = !!auth?.user;
10+
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard');
11+
if (isOnDashboard) {
12+
return isLoggedIn;
13+
// Redirect unauthenticated users to login page
14+
} else if (isLoggedIn) {
15+
return Response.redirect(new URL('/dashboard', nextUrl));
16+
}
17+
return true;
18+
},
19+
},
20+
providers: [], // Add providers with an empty array for now
21+
} satisfies NextAuthConfig;

auth.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import NextAuth from 'next-auth';
2+
import Credentials from 'next-auth/providers/credentials';
3+
import { authConfig } from './auth.config';
4+
import { z } from 'zod';
5+
import { sql } from '@vercel/postgres';
6+
import type { User } from '@/app/lib/definitions';
7+
import bcrypt from 'bcrypt';
8+
9+
async function getUser(email: string): Promise<User | undefined> {
10+
try {
11+
const user = await sql<User>`SELECT * FROM users WHERE email=${email}`;
12+
return user.rows[0];
13+
} catch (error) {
14+
console.error('Failed to fetch user:', error);
15+
throw new Error('Failed to fetch user.');
16+
}
17+
}
18+
19+
export const { auth, signIn, signOut } = NextAuth({
20+
...authConfig,
21+
providers: [
22+
Credentials({
23+
async authorize(credentials) {
24+
const parsedCredentials = z
25+
.object({ email: z.string().email(), password: z.string().min(6) })
26+
.safeParse(credentials);
27+
28+
if (parsedCredentials.success) {
29+
const { email, password } = parsedCredentials.data;
30+
const user = await getUser(email);
31+
if (!user) return null;
32+
const passwordsMatch = await bcrypt.compare(password, user.password);
33+
34+
if (passwordsMatch) return user;
35+
}
36+
37+
console.log('Invalid credentials');
38+
return null;
39+
}
40+
},
41+
),
42+
43+
],
44+
});

middleware.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import NextAuth from 'next-auth';
2+
import { authConfig } from './auth.config';
3+
4+
export default NextAuth(authConfig).auth;
5+
6+
export const config = {
7+
// https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher
8+
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
9+
};

0 commit comments

Comments
 (0)