From b0e474677e1cff388002d416d3ef6f6bfe35efda Mon Sep 17 00:00:00 2001 From: Tyler Myracle Date: Fri, 12 Jan 2024 12:00:36 -0600 Subject: [PATCH] minimal register and login --- apps/client/pages/api/auth/[...nextauth].ts | 34 +++--- apps/client/pages/login.tsx | 91 +++++++++------- apps/client/pages/register.tsx | 100 ++++++++++++++++-- apps/client/styles.css | 17 +++ apps/client/tailwind.config.js | 4 + .../server/src/app/routes/auth-user.router.ts | 7 +- .../src/auth-user/auth-user.service.ts | 2 +- prisma/schema.prisma | 2 +- 8 files changed, 187 insertions(+), 70 deletions(-) diff --git a/apps/client/pages/api/auth/[...nextauth].ts b/apps/client/pages/api/auth/[...nextauth].ts index ce370382..91bdde6c 100644 --- a/apps/client/pages/api/auth/[...nextauth].ts +++ b/apps/client/pages/api/auth/[...nextauth].ts @@ -1,7 +1,6 @@ import NextAuth, { type SessionStrategy } from 'next-auth' import CredentialsProvider from 'next-auth/providers/credentials' import { z } from 'zod' -import type { SharedType } from '@maybe-finance/shared' import { PrismaClient } from '@prisma/client' import { PrismaAdapter } from '@auth/prisma-adapter' import axios from 'axios' @@ -36,19 +35,20 @@ export const authOptions = { password: { label: 'Password', type: 'password' }, }, async authorize(credentials) { - console.log('inside the authorize method') const parsedCredentials = z .object({ + name: z.string().optional(), email: z.string().email(), password: z.string().min(6), - provider: z.string().optional(), }) .safeParse(credentials) + console.log("here's the credentials", parsedCredentials) + if (parsedCredentials.success) { - console.log("Credentials are valid, let's authorize") - const { email, password } = parsedCredentials.data - console.log('Here are the params', email, password) + const { name, email, password } = parsedCredentials.data + + console.log('Hitting endpoint to get user', email) const { data } = await axios.get(`/auth-users`, { params: { email: email }, headers: { 'Content-Type': 'application/json' }, @@ -56,19 +56,18 @@ export const authOptions = { const user = data.data['json'] - console.log('This is User', user) + console.log('here is the user', user) - if (!user.id) { - console.log('User does not exist, creating new user') + if (!user) { + console.log('User does not exist, creating user') const hashedPassword = await bcrypt.hash(password, 10) - const { data: newUser } = await axios.post( - '/auth-users', - { - email, - password: hashedPassword, - } - ) - console.log('Created new user', newUser) + console.log('Hitting endpoint to create user', name, email, hashedPassword) + const { data } = await axios.post('/auth-users', { + name, + email, + password: hashedPassword, + }) + const newUser = data.data['json'] if (newUser) return newUser throw new Error('Could not create user') } @@ -77,7 +76,6 @@ export const authOptions = { if (passwordsMatch) return user } - console.log('Invalid credentials') return null }, }), diff --git a/apps/client/pages/login.tsx b/apps/client/pages/login.tsx index d6810c1f..4c5b1a9d 100644 --- a/apps/client/pages/login.tsx +++ b/apps/client/pages/login.tsx @@ -1,12 +1,11 @@ -import type { ReactElement } from 'react' -import { useState } from 'react' +import { useState, type ReactElement } from 'react' import { FullPageLayout } from '@maybe-finance/client/features' import { Input, InputPassword, Button } from '@maybe-finance/design-system' -import { AiOutlineLoading3Quarters as LoadingIcon } from 'react-icons/ai' import { signIn, useSession } from 'next-auth/react' import { useRouter } from 'next/router' import { useEffect } from 'react' import Script from 'next/script' +import Link from 'next/link' export default function LoginPage() { const [email, setEmail] = useState('') @@ -39,43 +38,59 @@ export default function LoginPage() { strategy="lazyOnload" />
- Maybe Finance Logo -
- setEmail(e.currentTarget.value)} - /> +
+
+ Maybe Finance Logo + + setEmail(e.currentTarget.value)} + /> - { - const passwordValid = checks.filter((c) => !c.isValid).length === 0 - setIsValid(passwordValid) - }} - onChange={(e: React.ChangeEvent) => - setPassword(e.target.value) - } - /> + { + const passwordValid = + checks.filter((c) => !c.isValid).length === 0 + setIsValid(passwordValid) + }} + onChange={(e: React.ChangeEvent) => + setPassword(e.target.value) + } + /> - - + +
+
+ Don't have an account?{' '} + + Sign up + +
+
+ +
+
) diff --git a/apps/client/pages/register.tsx b/apps/client/pages/register.tsx index ac1e3f1d..af6b70e9 100644 --- a/apps/client/pages/register.tsx +++ b/apps/client/pages/register.tsx @@ -1,11 +1,18 @@ -import type { ReactElement } from 'react' -import { Input, InputPassword } from '@maybe-finance/design-system' +import { useState, type ReactElement } from 'react' +import { Input, InputPassword, Button } from '@maybe-finance/design-system' import { FullPageLayout } from '@maybe-finance/client/features' -import { useSession } from 'next-auth/react' +import { signIn, useSession } from 'next-auth/react' import { useRouter } from 'next/router' import { useEffect } from 'react' +import Script from 'next/script' +import Link from 'next/link' export default function RegisterPage() { + const [name, setName] = useState('') + const [email, setEmail] = useState('') + const [password, setPassword] = useState('') + const [isValid, setIsValid] = useState(false) + const { data: session } = useSession() const router = useRouter() @@ -13,13 +20,90 @@ export default function RegisterPage() { if (session) router.push('/') }, [session, router]) + const onSubmit = async (e: React.FormEvent) => { + e.preventDefault() + + setName('') + setEmail('') + setPassword('') + + await signIn('credentials', { + email, + password, + name, + redirect: false, + }) + } + // _app.tsx will automatically redirect if not authenticated return ( -
-
THIS IS THE LOGIN PAGE
- - -
+ <> +