Skip to content

Commit

Permalink
modified next-auth interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Prashant-flick committed Sep 30, 2024
1 parent 28b0564 commit 1423d5a
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 91 deletions.
4 changes: 2 additions & 2 deletions apps/merchant-app/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Button } from "@repo/ui/button";
import { getServerSession } from "next-auth";
import { getServerSession, Session } from "next-auth";

export default async function Home() {
const session = await getServerSession();
const session: Session | null = await getServerSession();

const handleOnClick = () => {
console.log("hello");
Expand Down
6 changes: 4 additions & 2 deletions apps/merchant-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
{
"name": "next"
}
]
],
"typeRoots": ["../../packages/types"]
},
"include": [
"next-env.d.ts",
"next.config.mjs",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
".next/types/**/*.ts",
"../../packages/types"
],
"exclude": ["node_modules"]
}
14 changes: 3 additions & 11 deletions apps/user-app/app/(dashboard)/p2p/page.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import prisma from "@repo/db/client";
import { BalanceCard } from "../../../components/BalanceCard";
import { getServerSession } from "next-auth";
import { getServerSession, Session} from "next-auth";
import { authOptions } from "../../lib/auth";
import { SendCard } from "../../../components/SendCard";
import { P2pTransaction } from "../../../components/P2pTransaction"

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

async function getBalance() {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
const balance = await prisma.balance.findFirst({
where: {
Expand All @@ -28,7 +20,7 @@ async function getBalance() {
}

async function getP2pTransaction() {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
const txns = await prisma.p2pTransfer.findMany({
where: {
Expand Down
12 changes: 2 additions & 10 deletions apps/user-app/app/(dashboard)/transactions/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import db from "@repo/db/client"
import { getServerSession } from "next-auth";
import { getServerSession, Session} from "next-auth";
import { authOptions } from "../../lib/auth";
import { AllTrasactions } from "../../../components/AllTransactions";

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

async function getTransaction() {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
const txns = await db.onRampTransaction.findMany({ where: { userId: Number(session?.user?.id) } });
const txns2 = await db.p2pTransfer.findMany({
Expand Down
14 changes: 3 additions & 11 deletions apps/user-app/app/(dashboard)/transfer/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,11 @@ import prisma from "@repo/db/client";
import { AddMoney } from "../../../components/AddMoneyCard";
import { BalanceCard } from "../../../components/BalanceCard";
import { OnRampTransactions } from "../../../components/OnRampTransaction";
import { getServerSession } from "next-auth";
import { getServerSession, Session } from "next-auth";
import { authOptions } from "../../lib/auth";

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

async function getBalance() {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
const balance = await prisma.balance.findFirst({
where: {
Expand All @@ -28,7 +20,7 @@ async function getBalance() {
}

async function getOnRampTransactions() {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
const txns = await prisma.onRampTransaction.findMany({
where: {
Expand Down
12 changes: 2 additions & 10 deletions apps/user-app/app/api/user/route.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import { NextResponse } from "next/server";
import { getServerSession } from "next-auth";
import { getServerSession, Session } from "next-auth";
import { authOptions } from "../../lib/auth";

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

export const GET = async () => {
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user
if (user) {
return NextResponse.json({
Expand Down
12 changes: 2 additions & 10 deletions apps/user-app/app/lib/actions/createOnRampTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
"use server"
import client from "@repo/db/client"
import { getServerSession } from "next-auth"
import { getServerSession, Session } from "next-auth"
import { authOptions } from "../auth"

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

export async function createOnRampTransactions(provider: string, amount: string, redirectUrl: string){
const session: newSession | null = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
const user = session?.user

if(!user || !user?.id){
Expand Down
12 changes: 2 additions & 10 deletions apps/user-app/app/lib/actions/p2pTransfer.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
"use server"
import { getServerSession } from "next-auth"
import { getServerSession, Session } from "next-auth"
import { authOptions } from "../auth"
import prisma from "@repo/db/client"

interface newSession {
user?: {
id?: string | null,
name?: string | null,
email?: string | null,
}
}

export async function p2pTransfer(to: string, amount: number){
const session: newSession | null = await getServerSession(authOptions)
const session: Session | null = await getServerSession(authOptions)
const user = session?.user
const from = user?.id

Expand Down
37 changes: 17 additions & 20 deletions apps/user-app/app/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import client from "@repo/db/client"
import CredentialsProvider from "next-auth/providers/credentials"
import bcrypt from "bcrypt"
import GoogleProvider from "next-auth/providers/google";
import { NextAuthOptions, User } from "next-auth";
import { Account, NextAuthOptions, User, Session } from "next-auth";
import { JWT } from "next-auth/jwt";
import { AdapterUser } from "next-auth/adapters";

Expand Down Expand Up @@ -33,7 +33,6 @@ export const authOptions : NextAuthOptions = {
const passwordValidation = await bcrypt.compare(credentials.password, existingUser.password)
if(passwordValidation){
return {
verifiedPassword: true,
user: existingUser
}
}else{
Expand Down Expand Up @@ -69,7 +68,6 @@ export const authOptions : NextAuthOptions = {

return {
user: res,
verifiedPassword: true,
}
}else{
throw new Error("user doesn't exist")
Expand All @@ -78,40 +76,44 @@ export const authOptions : NextAuthOptions = {
}
}),
GoogleProvider({
// eslint-disable-next-line turbo/no-undeclared-env-vars
clientId: process.env.GOOGLE_CLIENT_ID || "",
// eslint-disable-next-line turbo/no-undeclared-env-vars
clientSecret: process.env.GOOGLE_CLIENT_SECRET || ""
})
],
callbacks: {
async session({token, session}: any){
session.user = token.user.user
async session({token, session}: {token: JWT, session: Session}): Promise<Session>{
session.user.name = token.name
session.user.email = token.email
session.user.id = token.sub
return session
},
async jwt({token, user}: {token: JWT, user: User | AdapterUser}){
if (user) {
token.user = user;
token.name = user.name;
token.email = user.email
token.sub = user.id
}
return token;
},
async redirect({ baseUrl }: any) {
async redirect({ baseUrl }: {baseUrl: string}) {
return baseUrl
},
async signIn({user,account}: any): Promise<any>{
async signIn({
user,account
}: { user: User | AdapterUser, account: Account | null }): Promise<string | boolean>{
if(account?.provider === "google"){
const existingUser = await client.user.findFirst({
where: {
email : user.email
email : user.email as string
}
})
if(existingUser){
return true;
}else{
await client.user.create({
data: {
email: user.email,
name: user.name,
email: user.email as string,
name: user.name as string,
password: "",
number: "",
auth_type: "Google"
Expand All @@ -120,16 +122,11 @@ export const authOptions : NextAuthOptions = {
return true;
}
}else if(account?.provider === "email"){

if(user.verifiedPassword){
return true;
}else{
return false;
}
return true;
}
return false;
}
},
// eslint-disable-next-line turbo/no-undeclared-env-vars
secret: process.env.NEXTAUTH_SECRET || "secret",
pages: {
signIn: "/signin",
Expand Down
4 changes: 2 additions & 2 deletions apps/user-app/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getServerSession } from "next-auth";
import { getServerSession, Session } from "next-auth";
import { redirect } from 'next/navigation'
import { authOptions } from "./lib/auth";

export default async function Page() {
const session = await getServerSession(authOptions);
const session: Session | null = await getServerSession(authOptions);
if (session?.user) {
redirect('/dashboard')
} else {
Expand Down
8 changes: 5 additions & 3 deletions apps/user-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
{
"name": "next"
}
]
],
"typeRoots": ["../../packages/types"],
},
"include": [
"next-env.d.ts",
"next.config.mjs",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts"
".next/types/**/*.ts",
"../../packages/types"
],
"exclude": ["node_modules"]
"exclude": ["node_modules"],
}
10 changes: 10 additions & 0 deletions packages/types/next-auth.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import NextAuth, {DefaultSession} from "next-auth"

declare module "next-auth" {

interface Session {
user: {
id: string | undefined
} & DefaultSession["user"]
}
}

0 comments on commit 1423d5a

Please sign in to comment.