-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
congle-engineer
committed
May 1, 2024
1 parent
bebe628
commit c605478
Showing
10 changed files
with
207 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import AcmeLogo from '@/app/ui/acme-logo'; | ||
import LoginForm from '@/app/ui/login-form'; | ||
|
||
export default function LoginPage() { | ||
return ( | ||
<main className="flex items-center justify-center md:h-screen"> | ||
<div className="relative mx-auto flex w-full max-w-[400px] flex-col space-y-2.5 p-4 md:-mt-32"> | ||
<div className="flex h-20 w-full items-end rounded-lg bg-blue-500 p-3 md:h-36"> | ||
<div className="w-32 text-white md:w-36"> | ||
<AcmeLogo /> | ||
</div> | ||
</div> | ||
<LoginForm /> | ||
</div> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { NextAuthConfig } from 'next-auth'; | ||
|
||
export const authConfig = { | ||
pages: { | ||
signIn: '/login' | ||
}, | ||
callbacks: { | ||
authorized({ auth, request: { nextUrl } }) { | ||
const isLoggedIn = !!auth?.user; | ||
const isOnDashboard = nextUrl.pathname.startsWith('/dashboard'); | ||
if (isOnDashboard) { | ||
if (isLoggedIn) { | ||
return true; | ||
} | ||
return false; | ||
} else if (isLoggedIn) { | ||
return Response.redirect(new URL('/dashboard', nextUrl)); | ||
} | ||
return true; | ||
} | ||
}, | ||
providers: [] | ||
} satisfies NextAuthConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import NextAuth from 'next-auth'; | ||
import { authConfig } from './auth.config'; | ||
import Credentials from 'next-auth/providers/credentials' | ||
import { z } from 'zod'; | ||
import { sql } from '@vercel/postgres'; | ||
import type { User } from '@/app/lib/definitions'; | ||
import bcrypt from 'bcrypt'; | ||
|
||
async function getUser(email: string): Promise<User | undefined> { | ||
try { | ||
const user = await sql<User>`SELECT * FROM users WHERE email=${email}`; | ||
return user.rows[0]; | ||
} catch (error) { | ||
console.error('Failed to fetch user: ', error); | ||
throw new Error('Failed to fetch user.'); | ||
} | ||
} | ||
|
||
export const { auth, signIn, signOut } = NextAuth({ | ||
...authConfig, | ||
providers: [ | ||
Credentials({ | ||
async authorize(credentials) { | ||
const parsedCredentials = z | ||
.object({ email: z.string().email(), password: z.string().min(6) }) | ||
.safeParse(credentials); | ||
|
||
if (parsedCredentials.success) { | ||
const { email, password } = parsedCredentials.data; | ||
const user = await getUser(email); | ||
if (!user) { | ||
return null; | ||
} | ||
const passwordMatch = await bcrypt.compare(password, user.password); | ||
if (passwordMatch) { | ||
return user; | ||
} | ||
} | ||
|
||
console.log('Invalid credentials'); | ||
return null; | ||
} | ||
}) | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import NextAuth from 'next-auth'; | ||
import { authConfig } from './auth.config'; | ||
|
||
export default NextAuth(authConfig).auth; | ||
|
||
export const config = { | ||
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,19 @@ | |
"@jridgewell/gen-mapping" "^0.3.0" | ||
"@jridgewell/trace-mapping" "^0.3.9" | ||
|
||
"@auth/[email protected]": | ||
version "0.30.0" | ||
resolved "https://registry.npmjs.org/@auth/core/-/core-0.30.0.tgz" | ||
integrity sha512-8AE4m/nk+4EIiVCJwxZAsJeAQuzpEC8M8768mmKVn60CGDdupKQkVhxbRlm5Qh7eNRCoFFME+0DvtaX2aXrYaA== | ||
dependencies: | ||
"@panva/hkdf" "^1.1.1" | ||
"@types/cookie" "0.6.0" | ||
cookie "0.6.0" | ||
jose "^5.1.3" | ||
oauth4webapi "^2.4.0" | ||
preact "10.11.3" | ||
preact-render-to-string "5.2.3" | ||
|
||
"@babel/code-frame@^7.0.0", "@babel/code-frame@^7.22.13": | ||
version "7.22.13" | ||
resolved "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.22.13.tgz" | ||
|
@@ -392,6 +405,11 @@ | |
"@nodelib/fs.scandir" "2.1.5" | ||
fastq "^1.6.0" | ||
|
||
"@panva/hkdf@^1.1.1": | ||
version "1.1.1" | ||
resolved "https://registry.npmjs.org/@panva/hkdf/-/hkdf-1.1.1.tgz" | ||
integrity sha512-dhPeilub1NuIG0X5Kvhh9lH4iW3ZsHlnzwgwbOlgwQ2wG1IqFzsgHqmKPk3WzsdWAeaxKJxgM0+W433RmN45GA== | ||
|
||
"@pkgr/utils@^2.3.1": | ||
version "2.4.2" | ||
resolved "https://registry.npmjs.org/@pkgr/utils/-/utils-2.4.2.tgz" | ||
|
@@ -430,6 +448,11 @@ | |
dependencies: | ||
"@types/node" "*" | ||
|
||
"@types/[email protected]": | ||
version "0.6.0" | ||
resolved "https://registry.npmjs.org/@types/cookie/-/cookie-0.6.0.tgz" | ||
integrity sha512-4Kh9a6B2bQciAhf7FSuMRRkUWecJgJu9nPnx3yzpsfXX/c50REIqpHY4C82bXP90qrLtXtkDxTZosYO3UpOwlA== | ||
|
||
"@types/json-schema@^7.0.12", "@types/json-schema@^7.0.9": | ||
version "7.0.14" | ||
resolved "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.14.tgz" | ||
|
@@ -1138,6 +1161,11 @@ convert-source-map@^2.0.0: | |
resolved "https://registry.npmjs.org/convert-source-map/-/convert-source-map-2.0.0.tgz" | ||
integrity sha512-Kvp459HrV2FEJ1CAsi1Ku+MY3kasH19TFykTz2xWmMeq6bk2NU3XXvfJ+Q61m0xktWwt+1HSYf3JZsTms3aRJg== | ||
|
||
[email protected]: | ||
version "0.6.0" | ||
resolved "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz" | ||
integrity sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw== | ||
|
||
cross-spawn@^7.0.2, cross-spawn@^7.0.3: | ||
version "7.0.3" | ||
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz" | ||
|
@@ -2423,6 +2451,11 @@ jju@~1.4.0: | |
resolved "https://registry.npmjs.org/jju/-/jju-1.4.0.tgz" | ||
integrity sha512-8wb9Yw966OSxApiCt0K3yNJL8pnNeIv+OEq2YMidz4FKP6nonSRoOXc80iXY4JaN2FC11B9qsNmDsm+ZOfMROA== | ||
|
||
jose@^5.1.3: | ||
version "5.2.4" | ||
resolved "https://registry.npmjs.org/jose/-/jose-5.2.4.tgz" | ||
integrity sha512-6ScbIk2WWCeXkmzF6bRPmEuaqy1m8SbsRFMa/FLrSCkGIhj8OLVG/IH+XHVmNMx/KUo8cVWEE6oKR4dJ+S0Rkg== | ||
|
||
"js-tokens@^3.0.0 || ^4.0.0", js-tokens@^4.0.0: | ||
version "4.0.0" | ||
resolved "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz" | ||
|
@@ -2680,7 +2713,14 @@ natural-compare@^1.4.0: | |
resolved "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz" | ||
integrity sha512-OWND8ei3VtNC9h7V60qff3SVobHr996CTwgxubgyQYEpg290h9J0buyECNNJexkFm5sOajh5G116RYA1c8ZMSw== | ||
|
||
next@^14.0.2: | ||
next-auth@^5.0.0-beta.17: | ||
version "5.0.0-beta.17" | ||
resolved "https://registry.npmjs.org/next-auth/-/next-auth-5.0.0-beta.17.tgz" | ||
integrity sha512-XA/7JtAjOgDfAeotJPFUsFZGGItZwzZrxLt9Gc9fE7EchLk6zydZfuZ22Vvwixs3IilkN644D5IoD5tEOAFGCQ== | ||
dependencies: | ||
"@auth/core" "0.30.0" | ||
|
||
next@^14, next@^14.0.2: | ||
version "14.0.2" | ||
resolved "https://registry.npmjs.org/next/-/next-14.0.2.tgz" | ||
integrity sha512-jsAU2CkYS40GaQYOiLl9m93RTv2DA/tTJ0NRlmZIBIL87YwQ/xR8k796z7IqgM3jydI8G25dXvyYMC9VDIevIg== | ||
|
@@ -2776,6 +2816,11 @@ npmlog@^5.0.1: | |
gauge "^3.0.0" | ||
set-blocking "^2.0.0" | ||
|
||
oauth4webapi@^2.4.0: | ||
version "2.10.4" | ||
resolved "https://registry.npmjs.org/oauth4webapi/-/oauth4webapi-2.10.4.tgz" | ||
integrity sha512-DSoj8QoChzOCQlJkRmYxAJCIpnXFW32R0Uq7avyghIeB6iJq0XAblOD7pcq3mx4WEBDwMuKr0Y1qveCBleG2Xw== | ||
|
||
object-assign@^4.0.1, object-assign@^4.1.1: | ||
version "4.1.1" | ||
resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz" | ||
|
@@ -3095,6 +3140,18 @@ postgres-interval@^1.1.0: | |
dependencies: | ||
xtend "^4.0.0" | ||
|
||
[email protected]: | ||
version "5.2.3" | ||
resolved "https://registry.npmjs.org/preact-render-to-string/-/preact-render-to-string-5.2.3.tgz" | ||
integrity sha512-aPDxUn5o3GhWdtJtW0svRC2SS/l8D9MAgo2+AWml+BhDImb27ALf04Q2d+AHqUUOc6RdSXFIBVa2gxzgMKgtZA== | ||
dependencies: | ||
pretty-format "^3.8.0" | ||
|
||
preact@>=10, [email protected]: | ||
version "10.11.3" | ||
resolved "https://registry.npmjs.org/preact/-/preact-10.11.3.tgz" | ||
integrity sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg== | ||
|
||
prelude-ls@^1.2.1: | ||
version "1.2.1" | ||
resolved "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz" | ||
|
@@ -3118,6 +3175,11 @@ prettier@^3.0, prettier@^3.0.3, "prettier@>= 1.16.0", "prettier@>=3.0.0 <4": | |
resolved "https://registry.npmjs.org/prettier/-/prettier-3.0.3.tgz" | ||
integrity sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg== | ||
|
||
pretty-format@^3.8.0: | ||
version "3.8.0" | ||
resolved "https://registry.npmjs.org/pretty-format/-/pretty-format-3.8.0.tgz" | ||
integrity sha512-WuxUnVtlWL1OfZFQFuqvnvs6MiAGk9UNsBostyBOB0Is9wb5uRESevA6rnl/rkksXaGX3GzZhPup5d6Vp1nFew== | ||
|
||
prop-types@^15.8.1: | ||
version "15.8.1" | ||
resolved "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz" | ||
|