Skip to content

Commit

Permalink
add authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu committed Feb 8, 2024
1 parent 3546c2e commit 19c2367
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 22 deletions.
3 changes: 2 additions & 1 deletion nextjs/app/api/auth/[...nextauth]/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import NextAuth from "next-auth";
import GithubProvider from "next-auth/providers/github";

export const options = {
const options = {
secret: process.env.NEXTAUTH_SECRET,
providers: [
GithubProvider({
clientId: process.env.GITHUB_ID!,
Expand Down
17 changes: 0 additions & 17 deletions nextjs/app/api/restricted.ts

This file was deleted.

3 changes: 2 additions & 1 deletion nextjs/app/components/login-btn.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"use client";
import { useSession, signIn, signOut } from "next-auth/react";

export default function LoginButton() {
Expand All @@ -6,7 +7,7 @@ export default function LoginButton() {
if (session) {
return (
<>
Signed in as {session.user?.email} <br />
Signed in as {session?.user?.email} <br />
<button onClick={() => signOut()}>Sign out</button>
</>
);
Expand Down
3 changes: 3 additions & 0 deletions nextjs/app/components/session-provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"use client";
import { SessionProvider } from "next-auth/react";
export default SessionProvider;
10 changes: 8 additions & 2 deletions nextjs/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { Metadata } from "next";
import { getServerSession } from "next-auth";
import { Inter } from "next/font/google";
import SessionProvider from "./components/session-provider";
import "./globals.css";

const inter = Inter({ subsets: ["latin"] });
Expand All @@ -9,14 +11,18 @@ export const metadata: Metadata = {
description: "Generated by create next app",
};

export default function RootLayout({
export default async function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
const session = await getServerSession();

return (
<html lang="en">
<body className={inter.className}>{children}</body>
<body className={inter.className}>
<SessionProvider session={session}>{children}</SessionProvider>
</body>
</html>
);
}
6 changes: 5 additions & 1 deletion nextjs/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import LoginButton from "./components/login-btn";

export default function Home() {
return <LoginButton />;
return (
<div>
<LoginButton />
</div>
);
}
14 changes: 14 additions & 0 deletions nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,19 @@
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"Chrome 118",
"last 1 chrome version",
"last 1 edge version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

0 comments on commit 19c2367

Please sign in to comment.