From 5a6d8a8812905b0e11e02d2690809584775650b9 Mon Sep 17 00:00:00 2001 From: StephDietz Date: Wed, 20 Sep 2023 11:54:59 -0500 Subject: [PATCH] update login form --- .../app/api/auth/[...nextauth]/route.ts | 6 +- dashboard/15-final/app/login/page.tsx | 2 +- dashboard/15-final/app/ui/login-form.jsx | 148 ------------------ dashboard/15-final/app/ui/login-form.tsx | 99 ++++++++++++ 4 files changed, 105 insertions(+), 150 deletions(-) delete mode 100644 dashboard/15-final/app/ui/login-form.jsx create mode 100644 dashboard/15-final/app/ui/login-form.tsx diff --git a/dashboard/15-final/app/api/auth/[...nextauth]/route.ts b/dashboard/15-final/app/api/auth/[...nextauth]/route.ts index f15ffff..040cb31 100644 --- a/dashboard/15-final/app/api/auth/[...nextauth]/route.ts +++ b/dashboard/15-final/app/api/auth/[...nextauth]/route.ts @@ -2,6 +2,7 @@ import NextAuth from 'next-auth/next'; import CredentialsProvider from 'next-auth/providers/credentials'; import bcrypt from 'bcrypt'; import { fetchUser } from '../../../lib/data'; +import { User } from '@/app/lib/definitions'; export const authOptions = { providers: [ @@ -9,7 +10,10 @@ export const authOptions = { name: 'credentials', credentials: {}, - async authorize(credentials) { + async authorize(credentials: { + email: string; + password: string; + }): Promise { const { email, password } = credentials; try { diff --git a/dashboard/15-final/app/login/page.tsx b/dashboard/15-final/app/login/page.tsx index 8ab878a..baa4ad4 100644 --- a/dashboard/15-final/app/login/page.tsx +++ b/dashboard/15-final/app/login/page.tsx @@ -7,7 +7,7 @@ export default async function Page() { // Get user session token const session = await getServerSession(authOptions); if (session) redirect('/dashboard'); - // session = null || { user: { name, email, image } } + return (
diff --git a/dashboard/15-final/app/ui/login-form.jsx b/dashboard/15-final/app/ui/login-form.jsx deleted file mode 100644 index b3a3086..0000000 --- a/dashboard/15-final/app/ui/login-form.jsx +++ /dev/null @@ -1,148 +0,0 @@ -'use client'; - -import Link from 'next/link'; -import { useState } from 'react'; -import { signIn } from 'next-auth/react'; -import { useRouter } from 'next/navigation'; - -export default function LoginForm() { - const [email, setEmail] = useState(''); - const [password, setPassword] = useState(''); - const [error, setError] = useState(''); - - const router = useRouter(); - - const handleSubmit = async (e) => { - e.preventDefault(); - - try { - const res = await signIn('credentials', { - email, - password, - redirect: false, - }); - console.log(res); - if (res.error) { - setError('Invalid Credentials'); - return; - } - - router.replace('dashboard'); - } catch (error) { - console.log(error); - } - }; - - return ( -
-
-

Login

- -
- setEmail(e.target.value)} - type="text" - placeholder="Email" - /> - setPassword(e.target.value)} - type="password" - placeholder="Password" - /> - - {error && ( -
- {error} -
- )} - - - Don't have an account? Register - -
-
-
- ); -} - -// 'use client'; - -// import BackgroundBlur from '@/app/ui/background-blur'; -// import React, { useState } from 'react'; -// import Link from 'next/link'; -// import Image from 'next/image'; -// import { signIn } from 'next-auth/react'; - -// // This component contains basic logic for a React Form. -// // We'll be updating it in Chapter 8 - Adding Authentication. - -// export default function LoginForm() { -// const [email, setEmail] = useState(''); -// const [password, setPassword] = useState(''); - -// const handleSubmit = (e: React.FormEvent) => { -// e.preventDefault(); -// console.log(`Email: ${email}, Password: ${password}`); -// }; - -// return ( -//
-// - -//
-// -// Next.js Logo -// -//
-// -//
-//
-// -// setEmail(e.target.value)} -// /> -//
-//
-// -// setPassword(e.target.value)} -// /> -//
-//
-// -//
-//
-//
-//
-//
-// ); -// } diff --git a/dashboard/15-final/app/ui/login-form.tsx b/dashboard/15-final/app/ui/login-form.tsx new file mode 100644 index 0000000..337660b --- /dev/null +++ b/dashboard/15-final/app/ui/login-form.tsx @@ -0,0 +1,99 @@ +'use client'; + +import Link from 'next/link'; +import { signIn } from 'next-auth/react'; +import { useRouter } from 'next/navigation'; +import BackgroundBlur from '@/app/ui/background-blur'; +import React, { useState } from 'react'; +import Image from 'next/image'; + +// This component contains basic logic for a React Form. +// We'll be updating it in Chapter 8 - Adding Authentication. + +export default function LoginForm() { + const [email, setEmail] = useState(''); + const [password, setPassword] = useState(''); + + const [error, setError] = useState(''); + + const router = useRouter(); + + const handleSubmit = async (e: { preventDefault: () => void }) => { + e.preventDefault(); + + try { + const res = await signIn('credentials', { + email, + password, + redirect: false, + }); + + if (res?.error) { + setError('Invalid Credentials'); + return; + } + + router.replace('dashboard'); + } catch (error) { + console.log(error); + } + }; + return ( +
+ + +
+ + Next.js Logo + +
+
+
+ + setEmail(e.target.value)} + /> +
+
+ + setPassword(e.target.value)} + /> +
+
+ + {error && ( +
+ {error} +
+ )} +
+
+
+
+
+ ); +}