D: Supabase๋ Firebase์ ๊ฐ๋ ฅํ ๋์์ผ๋ก, ์คํ์์ค ๊ธฐ๋ฐ์ ๋ฐฑ์๋ ์๋น์ค๋ฅผ ์ ๊ณตํฉ๋๋ค. ํนํ ์ธ์ฆ(Authentication) ๊ธฐ๋ฅ์ ๊ฐ๋ฐ์๋ค์ด ์ฝ๊ฒ ๋ก๊ทธ์ธ ์์คํ ์ ๊ตฌ์ถํ ์ ์๋๋ก ๋์์ค๋๋ค. ์ด๋ฒ ๊ธ์์๋ 5๋ถ ์์ Supabase๋ก ๋ก๊ทธ์ธ ์์คํ ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ๋๋ฆฌ๊ฒ ์ต๋๋ค! โจ
๐ฅ 1. Supabase ํ๋ก์ ํธ ์์ฑํ๊ธฐ
- Supabase ๊ณต์ ์ฌ์ดํธ์ ์ ์ํฉ๋๋ค.
- “Start your project” ๋ฒํผ์ ํด๋ฆญํ๊ณ , GitHub ๋๋ Google ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํฉ๋๋ค.
- “New Project”๋ฅผ ์ ํํ๊ณ , ํ๋ก์ ํธ ์ด๋ฆ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋น๋ฐ๋ฒํธ๋ฅผ ์ค์ ํฉ๋๋ค.
- (์: ํ๋ก์ ํธ ์ด๋ฆ
my-auth-app
, ์ง์ญSeoul
์ ํ)
- (์: ํ๋ก์ ํธ ์ด๋ฆ
- ์์ฑ์ด ์๋ฃ๋๋ฉด, API URL๊ณผ Anon(์ต๋ช ) Key๋ฅผ ํ์ธํฉ๋๋ค. (์ด ๊ฐ๋ค์ ๋์ค์ ํด๋ผ์ด์ธํธ์์ ์ฌ์ฉ๋ฉ๋๋ค!)
๐ 2. ์ธ์ฆ(Auth) ์ค์ ํ๊ธฐ
Supabase๋ ๋ค์ํ ๋ก๊ทธ์ธ ๋ฐฉ์์ ์ง์ํฉ๋๋ค. ๊ธฐ๋ณธ์ ์ผ๋ก ์ด๋ฉ์ผ/๋น๋ฐ๋ฒํธ ๋ก๊ทธ์ธ์ ํ์ฑํํด ๋ณด๊ฒ ์ต๋๋ค.
- ์ข์ธก ๋ฉ๋ด์์ Authentication โ Providers๋ก ์ด๋ํฉ๋๋ค.
- “Email” ์ ๊ณต์๋ฅผ ์ผ๊ณ (ํ ๊ธ ๋ฒํผ ํ์ฑํ), ํ์ํ๋ฉด “Confirm email” ์ค์ ์ ์กฐ์ ํฉ๋๋ค.
- (ํ ์คํธ ์์๋ ์ด๋ฉ์ผ ํ์ธ์ ๋๋ ๊ฒ์ด ํธ๋ฆฌํฉ๋๋ค.)
- ์ถ๊ฐ๋ก Google, GitHub ๋ก๊ทธ์ธ๋ ์ฝ๊ฒ ์ฐ๋ํ ์ ์์ต๋๋ค!
๐ป 3. ํ๋ก ํธ์๋์ Supabase ์ฐ๊ฒฐํ๊ธฐ (React ์์)
์ด์ ํด๋ผ์ด์ธํธ์์ Supabase๋ฅผ ์ฌ์ฉํด ๋ณด๊ฒ ์ต๋๋ค. React ํ๋ก์ ํธ๋ฅผ ๊ธฐ์ค์ผ๋ก ์ค๋ช ํฉ๋๋ค.
1) ํจํค์ง ์ค์น
npm install @supabase/supabase-js
2) Supabase ํด๋ผ์ด์ธํธ ์ด๊ธฐํ
src/supabaseClient.js
ํ์ผ์ ์์ฑํ๊ณ , ์๊น ํ์ธํ API URL๊ณผ Anon Key๋ฅผ ์
๋ ฅํฉ๋๋ค.
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'YOUR_SUPABASE_URL';
const supabaseKey = 'YOUR_SUPABASE_ANON_KEY';
export const supabase = createClient(supabaseUrl, supabaseKey);
3) ๋ก๊ทธ์ธ/ํ์๊ฐ์ ๊ธฐ๋ฅ ๊ตฌํ
import { supabase } from './supabaseClient';
// ํ์๊ฐ์
async function signUp(email, password) {
const { user, error } = await supabase.auth.signUp({
email: email,
password: password,
});
if (error) console.error('ํ์๊ฐ์
์ค๋ฅ:', error);
else console.log('๊ฐ์
์ฑ๊ณต:', user);
}
// ๋ก๊ทธ์ธ
async function signIn(email, password) {
const { user, error } = await supabase.auth.signIn({
email: email,
password: password,
});
if (error) console.error('๋ก๊ทธ์ธ ์ค๋ฅ:', error);
else console.log('๋ก๊ทธ์ธ ์ฑ๊ณต:', user);
}
๐ 4. ๋ก๊ทธ์ธ ์ํ ํ์ธ & ๋ก๊ทธ์์
Supabase๋ ์ค์๊ฐ์ผ๋ก ์ฌ์ฉ์ ์ํ๋ฅผ ๊ฐ์งํ ์ ์์ต๋๋ค.
// ํ์ฌ ์ฌ์ฉ์ ๊ฐ์ ธ์ค๊ธฐ
const user = supabase.auth.user();
// ๋ก๊ทธ์์
async function signOut() {
await supabase.auth.signOut();
}
// ๋ก๊ทธ์ธ ์ํ ๊ฐ์ง (์ค์๊ฐ)
supabase.auth.onAuthStateChange((event, session) => {
console.log('ํ์ฌ ์ด๋ฒคํธ:', event); // 'SIGNED_IN', 'SIGNED_OUT' ๋ฑ
console.log('์ธ์
์ ๋ณด:', session);
});
๐ฏ 5. ์ถ๊ฐ ๊ธฐ๋ฅ & ํ
- ๋น๋ฐ๋ฒํธ ์ฌ์ค์ :
supabase.auth.api.resetPasswordForEmail(email)
- ์์
๋ก๊ทธ์ธ: Google/GitHub ์ฐ๋์ Providers์์ ์ค์ ํ
supabase.auth.signIn({ provider: 'google' })
- ์ฌ์ฉ์ ์ ๋ณด ์ ์ฅ:
supabase.from('profiles').insert(...)
๋ก ์ถ๊ฐ ๋ฐ์ดํฐ ์ ์ฅ ๊ฐ๋ฅ
โ ๋ง๋ฌด๋ฆฌ
Supabase๋ฅผ ์ด์ฉํ๋ฉด 5๋ถ ์์ ์๋ฒฝํ ๋ก๊ทธ์ธ ์์คํ ์ ๊ตฌ์ถํ ์ ์์ต๋๋ค. ๐
- ์ฅ์ : ์คํ์์ค, ๋ฌด๋ฃ ์๊ธ์ , Firebase ๋๋น ์ ์ฐ์ฑ
- ์ฃผ์์ : ์ด๊ธฐ ์ค์ ์ Anon Key ๋ณด์์ ์ ์ํ์ธ์!
์ง๊ธ ๋ฐ๋ก Supabase๋ก ๋๋ง์ ์ธ์ฆ ์์คํ ์ ๋ง๋ค์ด ๋ณด์ธ์! ๐ช
๐ ์ฐธ๊ณ ์๋ฃ
ํน์ ๊ถ๊ธํ ์ ์ด ์๋ค๋ฉด ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์ธ์! โ๏ธ