31
loading...
This website collects cookies to deliver better user experience
npm install -D tailwindcss@latest postcss@latest autoprefixer@latest
npx tailwindcss init -p
tailwind.config.js
file.purge: ['./pages/**/*.{js,ts,jsx,tsx}', './components/**/*.{js,ts,jsx,tsx}'],
styles/globals.css
to look like this:@import 'tailwindcss/base';
@import 'tailwindcss/components';
@import 'tailwindcss/utilities';
pages/index.js
file and import the dependencies we need.import { useEffect, useState } from 'react';
import { supabase } from '../lib/initSupabase';
export default function Home() {
const [session, setSession] = useState(null);
useEffect(() => {
setSession(supabase.auth.session());
supabase.auth.onAuthStateChange((_event, session) => {
setSession(session);
});
}, []);
return <main>{!session ? 'Show login' : 'show profile'}</main>;
}
[]
at the end of it.Login.js
file to your components directory.import { useState } from 'react';
import { supabase } from '../lib/initSupabase';
export default function Login() {
const [email, setEmail] = useState('');
const handleLogin = async (email) => {
try {
const { error } = await supabase.auth.signIn({ email });
if (error) throw error;
alert('Check your email for the login link!');
} catch (error) {
alert(error.error_description || error.message);
}
};
return (
<div className='container mx-auto grid place-content-center min-h-screen'>
<p className='mb-4'>Sign in via magic link with your email below</p>
<input
className='mb-4 border-2 border-gray-500 rounded-xl p-4 w-full'
type='email'
placeholder='Your email'
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
<button
onClick={(e) => {
e.preventDefault();
handleLogin(email);
}}
className='w-full mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'
>
<span>Send magic link</span>
</button>
</div>
);
}
handleLogin
function.Profile.js
file in the components directory.import { supabase } from '../lib/initSupabase';
export default function Profile({ session }) {
return (
<div className='container mx-auto grid place-content-center min-h-screen'>
<p>Oh hi there {session.user.email}</p>
<button
className='mt-4 p-2 pl-5 pr-5 bg-blue-500 text-gray-100 text-lg rounded-lg focus:border-4 border-blue-300'
onClick={() => supabase.auth.signOut()}
>
Logout
</button>
</div>
);
}
index.js
page and import the two new components like so:import Login from '../components/Login';
import Profile from '../components/Profile';
return <main>{!session ? <Login /> : <Profile session={session} />}</main>;
Note: Did you note we are passing the session here to the profile component?