Wakambogamuchira pasuru yemuvakidzani wako pamusuwo wako wepamberi here? (Zvichida wakaivhura netsaona?) Pamwe wakambova nevoicemail inonzwisa tsitsi yasiirwa mumwe munhu? Sevagadziri vezvishandiso, ibasa rako kuona kuti data rakavanzika rakachengetwa mukushandisa kwako hariunzwe netsaona kune rimwe bato.
Kune akawanda matekiniki anowanikwa kuchengetedza zvakachengeteka data revatengi, uye mazhinji akaomarara uye akaoma kuita. Zvakanaka, munhu anogona kuchengetedza data rese revatengi mune imwe dhatabhesi - kuchengetedza dhizaini yechinhu chiri nyore uye chakachengeteka.
Row-level chengetedzo (RLS) kugona kuchengetedza nekudzora kupinda kune yakatarwa mitsara yedata mukati metafura yedatabase. Icho chishandiso chine simba chinokutendera kuti uchengetedze ese mutengi wako data mune imwechete dhatabhesi pasina kunetsekana nezvekuburitswa kwedata mumaakaundi ese. Nekudaro, kuita nemazvo RLS inogona kuve inonyengera maitiro inosanganisira kubatanidza ruzivo rwekupinda nemvumo yako yedatabase. Neon Mvumo inokwenenzvera kuita uku nekubatanidza otomatiki huchokwadi kubva kune wako OAuth mupi nePostgreSQL dhatabhesi yako.
Neon Authorize inoshandisa yako iripo yekusimbisa layer kuti uone wese anopinda-mukati mushandisi uye inosanganisa data rese riri mudhatabhesi rako nemagwaro avo ekupinda. Izvi zvinovimbisa kuti data yakachengetwa mudhatabhesi inogona kuwanikwa chete nevashandisi-vakapinda-uye kuti vashandisi-vakapinda chete vanogona kuona data ravo.
Ichi chidzidzo chinokufamba iwe kuburikidza nekugadzira Remix app uchishandisa Clerk seyekusimbisa layer. Clerk ndeye yakakurumbira mushandisi yekusimbisa uye manejimendi chishandiso. Iwe unozoshandisa Neon Postgres seyako data layer uye kuwedzera Neon Mvumo kuchengetedza data rese kumutengi wega wega wakapinda. Mutsara wega wega patafura ucharatidza userID, iyo inopihwa naClerk. Ivo chete vakatenderwa neiyo userID vanogona kudyidzana nedata mumutsara.
Yedu yekuenzanisira application iri nyore-inorekodha yega yega kupinda muRLS dhatabhesi, uchishandisa iyo userID. Kana iyo peji yaremerwa, ekupedzisira gumi logins yemushandisi ane chokwadi icharatidzwa, uye hapana imwe data yemushandisi (yakachengetwa muiyo tafura yePostgreSQL) ichaonekwa. Ngatitangei!
Tanga nekugadzira Remix application uye nekuisa zvinotsamira uchishandisa iyo kodhi snippet pazasi. Kuti uwane mamwe mirairo yakadzama, tarisa kuRemix yekukurumidza kutanga gwara .
##make a directory and initialise your NPM project mkdir neon-authorize-remix-clerk-app cd neon-authorize-remix-clerk-app npm init -y ## install runtime dependecies for Remix npm i @remix-run/node @remix-run/react @remix-run/serve isbot@4 react react-dom @remix-run/router drizzle-orm npm install @neondatabase/serverless npm install @clerk/remix npm i -D @remix-run/dev vite
Sezvo Remix inoshandisa Vite, Javascript kuvaka chishandiso, gadzira vite.config.js
mumudziyo dhairekitori:
import { vitePlugin as remix } from "@remix-run/dev"; import { defineConfig } from "vite"; export default defineConfig({ plugins: [remix()], });
Tisati taita chero budiriro, tinoda kugadzira maakaundi kuClerk neNeon kushandisa masevhisi avo:
Pinda kuClerk dashboard kuti ugadzire chirongwa chitsva.
Mukufamba kwekuruboshwe, sarudza API Keys .
.env
mukodhi yako.Mukufamba kwekuruboshwe, sarudza “ JWT matemplate. ”
Pinda muNeon console uye gadzira chirongwa chitsva.
Kubva kuruboshwe rwekutenderera menyu, sarudza Gumisa .
Gadzira mupi mutsva uye unamate iyo Clerk JWKS URL yawakakopa kubva kuna Clerk kare.
Paunenge wagadzira chiitiko, tinya "Tanga." Penera repadivi rinovhura nematanho akatevedzana kuti upedze kusanganisa kwako Neon Authorize.
Iyo yekutanga kuseta inokupa iwe matanho ekumisikidza yakakosha Kubvumidza chirongwa naClerk.
1. Set up Neon Extension and Roles Privileges. Run these steps in the Dashboard. 2. Grant privileges to the roles in the neondb database.
Iyo kodhi yakapihwa ndeye todos app. Panzvimbo pekushandisa yakapihwa boilerplate kodhi kubva kuNeon kune todos app, isu tichagadzira login_history
tafura uye kumisikidza RLS pairi. Vhura iyo SQL Mharidzo muNeon dashboard uye mhanyisa kodhi pazasi. Iyo login_history
tafura ichashandiswa kuchengetedza nguva dzekupinda kumushandisi wega wega.
Ziva kuti
login_history
ine makoramu matatu chete: iyo id, user_id, uye login_at. Makoramu maviri ekupedzisira acharatidza achangoburwa logins mukushandisa.
CREATE TABLE login_history ( id bigint generated by default as identity primary key, user_id text not null default (auth.user_id()), login_at timestamp not null default now() ); -- 1st enable row level security for your table ALTER TABLE login_history ENABLE ROW LEVEL SECURITY; -- 2nd create policies for your table CREATE POLICY "Individuals can add login." ON login_history FOR INSERT TO authenticated WITH CHECK ((select auth.user_id()) = user_id); CREATE POLICY "Individuals can view their own logins. " ON login_history FOR SELECT TO authenticated USING ((select auth.user_id()) = user_id);
Wedzera zvakapihwa zvakatipoteredza zvinosiyana kune yako .env
Kana aya matanho ekuseta apera, yako .env
inofanira kuva nemhando ina: maviri kubva kuna Clerk uye maviri kubva kuNeon:
CLERK_PUBLISHABLE_KEY=pk_test_.... CLERK_SECRET_KEY=sk_test_... # Database owner connection string DATABASE_URL='postgresql://neondb_owner:...' # Neon "authenticated" role connection string DATABASE_AUTHENTICATED_URL='postgresql://authenticated@ep-...
Iyo application ikozvino yagadzirira kuvakwa. Iyo yakazara kodhi inowanikwa paGitHub , asi iyo inonyanya kukosha maficha inoratidzwa pano. Mhedzisiro yechishandiso iri app/routes/_index.tsx
:
export const loader: LoaderFunction = async (args) => { const { userId, getToken } = await getAuth(args); if (!userId) { return redirect("/sign-in"); } const authToken = await getToken(); console.log(userId); if (!authToken) { return null; } const DATABASE_AUTHENTICATED_URL= process.env.NEXT_PUBLIC_DATABASE_AUTHENTICATED_URL; try { const sql = neon(DATABASE_AUTHENTICATED_URL ?? '', { authToken, }); const loginResponse = await sql(`INSERT INTO login_history ("user_id") VALUES ($1) RETURNING *`,[userId]); // Retrieve last 10 logins const last10LoginsResponse = await sql(`SELECT * FROM login_history WHERE user_id = $1 ORDER BY login_at DESC LIMIT 10`, [userId]); console.log(`loginResponse: ${JSON.stringify(loginResponse)}`); return last10LoginsResponse as Array<LoginHistory>; } catch (error) { console.error(`Error inserting into login_history table: ${error.message}`); console.error(`Error details: ${JSON.stringify(error)}`); throw error; } }
Iyo LoaderFunction
mu _index.tsx
faira inopedza mabasa paserver isati yapa peji kumutengi. Muchishandiso ichi, chinorodha chinoita zvakawanda zvekusimudza zvinorema zveapp.
Basa racho rinotanga ratarisa kana mushandisi asina kupinda mukati uye rozodzosera mushandisi ku /sign-in
peji. Peji yekusaina inogona kugadziridzwa muClerk dashboard kuti igamuchire marudzi akasiyana ekupinda, akadai seGoogle uye email logins:
Kuti ugadzire peji rekusaina, famba uchienda kuClerk dashboard uye gadzira nzira dzinodiwa dzekupinda dzepurojekiti.
Kana mushandisi apinda mukati, basa racho rinotora userId
uye authToken
kubva kuna Clerk. Aya makoshero akakosha kuti uve nechokwadi chekuti mushandisi apinzwa mukati, uyezve unogona kushandisa userId
kuzara mutsetse wega wega mudhatabhesi rako.
Kuti uite shanduko kuRLS-yakachengetedzwa dhatabhesi, unofanirwa kudhonza iyo DATABASE_AUTHENTCATED_URL
kubva kune zvakatipoteredza zvinosiyana.
Iyo yakakosha pfungwa yekushandisa RLS kuchengetedza iri mukati LoaderFunction
. Muenzaniso weSQL Neon unotangwa uchishandisa machinjiro ezvakatipoteredza uye chiratidzo chechokwadi. Iyo loginResponse
basa rinoita SQL kufona uye inoisa iyo user_id (uye nguva iripo) muPostgreSQL dhatabhesi, mushure meiyo last10LoginsResponse
basa rinobvunza iyo DB yegumi ichangoburwa logins.
Pakupedzisira, iyo last10LoginsResponse
inodzoswa kubva kune yekurodha basa.
Index()
basa mu _index.tsx
faira rinopa marongerwo e peji sezviri kuratidzwa muchidimbu chiri pazasi:
export default function Index() { const logins = useLoaderData(); return ( <div> <h1>Signed in</h1> <p>You are signed in!</p> <p> <UserButton /></p> <div> <h1>Recent Logins</h1> {logins?.map((logins) => ( <li key={logins.id}> {logins.user_id} login at: {logins.login_at} </li> ))} </div> <p>< SignOutButton > Sign Out</ SignOutButton ></p> </div> ); }
Iyo kodhi iri pamusoro inotora mhinduro kubva LoaderFunction
, iyo ine ekupedzisira gumi ekupinda ekupinda. Mhinduro iyi inovaka peji inoudza mushandisi kuti vakasaina, inonyora mazita avo ekupedzisira gumi, uye inoratidza bhatani reKubuda sezvakaratidzwa pazasi:
Mumuenzaniso uyu, iyo user_id
inoratidzwawo kuratidza zvakajeka kuti chete data rekupinda remushandisi akanyorwa-mukati rinoonekwa.
Uchishandisa hwindo re incognito, unogona kupinda neyechipiri Google account, uye tarisa parutivi-ne-parutivi data kune vakasiyana vashandisi:
Ziva kuti nguva dzekupinda dzinopindirana, asi nekushandisa Row-Level Chengetedzo mudhatabhesi, unodzivirira kudonha kwedata mumaakaundi ese. Mitsara inogona chete kutorwa uye kuratidzwa kune anotenderwa mushandisi.
Kuchengeta data pachivande inyaya yakakosha yekushandisa. Sezvo maapplication anowanzo chengetedza ruzivo rwepachivande, inofanirwa kuchengetedzwa kuchengetedza data iri mumaoko ekurudyi. Vatengi vane dziviriro yepamutemo yakawedzera seGDPR, uye maturusi akaita Neon Authorize anoita kuti zvive nyore kuita Row Level Chengetedzo kuchengetedza data remutengi wako.
Mune ino post, takafamba nematanho anodiwa kugonesa Row Level Security muNeon dhatabhesi. Kushandisa RLS nedata remutengi wedu kunovimbisa kuti chete mushandisi akanyorwa-mukati ane zvitupa zvekubvisa yavo data.
Wedzera Row Layer Chengetedzo kune yako app nhasi neNeon.