What is the difference between overriding an api v...
# support-questions
p
What is the difference between overriding an api vs overriding a function? For example consumeCode as a function vs consumeCodePost as an api? What are the intended use cases for each option?
r
hey @User that's a great question. So there are two ways the recipe functions are called: - via the api functions - via the functions exposed by the recipes which you can call in your APIs If you override the override.functions, you will be affecting logic for both the calls above. On the other hand, if you are overriding the API, you will be affecting just the API call. One more difference is that apis may call multiple recipe functions in them. For example, the sign in API calls the sign in recipe function and the session's create new session function. So if you override the sign in API, you can do something before or after both those recipe functions have run. On the other hand, the recipe function for sign in (which is called by the sign in API), doesn't call any other recipe function. So if you want to do something post sign in, but before a session is created, you should override the recipe sign in function and not the API sign in function. Overall, if your modification doesn't depend on the request object, you should try and override the recipe function, else you should override the api function
p
My modification in this instance was that I wanted the email address inside the accesstoken payload. Solution was therefore to use the consumeCode function override to add the email to the context after successfully using the code. Then in the session create function add the email to the access token payload by pulling it from the context. There was a similar example in the docs but it was confusing to start with as I already had a consumeCode api override and that gets called in a different order.
I’m also considering creating / updating a user object in my own db after login / signup. Trying to decide if that’s best done in the api or the function
r
You are right about how to get the info from request obj to create new session function. You have to use the context object.
Adding to your own db is best done in the recipe function. Since if you call the consumeCode function manually later on (if ever in the future), the user will be added to your db then as well
p
That’s what I figured however the first example in the docs I read showed the api override. But I now can’t really see a reason to ever use the api override. Seems you want to pretty much always default to the recipe function
r
Yes. That’s true. API override would be useful to comsume some custom Params from the request body or send a custom response. And also add to the user context object
The reason it’s api override in docs is cause often people need some other info from the request when adding to their own db. That’s readily available when doing override of api as opposed to override of functions (in which case u have to use user context)
p
Ok makes sense thanks.
r
Seems like you really understood the concept well! How much time did it take to get this understanding? Any feedback on how to make it easier?
p
Spent about 4 hours implementing it today. Am up and running and deployed now.
r
That’s great
p
The api reference in the docs was probably the most confusing and least useful. The examples obviously much more helpful
r
Are you talking about the swagger api reference?
p
No the swagger docs are fine. The node js api reference was hard to read
r
Ah I see. The ones in /docs/nodejs?
p
I’ll just check the url..
Yes that’s the one
r
I see. Fair enough. That’s only meant to know more about a specific function. Like the Args and output of a function. Not really a guide
p
Yeah.. I had a look round them but went back to the guides
r
Thanks for the feedback
p
Im now considing how to implement a few more advanced features. 1: alerts / logging when high volume of login attempts 2: account lock / timeout on failed logins
I guess account lock after failed attempts is less relevant with passwordless.. but some sort of admin alert on suspisous activity would be useful
r
I see. I have ideas on how those can be implemented. Will do a small write up soon
p
great.. ill look forward to it.. for now im up and running with a good token system.. I can throw away the one I was half way through writing 🙂
r
I’ll probably do them as a blog post for our site. Seems useful for others too
p
Im considering deploying a docker image for production on heroku.. I don suppose there is a blog post on heroku hosting anywhere?
r
There isn’t 😦
Though there is an issue someone had faced with heroku. Something to do with permissions of users
And they had forked the docker image and fixed it for themselves.
I’ll send you the issue link
p
perfect thanks
p
Thanks
4 Views