https://discord.cloudflare.com logo
Join Discord
Powered by
# functions
  • s

    sjnsnsjxne

    04/10/2023, 2:43 PM
    Stupid question, but do I need a wrangler.toml file for a function to be deployed? Build process says it deploys, but then I can’t find it again. Project is an Astro site and the function is saved in /functions/api/submit.js
  • j

    James

    04/10/2023, 2:51 PM
    You do not, no. I'd recommend testing locally with
    wrangler pages dev
  • j

    James

    04/10/2023, 2:51 PM
    The function should also be listed in the functions tab on the deploy once deployed
  • s

    sjnsnsjxne

    04/10/2023, 3:24 PM
    Yeah that unfortunately doesn’t happen. It all works locally, when I explicitly tell wrangle to compile the function, but it doesn’t do it as part of the Astro deployment
  • s

    sjnsnsjxne

    04/10/2023, 3:25 PM
    I’m guessing I have to add Wrangler to the build process somehow
  • s

    sjnsnsjxne

    04/10/2023, 3:30 PM
    Figured it out: In Astro.config.mjs, if nothing else is specified, the Cloudflare loads functions in “Advanced mode.” If set explicitly to “Directory mode,” it works.
  • j

    James

    04/10/2023, 3:57 PM
    Aha, glad you figured it out!
  • c

    Cole Xemi

    04/11/2023, 2:55 AM
    Is the wrangler.toml file used for pages functions?
  • j

    James

    04/11/2023, 3:06 AM
    It is not, no
  • s

    sjnsnsjxne

    04/11/2023, 10:49 AM
    I’m trying to call encrypted environment variables in functions like so: export async function onRequestGet(context) { const variable = context.env.VARIABLE; return new Response (variable); } But it returns undefined. Am I missing something?
  • j

    James

    04/11/2023, 12:38 PM
    That looks fine assuming VARIABLE is set in your prod/preview env. Are you running locally or in prod?
  • j

    Jo Kristian

    04/11/2023, 1:16 PM
    I'm having the same problem as @sjnsnsjxne with secrets. I have a pages project (react) and a function using typescript. It works fine locally using wrangler pages dev build. I can also see the secret keys in the console, but when logging the context, they are not set. console.log(context) has "env": { "ASSETS": {}, "CF_PAGES": "1"..
  • j

    Jo Kristian

    04/11/2023, 1:19 PM
    Locally in dev I set the secrets in
    .dev.vars.
    and the secrets for prod is set via
    wrangler secret put KEY
  • j

    James

    04/11/2023, 1:20 PM
    Pages doesn’t use secrets from wrangler secret put, or wrangler.toml. You will need to configure these in the pages project dashboard, or via the API
  • j

    Jo Kristian

    04/11/2023, 1:21 PM
    Oh, that is a super-confusing difference.
  • j

    James

    04/11/2023, 1:21 PM
    I completely agree - the DX around this all is… extremely unintuitive
  • j

    Jo Kristian

    04/11/2023, 1:22 PM
    Thanks James, do you have a link on how to set secrets for pages functions with an API?
  • j

    James

    04/11/2023, 1:25 PM
    Check out Pages Project -> Update Project at https://developers.cloudflare.com/api/
  • j

    James

    04/11/2023, 1:25 PM
    The dash is generally easier today
  • j

    Jo Kristian

    04/11/2023, 1:27 PM
    The confusing part, is that the worker displays the secret variable, so I honestly thought this was the way. I have used workers before, but first time using functions. Is this the same for mTLS as well?
  • j

    James

    04/11/2023, 1:29 PM
    Everything in Functions needs to be manually configured via dash or api yes. I do not believe Functions has support for mTLS bindings though
  • j

    Jo Kristian

    04/11/2023, 1:32 PM
    Ah :/ Ok. reverting back to workers - Thank you for your answers James, appreciate it. I think some of these differences should be clarified in the documentation, since one can use wrangler to push the project.
  • j

    James

    04/11/2023, 1:34 PM
    Happy to help, sorry Functions don’t work out as the best option for you today. I completely agree - the DX around this all is confusing and unintuitive
  • s

    sjnsnsjxne

    04/11/2023, 6:30 PM
    It’s all in prod and configured via the web UI. Didn’t want to complicate it with local setup as well. The D1 DB env value returns an object, which is promising, but accessing an encrypted env variable somehow produces undefined. If it looks right, it’s probably something stupid Ive missed… 😄
  • j

    James

    04/11/2023, 6:50 PM
    Looks fine to me - are you certain you've set the var in the right env?
  • s

    silentdevnull

    04/11/2023, 7:33 PM
    Does anyone have any recommendations . For a library or sample code to do oauth1.0a signing. I have tried a few different things as well as old pre es6 code and I’m not able to get anything to work.
  • l

    Larry

    04/11/2023, 8:37 PM
    What do you mean by "encrypted"?
  • s

    sjnsnsjxne

    04/11/2023, 9:03 PM
    In the WebUI you can store an environment variable and optionally encrypt the value of it. Useful for API keys.
  • s

    sjnsnsjxne

    04/11/2023, 9:03 PM
    Thanks for helping out. I’ll have to get back to it when I have time, but I’m sure there’s an easy fix if the code principle looks ok
  • c

    c becker

    04/12/2023, 8:43 PM
    Hi, I finally found a decent implementation that meets my goals for oAuth enabled 3p logins on my app. All the oAuth magic happens in functions, my SPA works because of an httpOnly cookie that is implicitly set for all XHR that hit my functions after login/auth. If anyone cares to see or try it in action: Assemble auth url: https://github.com/readysetawesome/timely-tasker/blob/main/functions/greet.ts#L29 Handle callback: https://github.com/readysetawesome/timely-tasker/pull/92/files#diff-8d9eb198439d049f85fac1356029dfffb2fbc2fd51335a8b6620f878db1143aaR14 * Goal: only to establish identity/ownership of data, no google APIs will be authorized via oAuth scopes. For this we can target just the id_token info. * Uses oAuth only, not OIDC, only supports google accounts. * authorization code flow, server->server code exchange results in a crypto-safe unique session ID cookie (generated by my app) that is not visible to JS (i.e. marked 'httpOnly' to bust XSRF) * this cookie comes with all future XHRs to CRUD my app's cloud data, that's how I associate it with end users * the direct server->server call to google over TLS for token exchange means I don't validate the JWT separately, the identity returned is always valid within this context. I don't pass any oAuth tokens to the client ever. I really want this made into kind of a boilerplate integration so I can easily spin up new apps with an "identity" that I can associate with things I want to let my users create. Coming soon... what you see here is a bit brittle due to lack of error handlings. Unexpected challenges: * couldn't use any of google's api libraries, unfriendly to edge computing envs! I pretty much had to roll my own authorization url builder and the code exchange server->server integration.
1...370371372...392Latest