Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change email to username #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions my-babs-app/pages/api/auth/[...nextauth].ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ export default NextAuth({
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "text", placeholder: "example@gmail.com" },
username: { label: "Username", type: "text", placeholder: "example" },
password: { label: "Password", type: "password" },
},
async authorize(credentials) {
const { email, password } = credentials as { email: string; password: string };
const { username, password } = credentials as { username: string; password: string };

if (!email || !password) {
throw new Error("Email and password are required");
if (!username || !password) {
throw new Error("Username and password are required");
}

try {
const userExists = await prisma.user.findUnique({
where: {
email: email,
username: username,
},
});

Expand All @@ -38,7 +38,7 @@ export default NextAuth({

return {
id: userExists.id,
email: userExists.email,
username: userExists.username,
name: userExists.firstName + " " + userExists.lastName,
}

Expand All @@ -59,7 +59,7 @@ export default NextAuth({
async jwt({ token, user }) {
if (user) {
token.id = user.id;
token.email = user.email;
token.username = user.username;
token.name = user.name;
}
return token;
Expand All @@ -68,7 +68,7 @@ export default NextAuth({
async session({ session, token }) {
session.user = {
id: token.id,
email: token.email,
username: token.username,
name: token.name,
};
return session;
Expand Down
12 changes: 6 additions & 6 deletions my-babs-app/pages/api/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextApiRequest, NextApiResponse } from 'next';
interface UserReq {
firstName: string;
lastName: string;
email: string;
username: string;
password: string;
confirmPass: string;
pincode: string;
Expand All @@ -14,20 +14,20 @@ interface UserReq {
export default async function handler(req: NextApiRequest, res: NextApiResponse) {

if (req.method === 'POST') {
const { email, firstName, lastName, password, confirmPass, pincode } = await req.body as UserReq;
const { username, firstName, lastName, password, confirmPass, pincode } = await req.body as UserReq;

if (!firstName || !lastName || !email || !password || !pincode || !confirmPass) {
if (!firstName || !lastName || !username || !password || !pincode || !confirmPass) {
return res.status(400).json({ message: 'Please enter all fields!' })
}

if (password == confirmPass) {
try {
const userExists = await prisma.user.findUnique({
where: { email: email },
where: { username: username },
});

if (userExists) {
res.status(400).json({ message: 'This email has been used! Please log in with this email or sign up with another email' })
res.status(400).json({ message: 'This username has been used! Please log in with this username or sign up with another username' })
return
}

Expand All @@ -38,7 +38,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
data: {
firstName: firstName,
lastName: lastName,
email: email,
username: username,
password: hashedPassword,
pincode: pincode,
}
Expand Down
18 changes: 9 additions & 9 deletions my-babs-app/pages/api/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { prisma } from "@/libs/prisma";

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
const { amount, id, receiverEmail } = await req.body as { amount: number, id: number, receiverEmail: string }
const { amount, id, receiverUsername } = await req.body as { amount: number, id: number, receiverUsername: string }

if (amount <= 0) {
res.status(400).json({ message: 'Amount is required' })
Expand All @@ -15,20 +15,20 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
where: { id: id },
select: {
balance: true,
email: true,
username: true,
}
})

const receiver = await prisma.user.findUnique({
where: { email: String(receiverEmail) },
where: { username: String(receiverUsername) },
select: {
email: true,
username: true,
balance: true,
}
})

if (!receiver) {
res.status(400).json({ message: `Receiver does not exist ${String(receiverEmail)}` })
res.status(400).json({ message: `Receiver does not exist ${String(receiverUsername)}` })
return
}

Expand Down Expand Up @@ -56,14 +56,14 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
createdAt: new Date(),
accountBefore: Number(userCurrBalance),
accountAfter: Number(Number(userCurrBalance) - Number(amount)),
receiverEmail: String(receiverEmail),
receiverUsername: String(receiverUsername),
}
}
}
})

await prisma.user.update({
where: { email: String(receiverEmail) },
where: { username: String(receiverUsername) },
data: {
balance: {
increment: Number(amount)
Expand All @@ -75,7 +75,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
createdAt: new Date(),
accountBefore: Number(receiverCurrBalance),
accountAfter: Number(Number(receiverCurrBalance) + Number(amount)),
senderEmail: String(user?.email),
senderUsername: String(user?.username),
},
}
}
Expand All @@ -85,7 +85,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return
} catch (error) {
console.log(error)
res.status(500).json({ message: `Something went wrong ${amount} and ${id} and ${receiverEmail}` })
res.status(500).json({ message: `Something went wrong ${amount} and ${id} and ${receiverUsername}` })
return
}
} else {
Expand Down
14 changes: 7 additions & 7 deletions my-babs-app/pages/components/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import Link from 'next/link'
import Router from 'next/router'

interface FormProps {
email: string
username: string
password: string
}

const initialForm: FormProps = {
email: '',
username: '',
password: ''
}

Expand All @@ -30,7 +30,7 @@ export default function LogInForm({ withCreate = true, callbackUrl = '/dashboard
const res = await signIn('credentials', {
redirect: false,
callbackUrl: callbackUrl,
email: form.email,
username: form.username,
password: form.password
})

Expand All @@ -51,11 +51,11 @@ export default function LogInForm({ withCreate = true, callbackUrl = '/dashboard
<form onSubmit={handleSubmit} className="flex flex-col gap-[20px] items-center ">

<input
type="email"
name="email"
value={form.email}
type="username"
name="username"
value={form.username}
onChange={handleChange}
placeholder="Email"
placeholder="Username"
className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[24em] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-3 mt-2" />

<input
Expand Down
12 changes: 6 additions & 6 deletions my-babs-app/pages/components/SignUpForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Link from "next/link"
import Router from "next/router"

interface FormProps {
email: string,
username: string,
firstName: string,
lastName: string,
password: string,
Expand All @@ -15,7 +15,7 @@ interface FormProps {
}

const initialForm: FormProps = {
email: '',
username: '',
firstName: '',
lastName: '',
password: '',
Expand Down Expand Up @@ -65,11 +65,11 @@ export default function SignUpForm() {
className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[170px] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-2 mt-2" />
</div>
<input
type="email"
name="email"
value={form.email}
type="username"
name="username"
value={form.username}
onChange={handleChange}
placeholder="Email" className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[360px] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-2 mt-2" />
placeholder="Username" className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[360px] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-2 mt-2" />

<input
type="password"
Expand Down
8 changes: 4 additions & 4 deletions my-babs-app/pages/components/Transaction/Transaction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ interface Transaction {
amount: number
balanceBefore: number
balanceAfter: number
receiverEmail: string
senderEmail: string
receiverUsername: string
senderUsername: string
}


Expand Down Expand Up @@ -81,8 +81,8 @@ export default function Transaction({ handleClose, transactions }) {
date={item.createdAt.slice(0, 10)}
balanceBefore={item.accountBefore}
balanceAfter={item.accountAfter}
receiver={item.receiverEmail}
sender={item.senderEmail}
receiver={item.receiverUsername}
sender={item.senderUsername}
/>
</motion.div>
))}
Expand Down
12 changes: 6 additions & 6 deletions my-babs-app/pages/components/Transfer/TransferFund.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { setShowTransfer } from '../../redux/showTransferSlice'
interface TransferProps {
amount: number
id: number
receiverEmail: string
receiverUsername: string
}

const initialTransfer: TransferProps = {
amount: 0,
id: 0,
receiverEmail: ''
receiverUsername: ''
}

const dropIn = {
Expand Down Expand Up @@ -96,11 +96,11 @@ export default function TransferFund({ handleClose, id }) {
className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[20em] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-3 mt-2"
/>
<input
type="email"
value={form.receiverEmail}
name='receiverEmail'
type="username"
value={form.receiverUsername}
name='receiverUsername'
onChange={handleChange}
placeholder='Receiver Email'
placeholder='Receiver Username'
className="text-[#69C9D0] bg-[rgba(255,255,255,0.2)] w-[20em] border-[2px] border-[rgba(0,0,0,0)] focus:ring-[#69C9D0] focus:border-[#69C9D0] focus:outline-none text-sm rounded-lg block p-3 mt-2"
/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"firstName" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"email" TEXT NOT NULL,
"username" TEXT NOT NULL,
"password" TEXT NOT NULL,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
Expand All @@ -18,4 +18,4 @@ CREATE TABLE "Transactions" (
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

*/
-- AlterTable
ALTER TABLE "Transactions" ADD COLUMN "receiverEmail" TEXT,
ADD COLUMN "senderEmail" TEXT;
ALTER TABLE "Transactions" ADD COLUMN "receiverUsername" TEXT,
ADD COLUMN "senderUsername" TEXT;

-- AlterTable
ALTER TABLE "User" ALTER COLUMN "balance" SET DEFAULT 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ CREATE TABLE "transactions" (
"transactionType" TEXT NOT NULL,
"accountBefore" INTEGER NOT NULL,
"accountAfter" INTEGER NOT NULL,
"receiverEmail" TEXT,
"senderEmail" TEXT,
"receiverUsername" TEXT,
"senderUsername" TEXT,
"userId" INTEGER,

CONSTRAINT "transactions_pkey" PRIMARY KEY ("id")
Expand Down
22 changes: 11 additions & 11 deletions my-babs-app/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ model User {
id Int @id @default(autoincrement())
firstName String
lastName String
email String @unique
username String @unique
password String
pincode String
balance Int @default(0)
Expand All @@ -25,16 +25,16 @@ model User {
}

model Transaction {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
amount Int
transactionType String
accountBefore Int
accountAfter Int
receiverEmail String?
senderEmail String?
User User? @relation(fields: [userId], references: [id])
userId Int?
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
amount Int
transactionType String
accountBefore Int
accountAfter Int
receiverUsername String?
senderUsername String?
User User? @relation(fields: [userId], references: [id])
userId Int?

@@map(name: "transactions")
}