匠人学院 JR Academy
  • Position

    Where am I now, and where should I go?

    How your job is changing, where your experience can take you and what you still need for a target role, worked out from what you have actually done. Free, and the full result needs no sign-up.

    Analyse my career and skills

    Look up a career

    Will AI replace my job?AI impact and transition paths for common careersNew jobs in the AI eraWhat they do, what they need, how to move in

    Focused tools

    AI Engineer interview readiness30 timed questions on what you still need before interviewsCareer personality testDirections that suit how you workReturning to China checkJob prospects in China by school, major and graduation date

    AI coach

    Talk to your AI coachStill unsure after your result? Talk through what is holding you back
  • Core courses

    AI Engineer BootcampEngineering bootcamp from LLM apps to agentsAI Engineer Full-Stack Project Class16 weeks · RAG and full-stack AI projectsVibe Coding Masterclass8 weeks · direct agents to build and ship a productAI Essentials10 hours · use AI for email, reports, data and copyAI Office Automation Course8 weeks · help a whole company adopt AIOne-Person Company (OPC) Bootcamp15 weeks in person · build a one-person company with AIAll courses →

    AI engineering

    Machine Learning & Data Engineering Full-Stack ClassAI Agent & MCP Project BootcampAI Engineer Core TechniquesAI Engineer FoundationsAI Engineer Advanced: RAG to Agents and TuningClaude Code TasterAll AI engineering courses →

    AI at work

    AI Coding for everyoneClaude Code One-Day Masterclass (Non-Technical)Enterprise AI Automation BootcampAI Marketing BootcampAI Risk, Governance and Audit CourseOpenClaw 4-Week Personal AI Assistant BootcampAll AI-at-work courses →

    Career development

    AI Product Manager (BA) Bootcamp + InternshipAustralia IT Interview BootcampAustralia IT Technical Interview BootcampAustralia IT Resume BootcampAustralia Big Tech Job Search BootcampAustralia IT Career Planning BootcampAll career courses →

    Learn free first

    Vibe CodingAI for office workPrompt MasterAI EngineerAI data analysisResource centerInstructorsAll learning paths →
  • Evidence

    What will convince an employer?

    Certifications, projects, team work and competition entries, collected into one portfolio.

    See the portfolio

    Certifications

    Cert Master · all certificationsPrep and mock exams for AWS, Azure, GCP, Claude and moreAWS CLF-C02Entry-level AWS cloud certificationAWS SAA-C03Entry certification for cloud architectsClaude certificationsAnthropic Claude certification seriesAll certifications →

    Projects and team work

    P3 team projectsBuild a project with real teammates in a simulated team workflowP3 project experienceThe parts of a P3 project that go on your resumeDemo DayProject showcaseP3 certificatesProof once a project is completeAll of P3 →

    Competitions and work

    HackathonsBuild something against the clockSubmit your workAdd your project to the showcaseCareerOS portfolioDo tasks in the desktop app; your results become portfolio pieces
    Cert MasterBrowser extension, app and mini programScreenshot a question for analysis and practise anywhereAbout Cert Master
  • Connect

    How do I get myself in front of employers?

    Resume, interviews, referrals and mentors. See how far you are from the target role first, then decide what to fix.

    Start with Jobpin

    Resume and openings

    Graduate careersGraduate preparation across Australia, China, Malaysia, Singapore, the US and UKJobpinAI job search workspace: resume, roles, applicationsAI resumeRewrite your resume against a job descriptionReferralsRoles shared by students and mentorsOpportunitiesInternships, projects and collaborationsOffer boardOffers students have received

    Interviews

    AI Engineer interview guideSeven areas, with worked examplesFDE interview guideFive rounds, from scoping to the customer roundAI mock interviewPractise by text or voice and see the gapsInterview storiesReal interview experiencesSystem Design handbookPrepare for system design interviewsAll interview prep →

    Mentors

    1:1 mentorsBook AI, career and technical mentors by the hourGroup tutoringShare a tutor with a few others and pay less eachCareer coachingResume, interview and direction packages
    Jobpin desktopOne workspace for the whole job searchOrganise real experience and refine resume versions for target roles on macOS or Windows.About Jobpin
BusinessMembershipDownload CareerOS
0
匠人学院 JR Academy

Follow Us

linkedinfacebooktwitterinstagramyoutube

We Accept

/image/layout/pay-paypal.png/image/layout/pay-visa.png/image/layout/pay-master-card.png/image/layout/pay-airwallex.png/image/layout/pay-alipay.png
Airbotix · AI learning & creation for kids Another Life · AI story game
Products46

CareerOS and tools

  • CareerOS
  • Cert Master
  • Jobpin
  • Jobpin web
  • Jobpin report
  • Jobpin desktop download
  • My job search reports
  • Buy report credits
  • Jobpin billing
  • AI Toolbox
  • AI Skill Marketplace

Membership

  • Membership
  • AI membership

Courses

  • Bootcamps
  • Training Camp
  • Kanban learning
  • AI Builder course
  • MCP course
  • NotebookLM workshop
  • Business Analyst course
  • Web Full-Stack Bootcamp
  • DevOps Bootcamp
  • Data Engineering Bootcamp
  • Data Analysis Bootcamp
  • Coding for Beginners
  • Algorithm Bootcamp

Career services

  • Graduate careers
  • Graduate careers in Australia
  • Graduate careers in China
  • Graduate careers in Malaysia
  • Graduate careers in Singapore
  • Graduate careers in the US
  • Graduate careers in the UK
  • 1:1 coaching
  • Career coaching
  • AI Engineer VIP
  • BA & PM Internship
  • Data Science Internship
  • Data Analysis Internship
  • Marketing Internship
  • Resume Review
  • Interview Coaching
  • Job Application Service
  • Job Monitoring
  • LinkedIn Management
  • LinkedIn Networking
Library57

Learning paths

  • All Learning Paths
  • AI Engineer
  • Context Engineering
  • Vibe Coding
  • Prompt Master
  • AI Builder
  • AI Product Manager
  • Python Basics
  • AI Productivity
  • AI Data Analysis
  • AI Finance
  • AI Content Creation
  • AI Image Creation
  • Frontend Development
  • Hermes Agent
  • OpenClaw Local Agent

Interactive labs

  • Prompt Lab
  • Frontend Lab
  • JavaScript Lab
  • Python Lab
  • AWS Lab
  • Azure Lab
  • LLM Lab
  • Git Lab

Reference and guides

  • Resources
  • Wiki
  • Cheat sheets
  • Roadmaps
  • AI roadmaps
  • Videos
  • Ebooks
  • Blog
  • Interview handbook
  • Mock exams
  • Free resources

Cities

  • Sydney
  • Melbourne
  • Brisbane
  • Adelaide
  • Singapore
  • Kuala Lumpur
  • London
  • New York
  • San Francisco
  • Los Angeles
  • Seattle
  • Toronto
  • Vancouver

Immigration

  • Australia Immigration
  • Skilled Visa 189/190/491
  • Employer Sponsored 482/186/494
  • Business Visa 188/888
  • UK Immigration
  • US Immigration
  • Canada Immigration
  • Singapore immigration
  • Malaysia immigration
Business20

For business

  • AI for Business
  • Business portal sign-in
  • Corporate Training
  • Internship Partnership
  • Recruitment Partnership
  • Enterprise (EN)

Metatree AI Lab

  • Metatree AI Lab (Chinese)
  • AI solutions (Chinese)
  • Digital employees (Chinese)
  • Case studies (Chinese)
  • Metatree AI Lab
  • AI services
  • Digital employees
  • Case studies

AI education for kids

  • Airbotix — AI Learning & Creation for Kids
  • AU Family Resource Hub
  • NAPLAN Report Guide
  • My School Data Guide
  • Sydney Private School Fees 2026
  • Kids Coding Programs
About15

Company

  • About Us
  • Our Mentors
  • Become a Mentor
  • Metaverse Classroom
  • JR Careers
  • Another Life — AI Story Game
  • Contact Us

Help and policies

  • FAQs
  • Terms & Conditions
  • Privacy Policy
  • Cancellation & Refund Policy
  • Site map

Friend links

  • CS Browser

Contact

  • hello@jiangren.com.au
  • 0421-672-555

Offices

Level 10b, 144 Edward Street, Brisbane CBD (Headquarter)
Level 2, 171 La Trobe St, Melbourne VIC 3000
45A13, Block B, Oriental Hope Tianxiang Plaza, 500 Tianfu Avenue Middle Section, Wuhou District, Chengdu, Sichuan, China
Business Hub, 155 Waymouth St, Adelaide SA 5000
Disclaimer
footer-disclaimerfooter-disclaimer

JR Academy acknowledges Traditional Owners of Country throughout Australia and recognises the continuing connection to lands, waters and communities. We pay our respect to Aboriginal and Torres Strait Islander cultures; and to Elders past and present. Aboriginal and Torres Strait Islander peoples should be aware that this website may contain images or names of people who have since passed away.

All content on the JR Academy website, including course materials, logos, and information provided, is protected under Australian intellectual property laws. Unauthorized use, sale, distribution, reproduction, or modification is strictly prohibited. Violations may result in legal action. By accessing our website, you agree to respect our intellectual property. JR Academy Pty Ltd reserves all rights, including patents, trademarks, and copyrights. Any infringement will be subject to legal prosecution. View Terms of Service

© 2017-2026 JR Academy Pty Ltd. All rights reserved.

ABN 26621887572

HomeLearnCoursesCerts
  1. Home
  2. Learn
  3. Blog
  4. Lovable 实战手册:用 AI 对话构建真实应用 — Lovable + Supabase 完整集成:auth / database / storage / realtime
JR Academy · BlogCareer insights

Lovable 实战手册:用 AI 对话构建真实应用 — Lovable + Supabase 完整集成:auth / database / storage / realtime

从零连接 Supabase,覆盖用户认证、RLS 安全策略、文件上传、实时订阅四大模块——每个模块都有可直接复用的代码和 SQL

Published2026-11-30
Reading time4 min
Author

Contents

  • 第一步:连接 Supabase 项目
  • 在 Supabase 创建项目,拿到凭证
  • 在 Lovable 项目里接入
  • 模块一:用户认证(Auth)
  • 开启邮箱登录
  • 加 OAuth 登录(Google / GitHub)
  • 读取当前用户
  • 模块二:数据库 + Row Level Security
  • 建表和基本查询
  • Row Level Security(RLS)是什么,为什么必须开
  • Policy 类型速查
  • 常见 403 报错排查
  • 模块三:文件存储(Storage)
  • 创建 Bucket
  • 上传文件
  • 私有文件的临时访问
  • Storage 的 RLS
  • 模块四:实时订阅(Realtime)
  • 监听表变更
  • Realtime 的前置条件
  • 用 Lovable 接入 Realtime
  • 完整流程:从零搭一个 Todo 应用
  • 上线前必查清单

Lovable 自带前端生成,但真实产品离不开后端:用户账号、持久化数据、文件存储、多端同步。Supabase 是目前和 Lovable 配合最顺的后端选择——两者都走"告诉 AI 你想要什么"的思路,官方也做了原生连接器。

这一章把四个核心模块拆开讲,每个模块给出真实可用的代码和配置,不绕弯子。

Supabase 与 Lovable 集成架构:前端 ↔ Supabase API ↔ PostgreSQL + Auth + Storage + Realtime

第一步:连接 Supabase 项目

在 Supabase 创建项目,拿到凭证

登录 supabase.com,创建一个新项目(Organization → New project)。创建完成后进入:

Settings → API

你需要两个值:

  • Project URL:格式是 https://xxxxxxxxxxxx.supabase.co
  • anon key:公开匿名密钥,以 eyJ... 开头,很长

只复制 anon(public)key,绝对不要把 service_role key 粘到 Lovable。service_role 可以绕过所有 RLS 权限检查,泄露等于数据库裸奔。

在 Lovable 项目里接入

打开你的 Lovable 项目,找到右上角设置图标 → Settings → Connectors → Supabase,粘贴 Project URL 和 anon key,点 Save。出现绿色 "Connected" 状态即为成功。

连接后,Lovable 的 AI 会读取你的 Supabase 表结构和 RLS 规则——之后你说"帮我加一个 todos 表,每个用户只能看自己的数据",AI 会直接生成 SQL migration 和前端查询代码,不用你手动来回切换。


模块一:用户认证(Auth)

开启邮箱登录

Supabase 默认开启了邮箱/密码认证。开发期间建议先关掉邮件确认,不然每次测试都要去邮箱点链接:

Supabase Dashboard → Authentication → Providers → Email → 关闭 "Confirm email"

然后告诉 Lovable:

添加用户登录/注册功能:
- 邮箱 + 密码登录
- 注册后自动跳转 /dashboard
- 登录状态持久化(刷新页面不掉登录)
- 右上角加退出按钮

Lovable 会生成完整的认证逻辑,包括 supabase.auth.signUp()、signInWithPassword()、signOut() 和 session 监听。

加 OAuth 登录(Google / GitHub)

在 Supabase Dashboard 里先启用 OAuth Provider(Authentication → Providers → 选对应平台),填入该平台的 Client ID 和 Secret。

然后在 Lovable 里:

登录页加 "使用 Google 登录" 按钮,
调用 supabase.auth.signInWithOAuth({ provider: 'google' }),
回调地址配置到 /auth/callback 路由

读取当前用户

Supabase SDK 的 session 通过 supabase.auth.getSession() 获取,或者监听变化:

// 在 React 应用顶层监听 auth 状态
useEffect(() => {
  const { data: { subscription } } = supabase.auth.onAuthStateChange(
    (event, session) => {
      setUser(session?.user ?? null)
    }
  )
  return () => subscription.unsubscribe()
}, [])

模块二:数据库 + Row Level Security

建表和基本查询

Lovable 可以直接帮你生成 SQL migration。告诉它你想要的表结构,它会在 Supabase SQL Editor 里执行:

-- Lovable 生成的 todos 表示例
CREATE TABLE todos (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
  title TEXT NOT NULL,
  completed BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

前端查询同样由 Lovable 生成,但了解基本写法很有必要:

// 查询当前用户的所有 todos(RLS 会自动过滤)
const { data, error } = await supabase
  .from('todos')
  .select('*')
  .order('created_at', { ascending: false })

// 插入新 todo
const { error } = await supabase
  .from('todos')
  .insert({ title: '买咖啡', user_id: user.id })

// 更新
const { error } = await supabase
  .from('todos')
  .update({ completed: true })
  .eq('id', todoId)

Row Level Security(RLS)是什么,为什么必须开

默认情况下,Supabase 数据库对所有请求都开放——只要有 anon key,任何人都能读写你的表。这在开发时方便,但上线前必须关掉这扇门。

RLS(Row Level Security)是 PostgreSQL 的原生特性,作用是给每张表加一道"行级过滤器":每条 SQL 执行时,Postgres 都会检查当前用户是否满足 Policy 定义的条件,不满足的行直接不可见,不是返回空而是根本不存在。

开启方式:

-- 开启 RLS(必须)
ALTER TABLE todos ENABLE ROW LEVEL SECURITY;

-- 允许用户查看自己的 todos
CREATE POLICY "用户只能查看自己的 todos"
ON todos FOR SELECT
TO authenticated
USING (auth.uid() = user_id);

-- 允许用户插入自己的数据(WITH CHECK 确保 user_id 只能是自己)
CREATE POLICY "用户只能插入自己的 todos"
ON todos FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);

-- 允许用户修改自己的 todos
CREATE POLICY "用户只能更新自己的 todos"
ON todos FOR UPDATE
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);

-- 允许用户删除自己的 todos
CREATE POLICY "用户只能删除自己的 todos"
ON todos FOR DELETE
TO authenticated
USING (auth.uid() = user_id);

auth.uid() 是 Supabase 提供的函数,返回当前请求的用户 ID。未登录时返回 NULL,NULL = user_id 永远是 false,所以匿名请求拿不到任何数据。

Policy 类型速查

操作 USING WITH CHECK
SELECT ✅ 必须 ❌ 不用
INSERT ❌ 不用 ✅ 必须
UPDATE ✅ 建议 ✅ 建议
DELETE ✅ 必须 ❌ 不用

USING 是"过滤已有行的条件",WITH CHECK 是"验证写入新数据的条件"。

常见 403 报错排查

连接 Supabase 后出现 403 Forbidden 或 new row violates row-level security policy,基本是这几个原因:

  1. 表开了 RLS 但没有 Policy → 开启 RLS 等于给表加了一把没有钥匙的锁,必须显式创建 Policy 才能让用户进来
  2. Policy 里的字段名写错 → 比如 author_id 写成了 user_id,检查列名
  3. 前端没传 auth token → 确保 Supabase client 初始化时设置了 auth.persistSession: true,以及请求时 session 还有效

在 Supabase SQL Editor 里快速诊断:

-- 查看某张表的所有 Policy
SELECT policyname, cmd, qual, with_check
FROM pg_policies
WHERE tablename = 'todos';

模块三:文件存储(Storage)

创建 Bucket

在 Supabase Dashboard → Storage → Create a new bucket。

两种类型:

  • Public bucket:文件可直接通过 URL 访问,适合公开图片(产品图、封面图)
  • Private bucket:需要生成签名 URL 才能访问,适合用户私有文件(合同、隐私照片)

头像上传一般用 public bucket(avatars),用户上传的文档用 private bucket。

上传文件

// 上传用户头像
async function uploadAvatar(file: File, userId: string) {
  const fileExt = file.name.split('.').pop()
  const filePath = `${userId}/avatar.${fileExt}`

  const { data, error } = await supabase.storage
    .from('avatars')
    .upload(filePath, file, {
      upsert: true,          // 覆盖已有文件
      contentType: file.type
    })

  if (error) throw error

  // 获取公开 URL
  const { data: { publicUrl } } = supabase.storage
    .from('avatars')
    .getPublicUrl(filePath)

  return publicUrl
}

私有文件的临时访问

Private bucket 里的文件需要生成限时签名 URL:

// 生成 60 秒有效的临时访问链接
const { data, error } = await supabase.storage
  .from('private-docs')
  .createSignedUrl('contracts/agreement.pdf', 60)

if (data) {
  window.open(data.signedUrl)
}

Storage 的 RLS

Storage 同样支持 RLS Policy,通过 SQL 配置。例如,只允许用户访问自己的文件夹:

-- 允许已登录用户上传到自己的文件夹
CREATE POLICY "用户可以上传到自己的文件夹"
ON storage.objects FOR INSERT
TO authenticated
WITH CHECK (bucket_id = 'avatars' AND (storage.foldername(name))[1] = auth.uid()::text);

-- 允许公开读取 avatars bucket
CREATE POLICY "avatars 公开可读"
ON storage.objects FOR SELECT
USING (bucket_id = 'avatars');

告诉 Lovable "头像上传到 Supabase Storage 的 avatars bucket,文件路径用 userId/avatar.png,上传后更新 profiles 表的 avatar_url 字段",它会生成完整的组件代码,包括文件选择、上传进度、错误处理。


模块四:实时订阅(Realtime)

Supabase Realtime 基于 WebSocket,监听数据库表的变更并实时推送到前端。典型场景:聊天室、协作文档、实时仪表盘、通知系统。

监听表变更

// 监听 messages 表的新增消息
const channel = supabase
  .channel('messages-channel')
  .on(
    'postgres_changes',
    {
      event: 'INSERT',
      schema: 'public',
      table: 'messages'
    },
    (payload) => {
      console.log('新消息:', payload.new)
      setMessages(prev => [...prev, payload.new])
    }
  )
  .subscribe((status) => {
    if (status === 'SUBSCRIBED') {
      console.log('实时监听已就绪')
    }
  })

// 组件卸载时清理
return () => {
  supabase.removeChannel(channel)
}

支持三种事件类型:

  • INSERT — 新增行
  • UPDATE — 行被修改
  • DELETE — 行被删除
  • * — 以上三种全监听

也可以加过滤条件,只监听特定行的变更:

// 只监听当前用户的通知
.on('postgres_changes', {
  event: 'INSERT',
  schema: 'public',
  table: 'notifications',
  filter: `user_id=eq.${userId}`
}, callback)

Realtime 的前置条件

  1. Supabase Dashboard → Replication → 开启对应表的 Realtime(默认关闭)
  2. 表必须开启 RLS,且有对应的 SELECT Policy——Realtime 推送也受 RLS 过滤

用 Lovable 接入 Realtime

在 messages 页面添加实时消息功能:
- 监听 messages 表的 INSERT 事件
- 有新消息时立即追加到列表底部,不刷新整页
- 组件卸载时取消订阅

Lovable 会生成对应的 useEffect 钩子和 channel 清理逻辑。


完整流程:从零搭一个 Todo 应用

把以上四个模块组合起来,按顺序跑通一个完整应用:

第1步:
  在 Supabase 创建 todos 表,开启 RLS,加四条 CRUD Policy

第2步:
  在 Lovable 连接 Supabase,告诉 AI 表结构

第3步:
  添加邮箱登录/注册页面,登录后跳转 /todos

第4步:
  /todos 页面:查询当前用户的 todos,支持新增/完成/删除

第5步:
  监听 todos 表 INSERT 事件,多个标签页同时打开时实时同步

第6步:
  加一个头像上传,传到 Supabase Storage avatars bucket

每一步都是一条 Lovable prompt,独立可验证,出了问题精准回滚。


上线前必查清单

检查项 说明
RLS 已开启 每张表都要 ALTER TABLE xxx ENABLE ROW LEVEL SECURITY
所有表都有 Policy 开了 RLS 但没有 Policy = 无法访问任何数据
anon key 没有泄露为 service_role 看 Lovable 项目里配置的 key 以 eyJ... 开头且比 service_role 短
Storage bucket 权限配对 public bucket 不要放敏感文件;private bucket 记得建 Policy
Realtime 只在需要时开 不需要实时更新的表不要开 Replication,减少资源消耗
Email Confirm 重新开启 开发时关掉的 "Confirm email" 上线前要打开
Author
Share or copy the link
← Previous articleLovable 实战手册:用 AI 对话构建真实应用 — Lovable 常见问题 FAQ:踩坑避坑指南Next article →Lovable 实战手册:用 AI 对话构建真实应用 — 从 Lovable 到生产:自定义域名、SEO meta 和 Google Analytics

Related articles

🇦🇺测一下:袋鼠国码农能当FDE吗?

2026-12-01

Monash 研究:肠道酸度是膳食纤维降血压的关键;AI 搬运新闻冲击维州选举信息;MPAC 公布 2027 演出季

2026-12-01

Adelaide 物理学家获 $250,000 总理科学奖;全球葡萄酒消费六十年降四分之一;残障师范生实习障碍研究

2026-12-01

DVA-C03今日上线首发 · Google Cloud AI Agent认证完整解析 · AZ-401本月GA替代AZ-400

2026-12-01

Anthropic推迟下一代模型并调百人做安全,AWS推出AI办公三件套,Google让用户单独关掉AI训练

2026-12-01

GitHub Copilot 实战指南 — Copilot AI Code Review:30 秒让 AI 帮你审 PR,三档深度随需切换

2026-11-30
View all articles →

Done reading? Next steps

Not sure what to learn next? Tell us where you are

Tell us what you do now and where you want to go. See your gaps in skills, evidence, experience, expression and positioning, and what to work on first. No sign-in needed.

Compare with my situation
  • SkillAI Coding for EveryoneShip real products with Cursor + AI, no coding background needed. The 6-week Foundation track builds 4 deliverables; the 8-week Advanced track adds an Admin CMS, a chatbot and a data dashboard.See the program →
  • PositioningLearning roadmaps by roleFrontend, backend, data, AI and more, laid out stage by stage in the order to learn them.All roadmaps →
  • ExpressionTry an AI mock interviewPick a role and run a mock interview to practise explaining your experience and projects.Practise now →

Not sure what to do next?

Take this page's topic with you and see where your gaps are.

Compare with my situation
JR Academy
AI learning for the global Chinese community
BootcampsFree resourcesLearn AICareer coaching
精选推荐
AI Career Impact Map
Assess career exposure and explore transition paths and learning options
Popular tool
AI & Data Bootcamps
Build your skills through structured courses and hands-on projects
Popular courses
1:1 Career Coaching
Work with an experienced mentor on your resume and interview preparation
Career coaching
Custom Corporate Training
AI training tailored to your team
Business services
Popular tags
Vibe CodingAI codingCursorClaudeJob searchPromptFrontend developmentBackend development
Subscribe to updates

Receive AI learning resources, technical tutorials and career advice by email.

We respect your privacy and do not send spam.