[{"data":1,"prerenderedAt":703,"buildId":704},["ShallowReactive",2],{"docs-navigation-en":3,"example-\u002Fexamples\u002Fnuxt-security-rate-limit":50,"example-surround-\u002Fexamples\u002Fnuxt-security-rate-limit":530,"examples-en":540},[4],{"title":5,"path":6,"stem":7,"children":8,"page":49},"Docs","\u002Fdocs","docs",[9,13,17,21,25,29,33,37,41,45],{"title":10,"path":11,"stem":12},"Getting started","\u002Fdocs\u002Fgetting-started","docs\u002F01.getting-started",{"title":14,"path":15,"stem":16},"verifyUser","\u002Fdocs\u002Fverify-user","docs\u002F02.verify-user",{"title":18,"path":19,"stem":20},"Sites without users","\u002Fdocs\u002Fsites-without-users","docs\u002F03.sites-without-users",{"title":22,"path":23,"stem":24},"Build-time route check","\u002Fdocs\u002Froute-coverage","docs\u002F04.route-coverage",{"title":26,"path":27,"stem":28},"Public routes","\u002Fdocs\u002Fpublic-routes","docs\u002F05.public-routes",{"title":30,"path":31,"stem":32},"Same-origin writes","\u002Fdocs\u002Fsame-origin","docs\u002F06.same-origin",{"title":34,"path":35,"stem":36},"Scope and caching","\u002Fdocs\u002Fscope","docs\u002F07.scope",{"title":38,"path":39,"stem":40},"Configuration","\u002Fdocs\u002Fconfiguration","docs\u002F08.configuration",{"title":42,"path":43,"stem":44},"Migrating an existing BFF guard","\u002Fdocs\u002Fmigration","docs\u002F09.migration",{"title":46,"path":47,"stem":48},"Contributing","\u002Fdocs\u002Fcontributing","docs\u002F10.contributing",false,{"id":51,"title":52,"body":53,"category":518,"description":519,"extension":520,"meta":521,"navigation":522,"order":85,"path":523,"seo":524,"stem":525,"uses":526,"__hash__":529},"examples_en\u002Fexamples\u002Fnuxt-security-rate-limit.md","Rate limits with nuxt-security",{"type":54,"value":55,"toc":515},"minimark",[56,71,487,490,495,511],[57,58,59,66,67,70],"p",{},[60,61,65],"a",{"href":62,"rel":63},"https:\u002F\u002Fnuxt-security.vercel.app",[64],"nofollow","nuxt-security"," adds its rate limiter as a server middleware. Nitro runs middleware before route handlers, so an over-limit request is answered with 429 before ",[68,69,14],"code",{}," makes a single call to your auth provider.",[72,73,79],"pre",{"className":74,"code":75,"filename":76,"language":77,"meta":78,"style":78},"language-ts shiki shiki-themes material-theme-lighter github-light github-dark","export default defineNuxtConfig({\n  modules: ['nuxt-security', '@kurashina-design\u002Fnuxt-bff-guard'],\n  bffGuard: {\n    verifyUser: '.\u002Fserver\u002Fauth\u002Fverify-user.ts',\n  },\n  security: {\n    rateLimiter: {\n      tokensPerInterval: 150,\n      interval: 300_000, \u002F\u002F 5 minutes\n      headers: true, \u002F\u002F x-ratelimit-* response headers\n      \u002F\u002F Behind a proxy, name the header it sets. Otherwise the client's own\n      \u002F\u002F X-Forwarded-For decides the bucket.\n      ipHeader: 'x-real-ip',\n      driver: { name: 'redis', options: { url: process.env.REDIS_URL } },\n    },\n  },\n  routeRules: {\n    \u002F\u002F Public sign-in routes get a much smaller bucket.\n    '\u002Fapi\u002Fsession': {\n      security: { rateLimiter: { tokensPerInterval: 5, interval: 60_000 } },\n    },\n    '\u002Fapi\u002Fauth\u002F**': {\n      security: { rateLimiter: { tokensPerInterval: 10, interval: 60_000 } },\n    },\n  },\n})\n","nuxt.config.ts","ts","",[68,80,81,105,144,155,172,178,188,198,212,229,246,252,258,275,333,339,344,354,360,376,415,420,434,468,473,478],{"__ignoreMap":78},[82,83,86,90,93,97,101],"span",{"class":84,"line":85},"line",1,[82,87,89],{"class":88},"sVHd0","export",[82,91,92],{"class":88}," default",[82,94,96],{"class":95},"sGLFI"," defineNuxtConfig",[82,98,100],{"class":99},"su5hD","(",[82,102,104],{"class":103},"sP7_E","{\n",[82,106,108,112,115,118,122,125,127,130,133,136,138,141],{"class":84,"line":107},2,[82,109,111],{"class":110},"skxfh","  modules",[82,113,114],{"class":103},":",[82,116,117],{"class":99}," [",[82,119,121],{"class":120},"sjJ54","'",[82,123,65],{"class":124},"s_sjI",[82,126,121],{"class":120},[82,128,129],{"class":103},",",[82,131,132],{"class":120}," '",[82,134,135],{"class":124},"@kurashina-design\u002Fnuxt-bff-guard",[82,137,121],{"class":120},[82,139,140],{"class":99},"]",[82,142,143],{"class":103},",\n",[82,145,147,150,152],{"class":84,"line":146},3,[82,148,149],{"class":110},"  bffGuard",[82,151,114],{"class":103},[82,153,154],{"class":103}," {\n",[82,156,158,161,163,165,168,170],{"class":84,"line":157},4,[82,159,160],{"class":110},"    verifyUser",[82,162,114],{"class":103},[82,164,132],{"class":120},[82,166,167],{"class":124},".\u002Fserver\u002Fauth\u002Fverify-user.ts",[82,169,121],{"class":120},[82,171,143],{"class":103},[82,173,175],{"class":84,"line":174},5,[82,176,177],{"class":103},"  },\n",[82,179,181,184,186],{"class":84,"line":180},6,[82,182,183],{"class":110},"  security",[82,185,114],{"class":103},[82,187,154],{"class":103},[82,189,191,194,196],{"class":84,"line":190},7,[82,192,193],{"class":110},"    rateLimiter",[82,195,114],{"class":103},[82,197,154],{"class":103},[82,199,201,204,206,210],{"class":84,"line":200},8,[82,202,203],{"class":110},"      tokensPerInterval",[82,205,114],{"class":103},[82,207,209],{"class":208},"srdBf"," 150",[82,211,143],{"class":103},[82,213,215,218,220,223,225],{"class":84,"line":214},9,[82,216,217],{"class":110},"      interval",[82,219,114],{"class":103},[82,221,222],{"class":208}," 300_000",[82,224,129],{"class":103},[82,226,228],{"class":227},"sutJx"," \u002F\u002F 5 minutes\n",[82,230,232,235,237,241,243],{"class":84,"line":231},10,[82,233,234],{"class":110},"      headers",[82,236,114],{"class":103},[82,238,240],{"class":239},"syTEX"," true",[82,242,129],{"class":103},[82,244,245],{"class":227}," \u002F\u002F x-ratelimit-* response headers\n",[82,247,249],{"class":84,"line":248},11,[82,250,251],{"class":227},"      \u002F\u002F Behind a proxy, name the header it sets. Otherwise the client's own\n",[82,253,255],{"class":84,"line":254},12,[82,256,257],{"class":227},"      \u002F\u002F X-Forwarded-For decides the bucket.\n",[82,259,261,264,266,268,271,273],{"class":84,"line":260},13,[82,262,263],{"class":110},"      ipHeader",[82,265,114],{"class":103},[82,267,132],{"class":120},[82,269,270],{"class":124},"x-real-ip",[82,272,121],{"class":120},[82,274,143],{"class":103},[82,276,278,281,283,286,289,291,293,296,298,300,303,305,307,310,312,315,318,321,323,327,330],{"class":84,"line":277},14,[82,279,280],{"class":110},"      driver",[82,282,114],{"class":103},[82,284,285],{"class":103}," {",[82,287,288],{"class":110}," name",[82,290,114],{"class":103},[82,292,132],{"class":120},[82,294,295],{"class":124},"redis",[82,297,121],{"class":120},[82,299,129],{"class":103},[82,301,302],{"class":110}," options",[82,304,114],{"class":103},[82,306,285],{"class":103},[82,308,309],{"class":110}," url",[82,311,114],{"class":103},[82,313,314],{"class":99}," process",[82,316,317],{"class":103},".",[82,319,320],{"class":99},"env",[82,322,317],{"class":103},[82,324,326],{"class":325},"s_hVV","REDIS_URL",[82,328,329],{"class":103}," }",[82,331,332],{"class":103}," },\n",[82,334,336],{"class":84,"line":335},15,[82,337,338],{"class":103},"    },\n",[82,340,342],{"class":84,"line":341},16,[82,343,177],{"class":103},[82,345,347,350,352],{"class":84,"line":346},17,[82,348,349],{"class":110},"  routeRules",[82,351,114],{"class":103},[82,353,154],{"class":103},[82,355,357],{"class":84,"line":356},18,[82,358,359],{"class":227},"    \u002F\u002F Public sign-in routes get a much smaller bucket.\n",[82,361,363,366,370,372,374],{"class":84,"line":362},19,[82,364,365],{"class":120},"    '",[82,367,369],{"class":368},"sZUrc","\u002Fapi\u002Fsession",[82,371,121],{"class":120},[82,373,114],{"class":103},[82,375,154],{"class":103},[82,377,379,382,384,386,389,391,393,396,398,401,403,406,408,411,413],{"class":84,"line":378},20,[82,380,381],{"class":110},"      security",[82,383,114],{"class":103},[82,385,285],{"class":103},[82,387,388],{"class":110}," rateLimiter",[82,390,114],{"class":103},[82,392,285],{"class":103},[82,394,395],{"class":110}," tokensPerInterval",[82,397,114],{"class":103},[82,399,400],{"class":208}," 5",[82,402,129],{"class":103},[82,404,405],{"class":110}," interval",[82,407,114],{"class":103},[82,409,410],{"class":208}," 60_000",[82,412,329],{"class":103},[82,414,332],{"class":103},[82,416,418],{"class":84,"line":417},21,[82,419,338],{"class":103},[82,421,423,425,428,430,432],{"class":84,"line":422},22,[82,424,365],{"class":120},[82,426,427],{"class":368},"\u002Fapi\u002Fauth\u002F**",[82,429,121],{"class":120},[82,431,114],{"class":103},[82,433,154],{"class":103},[82,435,437,439,441,443,445,447,449,451,453,456,458,460,462,464,466],{"class":84,"line":436},23,[82,438,381],{"class":110},[82,440,114],{"class":103},[82,442,285],{"class":103},[82,444,388],{"class":110},[82,446,114],{"class":103},[82,448,285],{"class":103},[82,450,395],{"class":110},[82,452,114],{"class":103},[82,454,455],{"class":208}," 10",[82,457,129],{"class":103},[82,459,405],{"class":110},[82,461,114],{"class":103},[82,463,410],{"class":208},[82,465,329],{"class":103},[82,467,332],{"class":103},[82,469,471],{"class":84,"line":470},24,[82,472,338],{"class":103},[82,474,476],{"class":84,"line":475},25,[82,477,177],{"class":103},[82,479,481,484],{"class":84,"line":480},26,[82,482,483],{"class":103},"}",[82,485,486],{"class":99},")\n",[57,488,489],{},"The limiter counts per IP and per matched rule, so every path under one rule shares a bucket. Its default storage is an in-memory LRU cache, which is per process; use a shared driver such as Redis when you run more than one instance.",[491,492,494],"h2",{"id":493},"why-before-the-guard","Why before the guard",[496,497,498,502,508],"ul",{},[499,500,501],"li",{},"A flood of invalid tokens costs nothing at your auth provider.",[499,503,504,505,507],{},"Public sign-in routes, which skip ",[68,506,14],{},", are limited too.",[499,509,510],{},"Your handler's upstream calls are protected from abuse by signed-in users as well.",[512,513,514],"style",{},"html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .syTEX, html code.shiki .syTEX{--shiki-light:#FF5370;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sZUrc, html code.shiki .sZUrc{--shiki-light:#E53935;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":78,"searchDepth":107,"depth":107,"links":516},[517],{"id":493,"depth":107,"text":494},"security","Slow down credential stuffing and token guessing before verifyUser runs.","md",{},true,"\u002Fexamples\u002Fnuxt-security-rate-limit",{"title":52,"description":519},"examples\u002Fnuxt-security-rate-limit",[65,527,528],"rateLimiter","routeRules","DUDBwQr9R8Xp6249vkf6j4cUVjIzAlvmXOpBiXk5c0E",[531,536],{"path":532,"title":533,"description":534,"category":535,"order":180},"\u002Fexamples\u002Fjwt-jose","Any OpenID Connect provider (JWT)","Verify access tokens from Auth0, Keycloak, Cognito, Entra ID and others with jose.","auth",{"path":537,"title":538,"description":539,"category":518,"order":107},"\u002Fexamples\u002Fnuxt-security-headers","Headers, CSP and request limits with nuxt-security","A baseline nuxt-security setup next to the guard, tuned for a BFF.",[541,549,555,562,567,573,581,589,596,604,609,611,616,623,629,636,644,650,658,664,672,678,684,689,696],{"path":542,"title":543,"description":544,"category":545,"order":85,"uses":546},"\u002Fexamples\u002Fcontact-form","Contact form on a corporate site","A mail-sending route that only your own pages can call, with a bot check and a rate limit.","visitor",[547,548,65],"access: 'visitor'","@nuxtjs\u002Fturnstile",{"path":550,"title":551,"description":552,"category":545,"order":107,"uses":553},"\u002Fexamples\u002Fpaid-api-proxy","Search box that calls a paid API","Keep other sites and scripts from spending your API quota through your BFF.",[547,527,554],"defineCachedFunction",{"path":556,"title":557,"description":558,"category":545,"order":146,"uses":559},"\u002Fexamples\u002Fclient-data","JSON for client-side pages","Data that your pages fetch in the browser, kept from being embedded by other sites.",[547,560,561],"access: 'public'","useFetch",{"path":563,"title":564,"description":565,"category":545,"order":157,"uses":566},"\u002Fexamples\u002Fmember-site-contact","Visitor routes inside a member app","Keep 'user' as the default and open a few routes to visitors without accounts.",[547,14],{"path":568,"title":569,"description":570,"category":535,"order":85,"uses":571},"\u002Fexamples\u002Fnuxt-auth-utils","nuxt-auth-utils","Use the sealed Cookie session of nuxt-auth-utils as the user check.",[569,572],"requireUserSession",{"path":574,"title":575,"description":576,"category":535,"order":107,"uses":577},"\u002Fexamples\u002Fsidebase-nuxt-auth","Auth.js with @sidebase\u002Fnuxt-auth","Check the Auth.js session, and accept the same JWT as a Bearer token.",[578,579,580],"@sidebase\u002Fnuxt-auth","getServerSession","getToken",{"path":582,"title":583,"description":584,"category":535,"order":146,"uses":585},"\u002Fexamples\u002Fsupabase","Supabase","Verify the Supabase session with getClaims, and tell bad tokens from outages.",[586,587,588],"@nuxtjs\u002Fsupabase","getClaims","isAuthRetryableFetchError",{"path":590,"title":591,"description":592,"category":535,"order":157,"uses":593},"\u002Fexamples\u002Fclerk","Clerk","Read the auth object that Clerk's middleware put on the event.",[594,595],"@clerk\u002Fnuxt","event.context.auth",{"path":597,"title":598,"description":599,"category":535,"order":174,"uses":600},"\u002Fexamples\u002Ffirebase","Firebase Authentication","Session Cookies for the browser, ID tokens for apps, and error codes mapped to 401 or 503.",[601,602,603],"firebase-admin","verifySessionCookie","verifyIdToken",{"path":532,"title":533,"description":534,"category":535,"order":180,"uses":605},[606,607,608],"jose","jwtVerify","createRemoteJWKSet",{"path":523,"title":52,"description":519,"category":518,"order":85,"uses":610},[65,527,528],{"path":537,"title":538,"description":539,"category":518,"order":107,"uses":612},[65,613,614,615],"headers","requestSizeLimiter","allowedMethodsRestricter",{"path":617,"title":618,"description":619,"category":518,"order":146,"uses":620},"\u002Fexamples\u002Fnuxt-csurf","CSRF tokens with nuxt-csurf","Add a synchronizer token on top of the origin check, without breaking Bearer clients.",[621,622,528],"nuxt-csurf","useCsrfFetch",{"path":624,"title":625,"description":626,"category":518,"order":157,"uses":627},"\u002Fexamples\u002Fturnstile","Bot checks on public routes with Turnstile","Public sign-in and sign-up routes skip verifyUser, so give them their own check.",[548,628],"verifyTurnstileToken",{"path":630,"title":631,"description":632,"category":518,"order":174,"uses":633},"\u002Fexamples\u002Fcors-bearer-clients","A frontend on another origin","CORS with nuxt-security for a separate app that calls the BFF with Bearer tokens.",[65,634,635],"corsHandler","viaBearer",{"path":637,"title":638,"description":639,"category":640,"order":85,"uses":641},"\u002Fexamples\u002Frecord-authorization","Per-record authorization","The guard says who the user is. The handler decides what they may touch.","patterns",[642,643],"event.context","createError",{"path":645,"title":646,"description":647,"category":640,"order":107,"uses":648},"\u002Fexamples\u002Froles","Roles and app-wide access","Reject whole groups of users in verifyUser, and check roles per route.",[14,649],"403",{"path":651,"title":652,"description":653,"category":640,"order":146,"uses":654},"\u002Fexamples\u002Fupstream-proxy","Calling an upstream API","Attach the user's token after the guard, never in middleware or routeRules.proxy.",[655,656,657],"$fetch","routeRules.proxy","server\u002Futils",{"path":659,"title":660,"description":661,"category":640,"order":157,"uses":662},"\u002Fexamples\u002Fssr-and-server-functions","SSR and server functions","Call your own API during server rendering, or skip HTTP entirely.",[561,663,657],"useRequestFetch",{"path":665,"title":666,"description":667,"category":640,"order":174,"uses":668},"\u002Fexamples\u002Fwebhooks","Webhooks","Server-to-server callbacks belong outside server\u002Fapi, with their own signature check.",[669,670,671],"server\u002Froutes","readRawBody","xssValidator",{"path":673,"title":674,"description":675,"category":640,"order":180,"uses":676},"\u002Fexamples\u002Fbearer-clients","Mobile apps and CLI tools","One BFF for the browser and for clients that send Bearer tokens.",[635,677],"Authorization",{"path":679,"title":680,"description":681,"category":640,"order":190,"uses":682},"\u002Fexamples\u002Fmore-route-folders","Checking more folders at build time","Extend routePatterns to server\u002Froutes and layers.",[683],"routePatterns",{"path":685,"title":686,"description":687,"category":640,"order":200,"uses":688},"\u002Fexamples\u002Fpublic-cache","Caching public data","Cache what is the same for everyone on a separate public route.",[554,560],{"path":690,"title":691,"description":692,"category":640,"order":214,"uses":693},"\u002Fexamples\u002Fclient-errors","Handling 401, 403 and 503 in the app","Sign out on 401, explain 403, retry on 503.",[694,695],"$fetch.create","onResponseError",{"path":697,"title":698,"description":699,"category":640,"order":231,"uses":700},"\u002Fexamples\u002Ftesting","Testing the protection","An end-to-end test that every protected route refuses anonymous calls.",[701,702],"@nuxt\u002Ftest-utils","vitest",1796451483527,"a99f93bd-e267-45a7-90df-9194cd77468d"]