-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add database integration and protected routes
- Loading branch information
Showing
23 changed files
with
639 additions
and
138 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 |
---|---|---|
|
@@ -26,7 +26,7 @@ yarn-debug.log* | |
yarn-error.log* | ||
|
||
# local env files | ||
.env*.local | ||
.env | ||
|
||
# vercel | ||
.vercel | ||
|
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,67 +1,6 @@ | ||
import NextAuth, { NextAuthOptions } from "next-auth"; | ||
import GithubProvider from "next-auth/providers/github"; | ||
import NextAuth from "next-auth"; | ||
import { authOptions } from "../../../../lib/options"; | ||
|
||
const GITHUB_ID = process.env.GITHUB_ID ?? ""; | ||
const GITHUB_SECRET = process.env.GITHUB_SECRET ?? ""; | ||
const handler = NextAuth(authOptions); | ||
|
||
export const authOptions: NextAuthOptions = { | ||
pages: { | ||
signIn: "null", | ||
signOut: "null", | ||
error: "null", | ||
verifyRequest: "null", | ||
newUser: "null", | ||
}, | ||
providers: [ | ||
GithubProvider({ | ||
clientId: GITHUB_ID, | ||
clientSecret: GITHUB_SECRET, | ||
authorization: { | ||
params: { | ||
scope: "repo read:user user:email", | ||
}, | ||
}, | ||
}), | ||
], | ||
callbacks: { | ||
async signIn({ user, account, profile }) { | ||
const db_data = { | ||
name: profile?.name, | ||
email: user?.email, | ||
avatar_url: profile?.avatar_url, | ||
bio: profile?.bio, | ||
id: profile?.id, | ||
access_token: account?.access_token, | ||
}; | ||
for (const entry of Object.keys(db_data)) { | ||
if (!db_data[entry as keyof typeof db_data]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}, | ||
async session({ session, token }) { | ||
if (session.user) { | ||
session.user.bio = token.bio as string | undefined; | ||
session.user.avatar_url = token.avatar_url as string | undefined; | ||
} | ||
return session; | ||
}, | ||
async jwt({ token, user, profile }) { | ||
if (user) { | ||
token.id = user.id; | ||
token.bio = profile?.bio as string | undefined; | ||
token.avatar_url = profile?.avatar_url as string | undefined; | ||
token.name = user.name; | ||
} | ||
return token; | ||
}, | ||
}, | ||
session: { | ||
strategy: "jwt", | ||
}, | ||
secret: process.env.NEXT_AUTH_SECRET, | ||
}; | ||
|
||
export const GET = NextAuth(authOptions); | ||
export const POST = NextAuth(authOptions); | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import ProtectRoute from "@/components/ProtectRoute"; | ||
import React from "react"; | ||
|
||
export default async function Dashboard() { | ||
return (await ProtectRoute()) ?? <div>page</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
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,21 @@ | ||
"use client"; | ||
|
||
import { signIn } from "next-auth/react"; | ||
import { Button } from "./ui/button"; | ||
import { GithubIcon } from "lucide-react"; | ||
|
||
export default function LoginWithGithub() { | ||
const onSignin = () => { | ||
signIn("github"); | ||
}; | ||
|
||
return ( | ||
<Button | ||
className="w-full bg-[#24292e] hover:bg-[#2f363d] text-white" | ||
onClick={onSignin} | ||
> | ||
<GithubIcon className="w-4 h-4 mr-2" /> | ||
Login with GitHub | ||
</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
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,35 @@ | ||
import { getServerSession } from "next-auth"; | ||
import React from "react"; | ||
|
||
import { | ||
Card, | ||
CardContent, | ||
CardDescription, | ||
CardHeader, | ||
CardTitle, | ||
} from "@/components/ui/card"; | ||
import LoginWithGithub from "./LoginWithGithub"; | ||
import { authOptions } from "@/lib/options"; | ||
|
||
export default async function ProtectRoute() { | ||
const session = await getServerSession(authOptions); | ||
console.log("session",session); | ||
|
||
if (!session) { | ||
return ( | ||
<div className="w-full h-full flex items-center justify-center text-white"> | ||
<Card className="w-[350px]"> | ||
<CardHeader> | ||
<CardTitle>Unauthorized Access</CardTitle> | ||
<CardDescription> | ||
You are not authorized to view this page. | ||
</CardDescription> | ||
</CardHeader> | ||
<CardContent> | ||
<LoginWithGithub /> | ||
</CardContent> | ||
</Card> | ||
</div> | ||
); | ||
} | ||
} |
Oops, something went wrong.