Repository: gitdagray/next-auth-intro Branch: main Commit: 4281c170ac2e Files: 20 Total size: 10.5 KB Directory structure: gitextract_gzu13coo/ ├── .eslintrc.json ├── .gitignore ├── README.md ├── next.config.js ├── package.json ├── postcss.config.js ├── src/ │ ├── app/ │ │ ├── api/ │ │ │ └── auth/ │ │ │ └── [...nextauth]/ │ │ │ ├── options.ts │ │ │ └── route.ts │ │ ├── client/ │ │ │ └── page.tsx │ │ ├── components/ │ │ │ ├── Navbar.tsx │ │ │ └── UserCard.tsx │ │ ├── context/ │ │ │ └── AuthProvider.tsx │ │ ├── extra/ │ │ │ └── page.tsx │ │ ├── globals.css │ │ ├── layout.tsx │ │ ├── page.tsx │ │ └── server/ │ │ └── page.tsx │ └── middleware.ts ├── tailwind.config.js └── tsconfig.json ================================================ FILE CONTENTS ================================================ ================================================ FILE: .eslintrc.json ================================================ { "extends": "next/core-web-vitals" } ================================================ FILE: .gitignore ================================================ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies /node_modules /.pnp .pnp.js # testing /coverage # next.js /.next/ /out/ # production /build # misc .DS_Store *.pem # debug npm-debug.log* yarn-debug.log* yarn-error.log* # local env files .env*.local # vercel .vercel # typescript *.tsbuildinfo next-env.d.ts ================================================ FILE: README.md ================================================ # "NextAuth.js Login Authentication" ## With Next.js App Router --- ### Author Links 👋 Hello, I'm Dave Gray. 👉 [My Courses](https://courses.davegray.codes/) ✅ [Check out my YouTube Channel with hundreds of tutorials](https://www.youtube.com/DaveGrayTeachesCode). 🚩 [Subscribe to my channel](https://bit.ly/3nGHmNn) ☕ [Buy Me A Coffee](https://buymeacoffee.com/DaveGray) 🚀 Follow Me: - [Twitter](https://twitter.com/yesdavidgray) - [LinkedIn](https://www.linkedin.com/in/davidagray/) - [Blog](https://yesdavidgray.com) - [Reddit](https://www.reddit.com/user/DaveOnEleven) --- ### Description 📺 [YouTube Video](https://youtu.be/w2h54xz6Ndw) for this repository. --- ### 🎓 Academic Honesty **DO NOT COPY FOR AN ASSIGNMENT** - Avoid plagiarism and adhere to the spirit of this [Academic Honesty Policy](https://www.freecodecamp.org/news/academic-honesty-policy/). --- ### ⚙ Free Web Dev Tools - 🔗 [Google Chrome Web Browser](https://google.com/chrome/) - 🔗 [Visual Studio Code (aka VS Code)](https://code.visualstudio.com/) - 🔗 [ES7 React Snippets](https://marketplace.visualstudio.com/items?itemName=dsznajder.es7-react-js-snippets) ### 📚 References - 🔗 [NextAuth.js Official Site](https://next-auth.js.org/) - 🔗 [Next.js Official Site](https://nextjs.org/) ================================================ FILE: next.config.js ================================================ /** @type {import('next').NextConfig} */ const nextConfig = { images: { remotePatterns: [ { protocol: 'https', hostname: 'avatars.githubusercontent.com', port: '', pathname: '/u/**', }, ], }, } module.exports = nextConfig ================================================ FILE: package.json ================================================ { "name": "next-auth-tut", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev", "build": "next build", "start": "next start", "lint": "next lint" }, "dependencies": { "@types/node": "20.3.2", "@types/react": "18.2.14", "@types/react-dom": "18.2.6", "autoprefixer": "10.4.14", "eslint": "8.43.0", "eslint-config-next": "13.4.7", "next": "13.4.7", "next-auth": "^4.22.1", "postcss": "8.4.24", "react": "18.2.0", "react-dom": "18.2.0", "tailwindcss": "3.3.2", "typescript": "5.1.6" } } ================================================ FILE: postcss.config.js ================================================ module.exports = { plugins: { tailwindcss: {}, autoprefixer: {}, }, } ================================================ FILE: src/app/api/auth/[...nextauth]/options.ts ================================================ import type { NextAuthOptions } from 'next-auth' import GitHubProvider from 'next-auth/providers/github' import CredentialsProvider from 'next-auth/providers/credentials' export const options: NextAuthOptions = { providers: [ GitHubProvider({ clientId: process.env.GITHUB_ID as string, clientSecret: process.env.GITHUB_SECRET as string, }), CredentialsProvider({ name: "Credentials", credentials: { username: { label: "Username:", type: "text", placeholder: "your-cool-username" }, password: { label: "Password:", type: "password", placeholder: "your-awesome-password" } }, async authorize(credentials) { // This is where you need to retrieve user data // to verify with credentials // Docs: https://next-auth.js.org/configuration/providers/credentials const user = { id: "42", name: "Dave", password: "nextauth" } if (credentials?.username === user.name && credentials?.password === user.password) { return user } else { return null } } }) ], } ================================================ FILE: src/app/api/auth/[...nextauth]/route.ts ================================================ import NextAuth from 'next-auth' import { options } from './options' const handler = NextAuth(options) export { handler as GET, handler as POST } ================================================ FILE: src/app/client/page.tsx ================================================ 'use client' // Remember you must use an AuthProvider for // client components to useSession import { useSession } from 'next-auth/react' import { redirect } from 'next/navigation' import UserCard from '../components/UserCard' export default function ClientPage() { const { data: session } = useSession({ required: true, onUnauthenticated() { redirect('/api/auth/signin?callbackUrl=/client') } }) return (
) } ================================================ FILE: src/app/components/Navbar.tsx ================================================ import Link from "next/link" export default function Navbar() { return ( ) } ================================================ FILE: src/app/components/UserCard.tsx ================================================ import Image from "next/image" type User = { name?: string | null | undefined; email?: string | null | undefined; image?: string | null | undefined; } | undefined type Props = { user: User, pagetype: string, } export default function Card({ user, pagetype }: Props) { //console.log(user) const greeting = user?.name ? (
Hello {user?.name}!
) : null // const emailDisplay = user?.email ? ( //
// {user?.email} //
// ) : null const userImage = user?.image ? ( {user?.name ) : null return (
{greeting} {/* {emailDisplay} */} {userImage}

{pagetype} Page!

) } ================================================ FILE: src/app/context/AuthProvider.tsx ================================================ 'use client' import { SessionProvider } from 'next-auth/react' export default function AuthProvider({ children }: { children: React.ReactNode }) { return ( {children} ) } ================================================ FILE: src/app/extra/page.tsx ================================================ export default async function ExtraPage() { return

Extra Page!

} ================================================ FILE: src/app/globals.css ================================================ @tailwind base; @tailwind components; @tailwind utilities; :root { --foreground-rgb: 0, 0, 0; --background-start-rgb: 214, 219, 220; --background-end-rgb: 255, 255, 255; } @media (prefers-color-scheme: dark) { :root { --foreground-rgb: 255, 255, 255; --background-start-rgb: 0, 0, 0; --background-end-rgb: 0, 0, 0; } } body { color: rgb(var(--foreground-rgb)); background: linear-gradient( to bottom, transparent, rgb(var(--background-end-rgb)) ) rgb(var(--background-start-rgb)); } ================================================ FILE: src/app/layout.tsx ================================================ import './globals.css' import { Inter } from 'next/font/google' import Navbar from './components/Navbar' import AuthProvider from './context/AuthProvider' const inter = Inter({ subsets: ['latin'] }) export const metadata = { title: 'NextAuth Tutorial', description: 'Learn NextAuth.js by Dave Gray', } export default function RootLayout({ children, }: { children: React.ReactNode }) { return (
{children}
) } ================================================ FILE: src/app/page.tsx ================================================ import { options } from "./api/auth/[...nextauth]/options" import { getServerSession } from "next-auth/next" import UserCard from "./components/UserCard" export default async function Home() { const session = await getServerSession(options) return ( <> {session ? ( ) : (

You Shall Not Pass!

)} ) } ================================================ FILE: src/app/server/page.tsx ================================================ import { options } from "../api/auth/[...nextauth]/options" import { getServerSession } from "next-auth/next" import UserCard from "../components/UserCard" import { redirect } from "next/navigation" export default async function ServerPage() { const session = await getServerSession(options) if (!session) { redirect('/api/auth/signin?callbackUrl=/server') } return (
) } ================================================ FILE: src/middleware.ts ================================================ // Without a defined matcher, this one line applies next-auth // to the entire project export { default } from "next-auth/middleware" // Applies next-auth only to matching routes - can be regex // Ref: https://nextjs.org/docs/app/building-your-application/routing/middleware#matcher export const config = { matcher: ["/extra", "/dashboard"] } ================================================ FILE: tailwind.config.js ================================================ /** @type {import('tailwindcss').Config} */ module.exports = { content: [ './src/pages/**/*.{js,ts,jsx,tsx,mdx}', './src/components/**/*.{js,ts,jsx,tsx,mdx}', './src/app/**/*.{js,ts,jsx,tsx,mdx}', ], theme: { extend: { backgroundImage: { 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 'gradient-conic': 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', }, }, }, plugins: [], } ================================================ FILE: tsconfig.json ================================================ { "compilerOptions": { "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true, "strict": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "module": "esnext", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "jsx": "preserve", "incremental": true, "plugins": [ { "name": "next" } ], "paths": { "@/*": ["./src/*"] } }, "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], "exclude": ["node_modules"] }