-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from omsimos/partners
resolve merge conflict in lockfile
- Loading branch information
Showing
15 changed files
with
663 additions
and
19 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
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,20 @@ | ||
import Link from "next/link"; | ||
import { Badge } from "@umamin/ui/components/badge"; | ||
import { SignOutButton } from "./sign-out-btn"; | ||
|
||
export async function Navbar() { | ||
return ( | ||
<nav className="fixed left-0 right-0 top-0 z-50 w-full bg-background bg-opacity-40 bg-clip-padding py-5 backdrop-blur-xl backdrop-filter lg:z-40 container max-w-screen-xl flex justify-between items-center"> | ||
<div className="space-x-2 flex items-center"> | ||
<Link href="/" aria-label="logo"> | ||
<span className="font-semibold text-foreground">umamin</span> | ||
<span className="text-muted-foreground font-medium">.link</span> | ||
</Link> | ||
|
||
<Badge variant="outline">partners</Badge> | ||
</div> | ||
|
||
<SignOutButton /> | ||
</nav> | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
apps/partners/src/app/dashboard/components/sign-out-btn.tsx
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,21 @@ | ||
"use client"; | ||
|
||
import { Loader2 } from "lucide-react"; | ||
import { useFormStatus } from "react-dom"; | ||
import { Button } from "@umamin/ui/components/button"; | ||
|
||
export function SignOutButton() { | ||
const { pending } = useFormStatus(); | ||
|
||
return ( | ||
<Button | ||
data-testid="logout-btn" | ||
type="submit" | ||
disabled={pending} | ||
variant="outline" | ||
> | ||
{pending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />} | ||
Sign Out | ||
</Button> | ||
); | ||
} |
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,14 @@ | ||
import { Navbar } from "./components/navbar"; | ||
|
||
export default function Layout({ | ||
children, | ||
}: Readonly<{ | ||
children: React.ReactNode; | ||
}>) { | ||
return ( | ||
<main> | ||
<Navbar /> | ||
{children} | ||
</main> | ||
); | ||
} |
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 @@ | ||
import { getSession } from "@/lib/auth"; | ||
|
||
export default async function Dashboard() { | ||
const { user } = await getSession(); | ||
return ( | ||
<div className="max-w-screen-xl mx-auto mt-32 container"> | ||
<h1 className="text-4xl">Hello, {user?.displayName || user?.username}</h1> | ||
</div> | ||
); | ||
} |
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,43 @@ | ||
"use client"; | ||
|
||
import { login } from "@/lib/actions"; | ||
import { useFormState } from "react-dom"; | ||
|
||
import { LoginButton } from "./login-button"; | ||
import { Input } from "@umamin/ui/components/input"; | ||
import { Label } from "@umamin/ui/components/label"; | ||
|
||
export function LoginForm() { | ||
const [state, formAction] = useFormState(login, { error: "" }); | ||
|
||
return ( | ||
<form action={formAction} className="space-y-6"> | ||
<div> | ||
<Label htmlFor="username">Username</Label> | ||
<Input | ||
required | ||
id="username" | ||
name="username" | ||
placeholder="umamin" | ||
className="mt-2" | ||
/> | ||
</div> | ||
|
||
<div> | ||
<Label htmlFor="password">Password</Label> | ||
<Input | ||
required | ||
id="password" | ||
name="password" | ||
type="password" | ||
className="mt-2" | ||
/> | ||
{!!state?.error && ( | ||
<p className="text-red-500 text-sm mt-2 font-medium">{state.error}</p> | ||
)} | ||
</div> | ||
|
||
<LoginButton /> | ||
</form> | ||
); | ||
} |
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,25 @@ | ||
"use client"; | ||
|
||
import Link from "next/link"; | ||
import { Loader2 } from "lucide-react"; | ||
import { useFormStatus } from "react-dom"; | ||
import { Button } from "@umamin/ui/components/button"; | ||
|
||
export function LoginButton() { | ||
const { pending } = useFormStatus(); | ||
|
||
return ( | ||
<div> | ||
<Button disabled={pending} type="submit" className="w-full"> | ||
{pending && <Loader2 className="mr-2 h-4 w-4 animate-spin" />} | ||
Login | ||
</Button> | ||
|
||
<Button disabled={pending} variant="outline" asChild> | ||
<Link href="/login/google" className="mt-4 w-full"> | ||
Continue with Google | ||
</Link> | ||
</Button> | ||
</div> | ||
); | ||
} |
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,158 @@ | ||
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; | ||
} |
Oops, something went wrong.