-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates to Next.js 14 and migrate the backend to Supabase and Drizzle…
… ORM
- Loading branch information
1 parent
520c393
commit 6edbbc9
Showing
82 changed files
with
4,077 additions
and
1,616 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,7 @@ yarn-error.log* | |
|
||
# vercel | ||
.vercel | ||
|
||
# Supabase | ||
.branches | ||
.temp |
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,3 @@ | ||
{ | ||
"recommendations": ["denoland.vscode-deno"] | ||
} |
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,10 @@ | ||
{ | ||
"deno.enablePaths": [ | ||
"supabase/functions" | ||
], | ||
"deno.lint": true, | ||
"deno.unstable": true, | ||
"[typescript]": { | ||
"editor.defaultFormatter": "denoland.vscode-deno" | ||
} | ||
} |
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,19 @@ | ||
type User = { | ||
_id: string; | ||
name: string; | ||
username: string; | ||
email: string; | ||
image: string | null; | ||
bio: string | null; | ||
followers: string[] | null; | ||
following: string[] | null; | ||
verified: boolean | null; | ||
role: string | null; | ||
likes: string[] | null; | ||
createdAt: Date | null; | ||
updatedAt: Date | null; | ||
|
||
// For the frontend | ||
id: string | null | undefined; | ||
accessToken: string | null | undefined; | ||
}; |
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,26 @@ | ||
/** | ||
* FIXME: Custom type for JSONB column (Due to a bug which causes JSONB to be inserted as a string from the database) | ||
* NOTE: The below code is a HACK/Workaround, not to be used in production if the bug is fixed in the future | ||
* TODO: Remove this file and use the JSONB type directly from drizzle-orm/pg-core | ||
*/ | ||
|
||
import { customType } from 'drizzle-orm/pg-core'; | ||
|
||
const jsonb = customType<{ data: any }>({ | ||
dataType() { | ||
return 'jsonb'; | ||
}, | ||
toDriver(val) { | ||
return val as any; | ||
}, | ||
fromDriver(value) { | ||
if (typeof value === 'string') { | ||
try { | ||
return JSON.parse(value) as any; | ||
} catch {} | ||
} | ||
return value as any; | ||
}, | ||
}); | ||
|
||
export default jsonb; |
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 |
---|---|---|
@@ -1,73 +1,6 @@ | ||
import { connectMongoDB } from "@/libs/mongodb"; | ||
import User from "@/models/User"; | ||
import { authOptions } from "@/lib/authOptions"; | ||
import NextAuth from "next-auth/next"; | ||
import GoogleProvider from "next-auth/providers/google"; | ||
import SignToken from "@/app/api/auth/jwt"; | ||
import { NextAuthOptions } from "next-auth"; | ||
|
||
export const authOptions: NextAuthOptions = { | ||
session: { | ||
strategy: "jwt", | ||
maxAge: 7 * 24 * 60 * 60, // 7 days | ||
}, | ||
jwt: { | ||
maxAge: 7 * 24 * 60 * 60, | ||
}, | ||
|
||
providers: [ | ||
GoogleProvider({ | ||
clientId: process.env.GOOGLE_CLIENT_ID || "", | ||
clientSecret: process.env.GOOGLE_CLIENT_SECRET || "", | ||
}), | ||
], | ||
callbacks: { | ||
async jwt({ token, user, account }) { | ||
if (account?.provider === "google") { | ||
try { | ||
await connectMongoDB(); | ||
const userExists = await User.findOne({ email: user.email }); | ||
|
||
if (!userExists) { | ||
// Create the user document and add it to the token | ||
const newUser = await User.create({ | ||
name: user.name, | ||
username: user.email?.split("@")[0] || user.name, | ||
image: user?.image || "", | ||
email: user.email, | ||
}); | ||
token._id = newUser._id; // Include the MongoDB ObjectId in the token | ||
const data = { | ||
email: newUser.email, | ||
_id: newUser._id, | ||
}; | ||
const tokenString = await SignToken(data); | ||
token.accessToken = tokenString; | ||
} else { | ||
const data = { | ||
email: userExists.email, | ||
_id: userExists._id, | ||
}; | ||
const tokenString = await SignToken(data); | ||
token.accessToken = tokenString; | ||
token._id = userExists._id; | ||
} | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
} | ||
|
||
return token; | ||
}, | ||
|
||
async session({ session, token }) { | ||
session.user.id = token._id as string; | ||
session.user.accessToken = token.accessToken as string; | ||
session.user.expires = token.exp as number; | ||
return session; | ||
}, | ||
}, | ||
}; | ||
|
||
const handler = NextAuth(authOptions); | ||
|
||
export { handler as GET, handler as POST }; | ||
export { handler as GET, handler as POST }; |
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
Oops, something went wrong.