-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
337 additions
and
40 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { type NextRequest } from "next/server"; | ||
import * as qs from "qs"; | ||
import { saveUser, User } from "@/utils/db"; | ||
import { redirect } from "next/navigation"; | ||
|
||
export const dynamic = "force-dynamic"; // defaults to auto | ||
|
||
export async function GET(request: NextRequest) { | ||
// first step | ||
// get user info using github api | ||
const searchParams = request.nextUrl.searchParams; | ||
const code = searchParams.get("code"); | ||
|
||
if (!code) { | ||
// This happens when user cancelled the authentication. | ||
// In this case, we send an empty message which indicates no data available. | ||
|
||
return Response.json({ | ||
status: 400, | ||
code: "github_issue", | ||
description: "Github redirect code not found, please try again", | ||
}); | ||
} | ||
|
||
const q = qs.stringify({ | ||
client_id: process.env.NEXT_PUBLIC_GITHUB_OAUTH_CLIENT_ID, | ||
client_secret: process.env.GITHUB_OAUTH_CLIENT_SECRET, | ||
code: code, | ||
}); | ||
|
||
const accessTokenRes = await fetch( | ||
`https://github.com/login/oauth/access_token?${q}`, | ||
{ | ||
method: "POST", | ||
headers: { | ||
Accept: "application/json", | ||
}, | ||
} | ||
); | ||
|
||
if (!accessTokenRes.ok) { | ||
console.error( | ||
`Failed to get access token: ${ | ||
accessTokenRes.status | ||
} ${await accessTokenRes.text()}` | ||
); | ||
|
||
return Response.json({ | ||
status: 400, | ||
code: "github_issue", | ||
description: "Error generating access token, please try again ", | ||
}); | ||
} | ||
|
||
const { access_token: accessToken } = await accessTokenRes.json(); | ||
|
||
const userRes = await fetch("https://api.github.com/user", { | ||
headers: { | ||
Authorization: `bearer ${accessToken as string}`, | ||
}, | ||
}); | ||
|
||
if (!userRes.ok) { | ||
console.error( | ||
`Failed to get GitHub user: ${userRes.status} ${await userRes.text()}` | ||
); | ||
return Response.json({ | ||
status: 400, | ||
code: "github_issue", | ||
description: "Error retrieving user info , please try again ", | ||
}); | ||
} | ||
|
||
const user: User & { avatar_url: string } = await userRes.json(); | ||
|
||
if (!Boolean(user.login)) { | ||
return Response.json({ | ||
status: 400, | ||
code: "github_issue", | ||
description: "Invalid Github user data", | ||
}); | ||
} | ||
|
||
// save user to database | ||
|
||
try { | ||
await saveUser({ | ||
login: user.login, | ||
email: user.email, | ||
avatar: user.avatar_url, | ||
bio: user.bio, | ||
name: user.name, | ||
}); | ||
// res.status(200).json(user); | ||
// fetch images and dont wait for response | ||
} catch (error) { | ||
console.log(error); | ||
|
||
return Response.json({ | ||
status: 400, | ||
code: "error_database", | ||
description: "Error saving to database ", | ||
error, | ||
}); | ||
} | ||
|
||
redirect(`/ticket/${user.login}/me`); | ||
} |
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,11 +1,25 @@ | ||
import { TicketHero } from "@/components/ticket-hero"; | ||
import { getUserInfo } from "@/utils/ticket-service"; | ||
import type { Metadata } from "next"; | ||
|
||
export default async function Page({ | ||
params, | ||
}: { | ||
type Props = { | ||
params: { username: string }; | ||
}) { | ||
searchParams: { [key: string]: string | string[] | undefined }; | ||
}; | ||
|
||
export async function generateMetadata({ params }: Props): Promise<Metadata> { | ||
// read route params | ||
const { metadata } = await getUserInfo(params.username); | ||
|
||
// optionally access and extend (rather than replace) parent metadata | ||
if (metadata) { | ||
return metadata; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
export default async function Page({ params }: Props) { | ||
const { user } = await getUserInfo(params.username); | ||
return <TicketHero {...user} />; | ||
} |
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,12 +1,25 @@ | ||
import { TicketHero } from "@/components/ticket-hero"; | ||
import { getUserInfo } from "@/utils/ticket-service"; | ||
import type { Metadata } from "next"; | ||
|
||
export default async function Page({ | ||
params, | ||
}: { | ||
type Props = { | ||
params: { username: string }; | ||
}) { | ||
// TODO fetch user da | ||
searchParams: { [key: string]: string | string[] | undefined }; | ||
}; | ||
|
||
export async function generateMetadata({ params }: Props): Promise<Metadata> { | ||
// read route params | ||
const { metadata } = await getUserInfo(params.username); | ||
|
||
// optionally access and extend (rather than replace) parent metadata | ||
if (metadata) { | ||
return metadata; | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
export default async function Page({ params }: Props) { | ||
const { user } = await getUserInfo(params.username); | ||
return <TicketHero {...user} url={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,152 @@ | ||
import { ImageResponse } from "next/og"; | ||
// App router includes @vercel/og. | ||
// No need to install it. | ||
|
||
export const runtime = "edge"; | ||
|
||
export async function GET(request: Request) { | ||
const { searchParams } = new URL(request.url); | ||
|
||
// ?title=<title> | ||
const hasName = searchParams.has("name"); | ||
const hasLogin = searchParams.has("login"); | ||
const hasAvatar = searchParams.has("avatar"); | ||
const hasTicketNumber = searchParams.has("ticketNumber"); | ||
const name = hasName ? searchParams.get("name") : "Name"; | ||
const login = hasLogin ? searchParams.get("login") : ""; | ||
const avatar = hasAvatar ? searchParams.get("avatar") : ""; | ||
const avatarUrl = avatar === null ? "" : avatar; | ||
const ticketNumber = hasTicketNumber ? searchParams.get("ticketNumber") : ""; | ||
const number = | ||
new Array(7 + 1 - (ticketNumber + "").length).join("0") + ticketNumber; | ||
if (!login) { | ||
return new ImageResponse( | ||
( | ||
<div | ||
style={{ | ||
display: "flex", | ||
fontSize: 60, | ||
color: "black", | ||
background: "#f6f6f6", | ||
width: "100%", | ||
height: "100%", | ||
paddingTop: 50, | ||
flexDirection: "column", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
textAlign: "center", | ||
}} | ||
> | ||
Ticket dosn't exist please visit blablaconf.com | ||
</div> | ||
), | ||
{ | ||
width: 1200, | ||
height: 630, | ||
} | ||
); | ||
} | ||
|
||
return new ImageResponse( | ||
( | ||
<div | ||
style={{ | ||
display: "flex", | ||
fontSize: 60, | ||
color: "black", | ||
background: "#f6f6f6", | ||
width: "100%", | ||
height: "100%", | ||
flexDirection: "column", | ||
justifyContent: "center", | ||
alignItems: "center", | ||
}} | ||
> | ||
<img | ||
width="1200" | ||
height="630" | ||
src={`https://res.cloudinary.com/duko2tssr/image/upload/v1706825045/ticket-back_b6qvdk.jpg`} | ||
/> | ||
<div | ||
style={{ | ||
display: "flex", | ||
position: "absolute", | ||
top: 70, | ||
left: 0, | ||
right: 82, | ||
height: 150, | ||
flexDirection: "row-reverse", | ||
}} | ||
> | ||
<img | ||
width="150" | ||
height="150" | ||
style={{ | ||
borderRadius: 100, | ||
}} | ||
src={avatarUrl} | ||
/> | ||
<div | ||
style={{ | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "flex-end", | ||
justifyContent: "center", | ||
marginRight: 30, | ||
}} | ||
> | ||
<span | ||
style={{ | ||
color: "white", | ||
textAlign: "right", | ||
fontWeight: "900", | ||
fontSize: 37, | ||
}} | ||
> | ||
{name} | ||
</span> | ||
<span | ||
style={{ | ||
color: "white", | ||
textAlign: "right", | ||
fontSize: 30, | ||
marginTop: 0, | ||
}} | ||
> | ||
@{login} | ||
</span> | ||
</div> | ||
</div> | ||
|
||
<div | ||
style={{ | ||
display: "flex", | ||
position: "absolute", | ||
top: 250, | ||
right: 150, | ||
height: 150, | ||
width: 300, | ||
// flexDirection: "row-reverse", | ||
transform: "rotate(-40deg)", | ||
}} | ||
> | ||
<span | ||
style={{ | ||
color: "white", | ||
opacity: 0.5, | ||
textAlign: "right", | ||
fontWeight: "900", | ||
fontSize: 54, | ||
}} | ||
> | ||
N {number}{" "} | ||
</span> | ||
</div> | ||
</div> | ||
), | ||
{ | ||
width: 1200, | ||
height: 630, | ||
} | ||
); | ||
} |
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.