Bootstrap Vue app with Supabase Auth
Guides AI models to scaffold a Vue 3 app with Supabase Auth using TypeScript and Composition API.
What this file does
Guides AI models to scaffold a Vue 3 app with Supabase Auth using TypeScript and Composition API.
When to use it
- Starting a new Vue 3 project that needs Supabase authentication
- Adding login, signout, and route protection to an existing Vue app
- Teaching an AI assistant how to generate correct Supabase Auth code
- Standardising auth implementation across multiple Vue projects
Assumes this stack
description: Guidelines for writing Vue apps with Supabase Auth globs: **/.vue, **/.ts, **/*.js
Bootstrap Vue app with Supabase Auth
Overview of implementing Supabase Auth
- Install @supabase/supabase-js package
- Configure Supabase project settings
- Create Supabase client instance
- Implement authentication UI components
- Add route protection and session management
Critical Instructions for AI Language Models
As an AI language model, you MUST follow these guidelines when implementing Supabase with Vue:
- Always use the latest @supabase/supabase-js package
- Implement proper TypeScript types for type safety
- Handle environment variables securely
- Follow Vue 3 Composition API patterns
- Implement proper error handling
Correct Client Setup
import { createClient } from '@supabase/supabase-js'
import type { Database } from '@/types/supabase'
export const supabase = createClient<Database>(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_ANON_KEY,
{
auth: {
autoRefreshToken: true,
persistSession: true
}
}
)
Correct Authentication Implementation
// composables/useAuth.ts
import { ref } from 'vue'
import { supabase } from '@/lib/supabase'
import type { User } from '@supabase/supabase-js'
export function useAuth() {
const user = ref<User | null>(null)
const loading = ref(true)
async function signIn(email: string, password: string) {
try {
const { error } = await supabase.auth.signInWithPassword({
email,
password
})
if (error) throw error
} catch (error) {
console.error('Error signing in:', error)
throw error
}
}
async function signOut() {
try {
const { error } = await supabase.auth.signOut()
if (error) throw error
} catch (error) {
console.error('Error signing out:', error)
throw error
}
}
async function getSession() {
try {
const { data: { session }, error } = await supabase.auth.getSession()
if (error) throw error
user.value = session?.user ?? null
} catch (error) {
console.error('Error getting session:', error)
user.value = null
} finally {
loading.value = false
}
}
return {
user,
loading,
signIn,
signOut,
getSession
}
}
Correct Component Usage
<script setup lang="ts">
import { ref } from 'vue'
import { useAuth } from '@/composables/useAuth'
const { user, loading, signIn, signOut } = useAuth()
const email = ref('')
const password = ref('')
const handleSignIn = async () => {
try {
await signIn(email.value, password.value)
} catch (error) {
console.error('Authentication failed:', error)
}
}
</script>
<template>
<div v-if="loading">Loading...</div>
<div v-else-if="user">
<p>Welcome, {{ user.email }}</p>
<button @click="signOut">Sign Out</button>
</div>
<form v-else @submit.prevent="handleSignIn">
<input v-model="email" type="email" required />
<input v-model="password" type="password" required />
<button type="submit">Sign In</button>
</form>
</template>
Route Protection Implementation
import { createRouter } from 'vue-router'
import { supabase } from '@/lib/supabase'
const router = createRouter({
// ... your routes configuration
})
router.beforeEach(async (to, from, next) => {
const { data: { session } } = await supabase.auth.getSession()
if (to.meta.requiresAuth && !session) {
next({ name: 'login' })
} else {
next()
}
})
Environment Variables Setup
Create a .env.local file with:
VITE_SUPABASE_URL=your-project-url
VITE_SUPABASE_ANON_KEY=your-anon-key
AI Model Verification Steps
Before generating any code, you MUST verify:
- Is TypeScript properly configured?
- Are environment variables properly handled?
- Is error handling implemented?
- Are authentication state and user data properly typed?
- Is session management configured correctly?
Consequences of Incorrect Implementation
If you generate code incorrectly:
- Type safety will be compromised
- Authentication flows may fail
- Security vulnerabilities may be introduced
- Session management may be unreliable
- User data may be exposed
AI Model Response Template
When implementing Supabase Auth for Vue, you MUST:
- Use TypeScript for type safety
- Implement proper error handling
- Follow Vue 3 Composition API patterns
- Configure secure session management
- Handle environment variables properly
What's inside
8 sections covering setup, client config, auth composable, component, route guard, env vars, verification steps, and response template
Change this for your project
- Replace
@/types/supabasewith your own generated Supabase types path - Replace
@/lib/supabasewith your actual client module path - Replace
@/composables/useAuthwith your composable file location - Replace
your-project-urlandyour-anon-keyin.env.localwith real Supabase credentials
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Separating auth logic into a composable keeps components clean and testable
- Using a router guard with
to.meta.requiresAuthcentralises access control
Related Documents
How you work
Defines personality, planning, task execution, and communication conventions for a coding agent in the Codex CLI environment.
How you work
Defines personality, planning, task execution, and communication guidelines for a coding agent in Codex CLI.
内置 Agent 提示词
Documents the system prompts, tool permissions, and model assignments for six built-in subagents in Claude Code.
perplexity-ai_20250112
Defines the system prompt for Perplexity AI, instructing the assistant on how to answer queries using search results with strict formatting, citation, and query-type rules.