DEV Community

Cover image for Build a guestbook with Next.js, NextAuth and Supabase
EliasChen
EliasChen

Posted on • Edited on

Build a guestbook with Next.js, NextAuth and Supabase

Stack

  • Next.js as a framework
  • NextAuth (Authentication for Next.js)
  • Supabase (guestbook database)

Supabase

What is supabase?

Supabase is an open source Firebase alternative providing all the backend features you need to build a product and offer users more simple UI then Firebase.

Install supabase.

# npm
npm install @supabase/supabase-js
# yarn
yarn add @supabase/supabase-js
Enter fullscreen mode Exit fullscreen mode

Sign up an account on https://supabase.com, create a new project (mine: Guestbookdemo).
Create a table (mine: guestbookdata).

Remember to disable RLS(Row Level Security) for table

Image description

Create supabaseClient.js in /lib .

// lib/supabaseClient.js
import { createClient } from '@supabase/supabase-js'

export const supabase = createClient('<project_url>', '<anon_public>')
Enter fullscreen mode Exit fullscreen mode

Getting data from supabase data.

// pages/index.jsx
import React from 'react'
import { supabase } from '../lib/supabaseClient'

const index = () => {
    const [guestbookData, setGuestbookData] = React.useState(null)
    // fetch supabase table data
    const fetchguestbook = async () => {
        const { data, error } = await supabase.from('guestbook').select()
        if (data) {
            setGuestbookData(data)
            console.log(data)
        }
    }
    return (
        <div>
            <ul>{guestbookData && guestbookData.map((data) => <li>{data.message}</li>)}</ul>
        </div>
    )
}
export default index
Enter fullscreen mode Exit fullscreen mode

Upload table data to supabase.

// pages/index.jsx
// ...
const uploaddata = async (e) => {
    let { data } = await supabase.from('guestbook').insert([
        {
            message: '<your message>',
            username: '<your username>',
            email: '<your email>',
        },
    ])
    fetchguestbook()
}
// ...
Enter fullscreen mode Exit fullscreen mode

Remove table data from supabase.

// pages/index.jsx
// ...
const removedata = async (removeid) => {
    // removeid = table item you want to remove
    const { data } = await supabase.from('guestbook').delete().eq('id', removeid)
    fetchguestbook()
}
// ...
Enter fullscreen mode Exit fullscreen mode

NextAuth

Install NextAuth.

# npm
npm install next-auth
# yarn
yarn add next-auth
Enter fullscreen mode Exit fullscreen mode

To add NextAuth.js to a project create a file called [...nextauth].js in pages/api/auth

All provider set up documentation of NextAuth on https://next-auth.js.org/providers/

// pages/api/auth/[...nextauth].js
// Example for github provider
import NextAuth from 'next-auth'
import GithubProvider from 'next-auth/providers/github'

export const authOptions = {
    // Configure one or more authentication providers
    providers: [
        GithubProvider({
            clientId: process.env.GITHUB_ID,
            clientSecret: process.env.GITHUB_SECRET,
        }),
        // ...add more providers here
    ],
}

export default NextAuth(authOptions)
Enter fullscreen mode Exit fullscreen mode

Add NextAuth login in pages.

// pages/index.jsx
// ...
import { useSession, signIn, signOut } from 'next-auth/react'

const index = () => {
    const { data: session } = useSession()

    return(
      // ...
      <button onClick={()=>signIn('Login provider')}>Sign in</button>
      <button onClcik={()=>signOut()} >Sign out</button>
      // ...
    )
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

Supabase and NextAuth are both perfect choices for beginners who want to learn basic database or build a simple auth login application.

Guestbook demo: https://eliaschen-guestbook-demo.vercel.app
Guestbook demo (github repository): https://github.com/chenelias/eliaschen-guestbook-demo
My Guestbook: https://eliaschen.dev/guestbook

⚡Thanks for reading, see you in the next blog.

Top comments (2)

Collapse
 
mrzodoxvpython profile image
MrZodoxVpython

love you elias!

Collapse
 
eliaschen profile image
EliasChen

Thanks !!!