diff --git a/examples/with-supabase-auth-realtime-db/.env.local.example b/examples/with-supabase-auth-realtime-db/.env.local.example new file mode 100644 index 0000000000000000000000000000000000000000..b6fedec92e30868dbba7a10ce5195776427708de --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/.env.local.example @@ -0,0 +1,2 @@ +NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co +NEXT_PUBLIC_SUPABASE_KEY=your-anon-key \ No newline at end of file diff --git a/examples/with-supabase-auth-realtime-db/.gitignore b/examples/with-supabase-auth-realtime-db/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..1437c53f70bc211ec65739ec4a8c2a4db5874c73 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/.gitignore @@ -0,0 +1,34 @@ +# 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 +.env.development.local +.env.test.local +.env.production.local + +# vercel +.vercel diff --git a/examples/with-supabase-auth-realtime-db/README.md b/examples/with-supabase-auth-realtime-db/README.md new file mode 100644 index 0000000000000000000000000000000000000000..7e944e3f6fa6196b3c3fc7d7f572c048cec7d6d6 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/README.md @@ -0,0 +1,139 @@ +# Realtime chat example using Supabase + +This is a full-stack Slack clone example using: + +- Frontend: + - Next.js. + - [Supabase](https://supabase.io/docs/library/getting-started) for user management and realtime data syncing. +- Backend: + - [app.supabase.io](https://app.supabase.io/): hosted Postgres database with restful API for usage with Supabase.js. + +![Demo animation gif](./docs/slack-clone-demo.gif) + +This example is a clone of the [Slack Clone example](https://github.com/supabase/supabase/tree/master/examples/slack-clone) in the supabase repo, feel free to check it out! + +## Deploy your own + +Once you have access to [the environment variables you'll need](#step-3-set-up-environment-variables), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example): + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-supabase-auth-realtime-db&env=NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_KEY&envDescription=Required%20to%20connect%20the%20app%to%Supabase&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-supabase-auth-realtime-db%23step-3-set-up-environment-variables&project-name=supabase-slack-clone&repo-name=supabase-slack-clone) + +## How to use + +Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example: + +```bash +npx create-next-app --example with-supabase-auth-realtime-db realtime-chat-app +# or +yarn create next-app --example with-supabase-auth-realtime-db realtime-chat-app +``` + +## Configuration + +### Step 1. Create a new Supabase project + +Sign up to Supabase - [https://app.supabase.io](https://app.supabase.io) and create a new project. Wait for your database to start. + +### Step 2. Run the "Slack Clone" Quickstart + +Once your database has started, run the "Slack Clone" quickstart. + +![Slack Clone Quick Start](https://user-images.githubusercontent.com/10214025/88916135-1b1d7a00-d298-11ea-82e7-e2c18314e805.png) + +### Step 3. Set up environment variables + +In your Supabase project, go to Project Settings (the cog icon), open the API tab, and find your **API URL** and **anon** key, you'll need these in the next step. + +![image](https://user-images.githubusercontent.com/10214025/88916245-528c2680-d298-11ea-8a71-708f93e1ce4f.png) + +Next, copy the `.env.local.example` file in this directory to `.env.local` (which will be ignored by Git): + +```bash +cp .env.local.example .env.local +``` + +Then set each variable on `.env.local`: + +- `NEXT_PUBLIC_SUPABASE_URL` should be the **API URL** +- `NEXT_PUBLIC_SUPABASE_KEY` should be the **anon** key + +The **anon** key is your client-side API key. It allows "anonymous access" to your database, until the user has logged in. Once they have logged in, the keys will switch to the user's own login token. This enables row level security for your data. Read more about this [below](#postgres-row-level-security). + +> **_NOTE_**: The `service_role` key has full access to your data, bypassing any security policies. These keys have to be kept secret and are meant to be used in server environments and never on a client or browser. + +### Step 4. Run Next.js in development mode + +```bash +npm install +npm run dev + +# or + +yarn install +yarn dev +``` + +Visit [http://localhost:3000](http://localhost:3000) and start chatting! Open a channel across two browser tabs to see everything getting updated in realtime 🥳. If it doesn't work, post on [GitHub discussions](https://github.com/vercel/next.js/discussions). + +### Step 5. Deploy on Vercel + +You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)). + +#### Deploy Your Local Project + +To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/import/git?utm_source=github&utm_medium=readme&utm_campaign=next-example). + +**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file. + +#### Deploy from Our Template + +Alternatively, you can deploy using our template by clicking on the Deploy button below. + +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-supabase-auth-realtime-db&env=NEXT_PUBLIC_SUPABASE_URL,NEXT_PUBLIC_SUPABASE_KEY&envDescription=Required%20to%20connect%20the%20app%to%Supabase&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-supabase-auth-realtime-db%23step-3-set-up-environment-variables&project-name=supabase-slack-clone&repo-name=supabase-slack-clone) + +## Supabase details + +### Postgres Row level security + +This project uses very high-level Authorization using Postgres' Role Level Security. +When you start a Postgres database on Supabase, we populate it with an `auth` schema, and some helper functions. +When a user logs in, they are issued a JWT with the role `authenticated` and thier UUID. +We can use these details to provide fine-grained control over what each user can and cannot do. + +This is a trimmed-down schema, with the policies: + +```sql +-- USER PROFILES +CREATE TYPE public.user_status AS ENUM ('ONLINE', 'OFFLINE'); +CREATE TABLE public.users ( + id uuid NOT NULL PRIMARY KEY, -- UUID from auth.users (Supabase) + username text, + status user_status DEFAULT 'OFFLINE'::public.user_status +); +ALTER TABLE public.users ENABLE ROW LEVEL SECURITY; +CREATE POLICY "Allow logged-in read access" on public.users FOR SELECT USING ( auth.role() = 'authenticated' ); +CREATE POLICY "Allow individual insert access" on public.users FOR INSERT WITH CHECK ( auth.uid() = id ); +CREATE POLICY "Allow individual update access" on public.users FOR UPDATE USING ( auth.uid() = id ); + +-- CHANNELS +CREATE TABLE public.channels ( + id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL, + slug text NOT NULL UNIQUE +); +ALTER TABLE public.channels ENABLE ROW LEVEL SECURITY; +CREATE POLICY "Allow logged-in full access" on public.channels FOR ALL USING ( auth.role() = 'authenticated' ); + +-- MESSAGES +CREATE TABLE public.messages ( + id bigint GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, + inserted_at timestamp with time zone DEFAULT timezone('utc'::text, now()) NOT NULL, + message text, + user_id uuid REFERENCES public.users NOT NULL, + channel_id bigint REFERENCES public.channels NOT NULL +); +ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY; +CREATE POLICY "Allow logged-in read access" on public.messages USING ( auth.role() = 'authenticated' ); +CREATE POLICY "Allow individual insert access" on public.messages FOR INSERT WITH CHECK ( auth.uid() = user_id ); +CREATE POLICY "Allow individual update access" on public.messages FOR UPDATE USING ( auth.uid() = user_id ); +``` diff --git a/examples/with-supabase-auth-realtime-db/components/Layout.js b/examples/with-supabase-auth-realtime-db/components/Layout.js new file mode 100644 index 0000000000000000000000000000000000000000..1b94dea575aa5ac8fcb8b5096548ad1660647c4e --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/components/Layout.js @@ -0,0 +1,80 @@ +import Link from 'next/link' +import { useContext } from 'react' +import UserContext from '~/lib/UserContext' +import { addChannel } from '~/lib/Store' + +export default function Layout(props) { + const { signOut } = useContext(UserContext) + + const slugify = (text) => { + return text + .toString() + .toLowerCase() + .replace(/\s+/g, '-') // Replace spaces with - + .replace(/[^\w-]+/g, '') // Remove all non-word chars + .replace(/--+/g, '-') // Replace multiple - with single - + .replace(/^-+/, '') // Trim - from start of text + .replace(/-+$/, '') // Trim - from end of text + } + + const newChannel = async () => { + const slug = prompt('Please enter your name') + if (slug) { + addChannel(slugify(slug)) + } + } + + return ( +
+ {/* Sidebar */} + + + {/* Messages */} +
{props.children}
+
+ ) +} + +const SidebarItem = ({ channel, isActiveChannel }) => ( + <> +
  • + + {channel.slug} + +
  • + +) diff --git a/examples/with-supabase-auth-realtime-db/components/Message.js b/examples/with-supabase-auth-realtime-db/components/Message.js new file mode 100644 index 0000000000000000000000000000000000000000..1590a0ab9c09505598edbae2946f6eb59dbb8d19 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/components/Message.js @@ -0,0 +1,10 @@ +const Message = ({ message }) => ( + <> +
    +

    {message.author.username}

    +

    {message.message}

    +
    + +) + +export default Message diff --git a/examples/with-supabase-auth-realtime-db/components/MessageInput.js b/examples/with-supabase-auth-realtime-db/components/MessageInput.js new file mode 100644 index 0000000000000000000000000000000000000000..1c0e3501beb571a41326a284dde084d1c086418e --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/components/MessageInput.js @@ -0,0 +1,28 @@ +import { useState } from 'react' + +const MessageInput = ({ onSubmit }) => { + const [messageText, setMessageText] = useState('') + + const submitOnEnter = (event) => { + // Watch for enter key + if (event.keyCode === 13) { + onSubmit(messageText) + setMessageText('') + } + } + + return ( + <> + setMessageText(e.target.value)} + onKeyDown={(e) => submitOnEnter(e)} + /> + + ) +} + +export default MessageInput diff --git a/examples/with-supabase-auth-realtime-db/docs/slack-clone-demo.gif b/examples/with-supabase-auth-realtime-db/docs/slack-clone-demo.gif new file mode 100644 index 0000000000000000000000000000000000000000..8dff9b017beaebe7ef6038cd52bfe3a47530dddf Binary files /dev/null and b/examples/with-supabase-auth-realtime-db/docs/slack-clone-demo.gif differ diff --git a/examples/with-supabase-auth-realtime-db/jsconfig.json b/examples/with-supabase-auth-realtime-db/jsconfig.json new file mode 100644 index 0000000000000000000000000000000000000000..7af8f6e0edda1038fc0af489da8cbb25c2f290b7 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/jsconfig.json @@ -0,0 +1,8 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "paths": { + "~/*": ["./*"] + } + } +} diff --git a/examples/with-supabase-auth-realtime-db/lib/Store.js b/examples/with-supabase-auth-realtime-db/lib/Store.js new file mode 100644 index 0000000000000000000000000000000000000000..b22779c735a08b95f2ba7165afc290d5eb65252d --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/lib/Store.js @@ -0,0 +1,169 @@ +import { useState, useEffect } from 'react' +import { createClient } from '@supabase/supabase-js' + +const supabase = createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL, + process.env.NEXT_PUBLIC_SUPABASE_KEY +) + +/** + * @param {number} channelId the currently selected Channel + */ +export const useStore = (props) => { + const [channels, setChannels] = useState([]) + const [messages, setMessages] = useState([]) + const [users] = useState(new Map()) + const [newMessage, handleNewMessage] = useState(null) + const [newChannel, handleNewChannel] = useState(null) + const [newOrUpdatedUser, handleNewOrUpdatedUser] = useState(null) + + // Load initial data and set up listeners + useEffect(() => { + // Get Channels + fetchChannels(setChannels) + // Listen for new messages + const messageListener = supabase + .from('messages') + .on('INSERT', (payload) => handleNewMessage(payload.new)) + .subscribe() + // Listen for changes to our users + const userListener = supabase + .from('users') + .on('*', (payload) => handleNewOrUpdatedUser(payload.new)) + .subscribe() + // Listen for new channels + const channelListener = supabase + .from('channels') + .on('INSERT', (payload) => handleNewChannel(payload.new)) + .subscribe() + // Cleanup on unmount + return () => { + messageListener.unsubscribe() + userListener.unsubscribe() + channelListener.unsubscribe() + } + }, []) + + // Update when the route changes + useEffect(() => { + if (props?.channelId > 0) { + fetchMessages(props.channelId, (messages) => { + messages.forEach((x) => users.set(x.user_id, x.author)) + setMessages(messages) + }) + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [props.channelId]) + + // New message recieved from Postgres + useEffect(() => { + if (newMessage && newMessage.channel_id === Number(props.channelId)) { + const handleAsync = async () => { + let authorId = newMessage.user_id + if (!users.get(authorId)) + await fetchUser(authorId, (user) => handleNewOrUpdatedUser(user)) + setMessages(messages.concat(newMessage)) + } + handleAsync() + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [newMessage]) + + // New channel recieved from Postgres + useEffect(() => { + if (newChannel) setChannels(channels.concat(newChannel)) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [newChannel]) + + // New or updated user recieved from Postgres + useEffect(() => { + if (newOrUpdatedUser) users.set(newOrUpdatedUser.id, newOrUpdatedUser) + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [newOrUpdatedUser]) + + return { + // We can export computed values here to map the authors to each message + messages: messages.map((x) => ({ ...x, author: users.get(x.user_id) })), + channels: channels.sort((a, b) => a.slug.localeCompare(b.slug)), + users, + } +} + +/** + * Fetch all channels + * @param {function} setState Optionally pass in a hook or callback to set the state + */ +export const fetchChannels = async (setState) => { + try { + let { body } = await supabase.from('channels').select('*') + if (setState) setState(body) + return body + } catch (error) { + console.log('error', error) + } +} + +/** + * Fetch a single user + * @param {number} userId + * @param {function} setState Optionally pass in a hook or callback to set the state + */ +export const fetchUser = async (userId, setState) => { + try { + let { body } = await supabase.from('users').eq('id', userId).select(`*`) + let user = body[0] + if (setState) setState(user) + return user + } catch (error) { + console.log('error', error) + } +} + +/** + * Fetch all messages and their authors + * @param {number} channelId + * @param {function} setState Optionally pass in a hook or callback to set the state + */ +export const fetchMessages = async (channelId, setState) => { + try { + let { body } = await supabase + .from('messages') + .eq('channel_id', channelId) + .select(`*, author:user_id(*)`) + .order('inserted_at', true) + if (setState) setState(body) + return body + } catch (error) { + console.log('error', error) + } +} + +/** + * Insert a new channel into the DB + * @param {string} slug The channel name + */ +export const addChannel = async (slug) => { + try { + let { body } = await supabase.from('channels').insert([{ slug }]) + return body + } catch (error) { + console.log('error', error) + } +} + +/** + * Insert a new message into the DB + * @param {string} message The message text + * @param {number} channel_id + * @param {number} user_id The author + */ +export const addMessage = async (message, channel_id, user_id) => { + try { + let { body } = await supabase + .from('messages') + .insert([{ message, channel_id, user_id }]) + return body + } catch (error) { + console.log('error', error) + } +} diff --git a/examples/with-supabase-auth-realtime-db/lib/UserContext.js b/examples/with-supabase-auth-realtime-db/lib/UserContext.js new file mode 100644 index 0000000000000000000000000000000000000000..ee09678f5089b80e6a793480b02f03a44f020c09 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/lib/UserContext.js @@ -0,0 +1,5 @@ +import { createContext } from 'react' + +const UserContext = createContext() + +export default UserContext diff --git a/examples/with-supabase-auth-realtime-db/package.json b/examples/with-supabase-auth-realtime-db/package.json new file mode 100644 index 0000000000000000000000000000000000000000..5b33e3198609d53b131da11e84328d26b5281e98 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/package.json @@ -0,0 +1,18 @@ +{ + "name": "realtime-chat-app", + "version": "0.1.0", + "license": "MIT", + "scripts": { + "dev": "next", + "build": "next build", + "start": "next start" + }, + "dependencies": { + "@supabase/supabase-js": "^0.35.9", + "next": "latest", + "react": "^16.13.1", + "react-dom": "^16.13.1", + "sass": "^1.26.2", + "tailwindcss": "^1.1.4" + } +} diff --git a/examples/with-supabase-auth-realtime-db/pages/_app.js b/examples/with-supabase-auth-realtime-db/pages/_app.js new file mode 100644 index 0000000000000000000000000000000000000000..8bd38f78bcc7a6a53123d9866fbde5d26d7db0fe --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/pages/_app.js @@ -0,0 +1,71 @@ +import '~/styles/style.scss' +import React from 'react' +import App from 'next/app' +import Router from 'next/router' +import UserContext from 'lib/UserContext' +import { createClient } from '@supabase/supabase-js' + +const supabase = createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL, + process.env.NEXT_PUBLIC_SUPABASE_KEY +) + +export default class SupabaseSlackClone extends App { + state = { + authLoaded: false, + user: null, + } + + componentDidMount = () => { + const user = localStorage.getItem('supabase-slack-clone') + if (user) this.setState({ user, authLoaded: true }) + else Router.push('/') + } + + signIn = async (id, username) => { + try { + let { body } = await supabase + .from('users') + .match({ username }) + .select('id, username') + const existing = body[0] + const { body: user } = existing?.id + ? await supabase + .from('users') + .update({ id, username }) + .match({ id }) + .single() + : await supabase.from('users').insert([{ id, username }]).single() + + localStorage.setItem('supabase-slack-clone', user.id) + this.setState({ user: user.id }, () => { + Router.push('/channels/[id]', '/channels/1') + }) + } catch (error) { + console.log('error', error) + } + } + + signOut = () => { + supabase.auth.logout() + localStorage.removeItem('supabase-slack-clone') + this.setState({ user: null }) + Router.push('/') + } + + render() { + const { Component, pageProps } = this.props + return ( + + + + ) + } +} diff --git a/examples/with-supabase-auth-realtime-db/pages/channels/[id].js b/examples/with-supabase-auth-realtime-db/pages/channels/[id].js new file mode 100644 index 0000000000000000000000000000000000000000..7e9bd5f808ebd5a07748e59b4b2076ced03e4958 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/pages/channels/[id].js @@ -0,0 +1,53 @@ +import Layout from '~/components/Layout' +import Message from '~/components/Message' +import MessageInput from '~/components/MessageInput' +import { useRouter } from 'next/router' +import { useStore, addMessage } from '~/lib/Store' +import { useContext, useEffect, useRef } from 'react' +import UserContext from '~/lib/UserContext' + +const ChannelsPage = (props) => { + const router = useRouter() + const { user, authLoaded, signOut } = useContext(UserContext) + const messagesEndRef = useRef(null) + + // Redirect if not signed in. + useEffect(() => { + if (authLoaded && !user) signOut() + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [user, router]) + + // Else load up the page + const { id: channelId } = router.query + const { messages, channels } = useStore({ channelId }) + + useEffect(() => { + messagesEndRef.current.scrollIntoView({ + block: 'start', + behavior: 'smooth', + }) + }, [messages]) + + // Render the channels and messages + return ( + +
    +
    +
    + {messages.map((x) => ( + + ))} +
    +
    +
    +
    + addMessage(text, channelId, user)} + /> +
    +
    + + ) +} + +export default ChannelsPage diff --git a/examples/with-supabase-auth-realtime-db/pages/index.js b/examples/with-supabase-auth-realtime-db/pages/index.js new file mode 100644 index 0000000000000000000000000000000000000000..7585c0be47dd73bd28850f7e47b8f24499abf300 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/pages/index.js @@ -0,0 +1,87 @@ +import { useState, useContext } from 'react' +import UserContext from 'lib/UserContext' +import { createClient } from '@supabase/supabase-js' + +const supabase = createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL, + process.env.NEXT_PUBLIC_SUPABASE_KEY +) + +const Home = () => { + const { signIn } = useContext(UserContext) + const [username, setUsername] = useState('') + const [password, setPassword] = useState('') + + const handleLogin = async (type, username, password) => { + try { + const { + body: { user }, + } = + type === 'LOGIN' + ? await supabase.auth.login(username, password) + : await supabase.auth.signup(username, password) + if (!!user) signIn(user.id, user.email) + } catch (error) { + console.log('error', error) + alert(error.error_description || error) + } + } + + return ( +
    +
    +
    +
    + + setUsername(e.target.value)} + /> +
    +
    + + setPassword(e.target.value)} + /> +
    + + +
    +
    +
    + ) +} + +export default Home diff --git a/examples/with-supabase-auth-realtime-db/styles/style.scss b/examples/with-supabase-auth-realtime-db/styles/style.scss new file mode 100644 index 0000000000000000000000000000000000000000..e6f0385abc078779bc0b678066367466c9295b10 --- /dev/null +++ b/examples/with-supabase-auth-realtime-db/styles/style.scss @@ -0,0 +1,28 @@ +@import '~tailwindcss/dist/base.css'; +@import '~tailwindcss/dist/components.css'; +@import '~tailwindcss/dist/utilities.css'; + +html, +body, +#__next, +.main { + max-height: 100vh; + height: 100vh; + margin: 0; + padding: 0; + overflow: hidden; +} +.channel-list { + li a:before { + content: '# '; + opacity: 0.5; + } + li a:hover { + opacity: 0.9; + } +} +.Messages { + overflow: auto; + display: flex; + flex-direction: column-reverse; +}