-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add shared package for reused pages
- Loading branch information
Showing
30 changed files
with
979 additions
and
364 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
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
159 changes: 1 addition & 158 deletions
159
apps/social/src/app/(authentication)/login/google/callback/route.ts
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,158 +1 @@ | ||
import { nanoid } from "nanoid"; | ||
import { generateId } from "lucia"; | ||
import { cookies } from "next/headers"; | ||
import { db, and, eq } from "@umamin/db"; | ||
import { OAuth2RequestError } from "arctic"; | ||
import { | ||
user as userSchema, | ||
account as accountSchema, | ||
} from "@umamin/db/schema/user"; | ||
|
||
import { getSession, google, lucia } from "@/lib/auth"; | ||
|
||
export async function GET(request: Request): Promise<Response> { | ||
const url = new URL(request.url); | ||
const code = url.searchParams.get("code"); | ||
const state = url.searchParams.get("state"); | ||
|
||
const storedState = cookies().get("google_oauth_state")?.value ?? null; | ||
const storedCodeVerifier = cookies().get("code_verifier")?.value ?? null; | ||
|
||
if ( | ||
!code || | ||
!state || | ||
!storedState || | ||
!storedCodeVerifier || | ||
state !== storedState | ||
) { | ||
return new Response(null, { | ||
status: 400, | ||
}); | ||
} | ||
|
||
try { | ||
const tokens = await google.validateAuthorizationCode( | ||
code, | ||
storedCodeVerifier, | ||
); | ||
|
||
const googleUserResponse = await fetch( | ||
"https://openidconnect.googleapis.com/v1/userinfo", | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${tokens.accessToken}`, | ||
}, | ||
}, | ||
); | ||
|
||
const googleUser: GoogleUser = await googleUserResponse.json(); | ||
|
||
const { user } = await getSession(); | ||
|
||
const existingUser = await db.query.account.findFirst({ | ||
where: and( | ||
eq(accountSchema.providerId, "google"), | ||
eq(accountSchema.providerUserId, googleUser.sub), | ||
), | ||
}); | ||
|
||
if (user && existingUser) { | ||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: "/settings?error=already_linked", | ||
}, | ||
}); | ||
} else if (user) { | ||
await db | ||
.update(userSchema) | ||
.set({ | ||
imageUrl: googleUser.picture, | ||
}) | ||
.where(eq(userSchema.id, user.id)); | ||
|
||
await db.insert(accountSchema).values({ | ||
providerId: "google", | ||
providerUserId: googleUser.sub, | ||
userId: user.id, | ||
picture: googleUser.picture, | ||
email: googleUser.email, | ||
}); | ||
|
||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: "/settings", | ||
}, | ||
}); | ||
} | ||
|
||
if (existingUser) { | ||
const session = await lucia.createSession(existingUser.userId, {}); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
|
||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes, | ||
); | ||
|
||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: "/login", | ||
}, | ||
}); | ||
} | ||
|
||
const usernameId = generateId(5); | ||
const userId = nanoid(); | ||
|
||
await db.insert(userSchema).values({ | ||
id: userId, | ||
imageUrl: googleUser.picture, | ||
username: `umamin_${usernameId}`, | ||
}); | ||
|
||
await db.insert(accountSchema).values({ | ||
providerId: "google", | ||
providerUserId: googleUser.sub, | ||
userId, | ||
picture: googleUser.picture, | ||
email: googleUser.email, | ||
}); | ||
|
||
const session = await lucia.createSession(userId, {}); | ||
const sessionCookie = lucia.createSessionCookie(session.id); | ||
|
||
cookies().set( | ||
sessionCookie.name, | ||
sessionCookie.value, | ||
sessionCookie.attributes, | ||
); | ||
|
||
return new Response(null, { | ||
status: 302, | ||
headers: { | ||
Location: "/login", | ||
}, | ||
}); | ||
} catch (err: any) { | ||
console.log(err); | ||
if (err instanceof OAuth2RequestError) { | ||
return new Response(null, { | ||
status: 400, | ||
}); | ||
} | ||
|
||
return new Response(null, { | ||
status: 500, | ||
}); | ||
} | ||
} | ||
|
||
interface GoogleUser { | ||
sub: string; | ||
picture: string; | ||
email: string; | ||
} | ||
export { GET } from "@umamin/shared/routes/google/callback/route"; |
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,31 +1 @@ | ||
import { generateState, generateCodeVerifier } from "arctic"; | ||
import { cookies } from "next/headers"; | ||
import { google } from "@/lib/auth"; | ||
|
||
export async function GET(): Promise<Response> { | ||
const state = generateState(); | ||
const codeVerifier = generateCodeVerifier(); | ||
const url = await google.createAuthorizationURL(state, codeVerifier, { | ||
scopes: ["profile", "email"], | ||
}); | ||
|
||
url.searchParams.set("access_type", "offline"); | ||
|
||
cookies().set("google_oauth_state", state, { | ||
path: "/", | ||
secure: process.env.NODE_ENV === "production", | ||
httpOnly: true, | ||
maxAge: 60 * 10, | ||
sameSite: "lax", | ||
}); | ||
|
||
cookies().set("code_verifier", codeVerifier, { | ||
path: "/", | ||
secure: process.env.NODE_ENV === "production", | ||
httpOnly: true, | ||
maxAge: 60 * 10, | ||
sameSite: "lax", | ||
}); | ||
|
||
return Response.redirect(url); | ||
} | ||
export { GET } from "@umamin/shared/routes/google/route"; |
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,28 +1 @@ | ||
import { Skeleton } from "@umamin/ui/components/skeleton"; | ||
|
||
export default function Loading() { | ||
return ( | ||
<div className="max-w-lg md:max-w-md container mt-36 [&>div]:gap-3 [&>div]:flex [&>div]:flex-col flex gap-8 flex-col"> | ||
<div> | ||
<Skeleton className="w-2/5 h-[25px] rounded-md" /> | ||
<Skeleton className="w-1/2 h-[10px] rounded-md" /> | ||
</div> | ||
|
||
<div> | ||
<Skeleton className="w-1/5 h-[10px] rounded-md" /> | ||
<Skeleton className="w-full h-[30px] rounded-md" /> | ||
</div> | ||
|
||
<div> | ||
<Skeleton className="w-1/5 h-[10px] rounded-md" /> | ||
<Skeleton className="w-full h-[30px] rounded-md" /> | ||
</div> | ||
|
||
<div> | ||
<Skeleton className="w-full h-[30px] rounded-md" /> | ||
<Skeleton className="w-full h-[30px] rounded-md" /> | ||
<Skeleton className="mx-auto w-1/2 h-[10px] rounded-md" /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
export { default } from "@umamin/shared/app/login/loading"; |
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,44 +1 @@ | ||
import { Skeleton } from "@umamin/ui/components/skeleton"; | ||
|
||
export default function Loading() { | ||
return ( | ||
<div className="container max-w-xl lg:mt-36 mt-28 mx-auto "> | ||
<div className="flex flex-col gap-4"> | ||
<div className="flex gap-3"> | ||
<Skeleton className="size-16 md:size-20 rounded-full" /> | ||
|
||
<div className="flex flex-col gap-2"> | ||
<Skeleton className="h-[20px] w-[80px] rounded-md" /> | ||
<Skeleton className="h-[15px] w-[50px] rounded-md" /> | ||
</div> | ||
</div> | ||
|
||
<div className="flex flex-col gap-2"> | ||
<div className="flex gap-1 items-center"> | ||
<Skeleton className="size-[15px] rounded-full" /> | ||
<Skeleton className="h-[10px] w-[130px] rounded-md" /> | ||
</div> | ||
|
||
<div className="flex gap-1 items-center"> | ||
<Skeleton className="size-[15px] rounded-full" /> | ||
<Skeleton className="h-[10px] w-[130px] rounded-md" /> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className="space-y-5 mt-8"> | ||
<div> | ||
<div className="flex justify-around"> | ||
<Skeleton className="h-[15px] w-[90px] rounded-md" /> | ||
<Skeleton className="h-[15px] w-[90px] rounded-md" /> | ||
</div> | ||
|
||
<Skeleton className="w-full h-[2px] rounded-md mt-2" /> | ||
</div> | ||
|
||
<Skeleton className="w-full h-[200px] rounded-md" /> | ||
<Skeleton className="w-full h-[200px] rounded-md" /> | ||
</div> | ||
</div> | ||
); | ||
} | ||
export { default } from "@umamin/shared/app/register/loading"; |
Oops, something went wrong.