implementing robust authentication in nuxt.js with supabase auth
building a modern full stack application requires a secure and scalable way to handle user identities. for developers using nuxt.js, integrating supabase auth is one of the most efficient ways to implement robust authentication without spending weeks writing backend boilerplate. in this guide, we will walk through the entire process of setting up a secure auth system, focusing on best practices for coding and devops.
why choose supabase for nuxt.js?
supabase is an open-source firebase alternative that provides a complete backend suite. when it comes to authentication, it offers several advantages for the modern engineer:
- ease of setup: you can go from zero to a working login system in minutes.
- multiple providers: support for email/password, magic links, and social oauth (google, github, etc.).
- row level security (rls): direct integration with postgresql allows you to control data access at the database level, a crucial part of devops security.
- nuxt module: the official
@nuxtjs/supabasemodule simplifies the integration process significantly.
step 1: setting up your supabase project
before diving into the coding part, you need a supabase project. follow these steps:
- go to supabase.com and create a free account.
- create a new project and note down your project url and anon key.
- navigate to the authentication tab in your dashboard to configure your providers (e.g., enabling email confirmation).
step 2: installing the supabase module in nuxt
to integrate supabase into your nuxt project, install the official module via your terminal:
npm install @nuxtjs/supabase
# or
yarn add @nuxtjs/supabase
next, add the module to your nuxt.config.ts file to enable it globally across your application:
export default definenuxtconfig({
modules: ['@nuxtjs/supabase'],
supabase: {
redirect: false // we set this to false to handle redirects manually for better ux
}
})
finally, create a .env file in your root directory to store your credentials securely. never commit your secret keys to github!
supabase_url=https://your-project-id.supabase.co
supabase_key=your-anon-public-key
step 3: creating the authentication logic
now, let's implement the actual functionality. we will use the usesupabaseclient and usesupabaseuser composables provided by the module.
implementing sign-up and login
here is a simplified example of how to handle user authentication in a nuxt component:
<script setup>
const client = usesupabaseclient()
const email = ref('')
const password = ref('')
async function handlesignup() {
const { data, error } = await client.auth.signup({
email: email.value,
password: password.value,
})
if (error) alert(error.message)
else alert('check your email for the confirmation link!')
}
async function handlelogin() {
const { error } = await client.auth.signinwithpassword({
email: email.value,
password: password.value,
})
if (error) alert(error.message)
else navigateto('/dashboard')
}
</script>
handling user sessions and logout
to ensure a smooth user experience, you need to be able to sign users out and check if they are currently authenticated:
<script setup>
const client = usesupabaseclient()
const user = usesupabaseuser()
async function handlelogout() {
const { error } = await client.auth.signout()
if (error) console.error('logout failed:', error.message)
else navigateto('/login')
}
</script>
step 4: protecting routes with middleware
a robust authentication system isn't complete without protected routes. you don't want unauthenticated users accessing your /dashboard or /profile pages.
create a file named auth.ts in your middleware/ folder:
export default definenuxtroutemiddleware((to, from) => {
const user = usesupabaseuser()
// if user is not logged in, redirect to login page
if (!user.value) {
return navigateto('/login')
}
})
to apply this middleware to a page, simply add this to your page component:
<script setup>
definepagemeta({
middleware: 'auth'
})
</script>
step 5: database security (the devops perspective)
implementing a login form is only half the battle. to truly secure your full stack app, you must use row level security (rls). without rls, anyone with your public anon key could potentially read or delete your data.
in your supabase sql editor, you can create a policy that allows users to access only their own data:
-- enable rls on the profiles table
alter table profiles enable row level security;
-- create a policy: users can only see their own profile
create policy "user can view own profile"
on profiles for select
using (auth.uid() = id);
conclusion and seo tips for developers
by combining nuxt.js and supabase, you've built a professional-grade authentication system that is secure, scalable, and easy to maintain. for those looking to improve the seo of their authenticated apps, remember that while dashboard pages are hidden from search engines, your landing pages and public profiles should be optimized with proper meta tags and server-side rendering (ssr).
key takeaways for your workflow:
- use environment variables for all keys.
- always implement middleware to protect sensitive routes.
- never trust the frontend; always enforce rls on the backend.
- keep your coding clean by utilizing nuxt composables.
Comments
Share your thoughts and join the conversation
Loading comments...
Please log in to share your thoughts and engage with the community.