This file contains hidden or 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
// prisma/prisma.js | |
import { PrismaClient } from '@prisma/client' | |
let prisma | |
if (process.env.NODE_ENV === 'production') { | |
prisma = new PrismaClient() | |
} else { | |
if (!global.prisma) { | |
global.prisma = new PrismaClient() |
This file contains hidden or 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
// prisma/schema.prisma | |
datasource db { | |
provider = "mongodb" | |
url = env("DATABASE_URL") | |
} | |
generator client { | |
provider = "prisma-client-js" | |
previewFeatures = ["mongoDb"] |
This file contains hidden or 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
import nodemailer from 'nodemailer' | |
import NextAuth from 'next-auth' | |
import { MongoDBAdapter } from '@next-auth/mongodb-adapter' | |
import EmailProvider from 'next-auth/providers/email' | |
import MongoClientPromise from '../../../lib/mongodb' | |
const THIRTY_DAYS = 30 * 24 * 60 * 60 | |
const THIRTY_MINUTES = 30 * 60 | |
export default NextAuth({ |
This file contains hidden or 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
import React, { useState } from 'react' | |
import { signIn } from 'next-auth/react' | |
const Login = () => { | |
const [email, setEmail] = useState('') | |
const sendLoginVerification = e => { | |
e.preventDefault() | |
// Notice, we are also redirecting users to the protected route instead of the homepage after signing in. |
This file contains hidden or 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
import NextAuth from 'next-auth' | |
import { MongoDBAdapter } from '@next-auth/mongodb-adapter' | |
import EmailProvider from 'next-auth/providers/email' | |
import MongoClientPromise from '../../../lib/mongodb' | |
const THIRTY_DAYS = 30 * 24 * 60 * 60 | |
const THIRTY_MINUTES = 30 * 60 | |
export default NextAuth({ | |
pages: { |
This file contains hidden or 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
import { getToken } from 'next-auth/jwt' | |
import { NextResponse } from 'next/server' | |
export async function middleware (req) { | |
const session = await getToken({ | |
req, | |
secret: process.env.SECRET, | |
secureCookie: process.env.NEXTAUTH_URL?.startsWith('https://') | |
}) |
This file contains hidden or 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
import { useSession } from 'next-auth/react' | |
import { useRouter } from 'next/router' | |
export default function ClientProtected () { | |
const router = useRouter() | |
const { status } = useSession({ | |
required: true, | |
onUnauthenticated () { | |
router.push('/api/auth/signin') | |
} |
This file contains hidden or 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
import { getSession } from 'next-auth/react' | |
export default async function handler (req, res) { | |
const session = await getSession({ req }) | |
console.log({ session }) | |
res.status(200).json({ session }) | |
} |
This file contains hidden or 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
import { getSession, signOut, useSession } from 'next-auth/react' | |
import Link from 'next/link' | |
import React from 'react' | |
const Homepage = () => { | |
// Check if the user is authenticated from the client | |
const { data: session, status } = useSession() | |
if (status === 'loading') { | |
return <>Loading...</> |
This file contains hidden or 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
import { signOut, useSession } from 'next-auth/react' | |
import Link from 'next/link' | |
import React from 'react' | |
const Homepage = () => { | |
const { data: session, status } = useSession() | |
if (status === 'loading') { | |
return <>Loading...</> | |
} |