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 (
{pagetype} Page!