Supabase Auth - with slack alerts?
How can I setup a Slackbot so anytime a user signs up it's posted in Slack?
Also, this is my first time using Auth. I notice so many spam accounts sign up via email. Does others see this? How can you avoid this?
62 Replies
It's recommended to implement captcha for password-based login.
I would assume something like a
Custom Access Token
hook can solve your need of posting to slack.
https://supabase.com/docs/guides/auth/auth-hooks/custom-access-token-hook?queryGroups=language&language=http
It just points to an http endpoint, either in your app/server or you can make a supabase edge function that when triggered will post to the relevant slack channel.Custom Access Token Hook | Supabase Docs
Customize the access token issued by Supabase Auth
What I do for slack notifications for signups is to use a db function that is triggered by inserts in
auth.users
(when a user signs up a record is created). This function calls an external webhook and passes the email and id to it. That webhook will take care of the Slack notification (I use no-code make.com)
this is my function code in case it is useful:
you later set up the trigger on auth.users
to call this functionThanks @j4 , adding captcha was pretty easy
@TomΓ‘s P. - i'm confused you cant trigger on auth.users though ?
I tried setting up an auth hook which calls an HTTP endpoint which is an edge function
this didn't work. and when i tried to login it broke my app

but the edge function logs for this show nothing
I also tried to use your postgres function and got errors anytime i tried to save it

It said net wasn't enabled in my logs or an error around that
thanks @silentworks will try this now. Should I try the database function method or the edge functions route?
Will something like this work with pg_net enabled?
As a developer you should explore your options and see what works for you. It's a part of the journey.
Yeah, that's fair. I spent like 1.5hrs last night exploring every option so was curious for a bit of a suggestion lol
When I tried the edge function route I never saw the function was invoked or any logs in the supabase edge function tab. But ill try this again when that setting enabled
1.5hrs is a small amount of time to explore. Implement all the ways you've learned about and see which works best for you.
Your own personal experience trumps any suggestion you will be given.
Let me ask one question then. If i call PERFORM pg_notify('edge_function_event', payload::TEXT);
and call my edge function how would you debug that function not being invoked?
I've never used
PERFORM
nor pg_notify
before, so I cannot advice you on that.Perform is just Select with no requirement to return data.
pg_notify will not work with edge functions. It is normally a call between postgres and another postgres or a server using a direct database connection.
Gotcha.
Does this make sense to ppl?
with this trigger
This doesnt work πΏ
Use
raise log 'blah = %',var;
to get messages in postgres log of dashboard. Also check for postgres errors.
I would use pg_net if you are going to do perform (no reponse).You can do triggers off Auth.users right? In the UI it says you cant which confused me
You can.
You have to do it with SQL. Your function will also have to be security definer type.
So i just tried again with log statements but i dont see anything in postgres logs

So maybe the trigger isnt working?
Tried with pg_net too
If you are not getting errors and not getting log messages then your function is likely not being called.
Your trigger SQL looks correct.
Yeah, so strange π¦
Are you getting a new user showing up in the auth.users table?

Yeah, in the uathentication tab i see a users table
And a new user gets added to it?
Yeah
the user is being created
I should see it in the postgres log collection right?
Yes. those are raise log messages from a function I have.

yeah, i dont think the function or trigger is being called
Where did you get your trigger screen from?
Mine looks different but there may be more than one spot.


looks similar
can i see what function you used?
No that is different than the first one you showed. And the function name is not the one you are showing.
oh, the other screenshot was from a query showing all triggers i have
So you are calling a different function.
notify_new_signup
oh wait weird
versus notify_slack
Right lol
let me rename the function to notify_new-signup
When you enabled pg_net extension which table did you enable it on?
Extensions, public or auth?
extensions
then you call it with net.http_post
enabled it with extensions. So now i see errors in the log which is good.
it said
function net.http_post(url => text, body => text, headers => jsonb) does not exist
but strange b/c i have pg_net enabledYour parameters are likely not the right types.
ok it worked!! π
i switched back to http_post , got an error then tried public.http_post which worked.
I bet for
net
i had to do extensions.net.http_post
but in any event thank you so much πIt would be net.http_post. But the parameters are not the same for the two calls. Some are jsonb instead of text.
I would use pg_net unless you are going to check for error in an auth function. The issue is if your webhook takes several seconds to respond, auth will timeout and error back to the client and then you create user friction.
Oh thatβs a good point. Is your function calling slack? Curious to see what parameters you used?
I don't call an external endpoint from my auth trigger.
Do you post to Slack?
No.
Another user commented here that they do.
Im using this now
but now i see no logs again π¦
and it snot working
why is this so complicated lol
You changed the function name?
kept it notify_new_signup
and see that in my auth db triggers so thats not an issue
If the function is being called you will see your log message or an error in the Postgres log. I've noticed sometimes the log can be delayed a minute or so.
ok, i see the log there. reviewing these docs . i think slack requires a header for app json
https://supabase.com/docs/guides/database/extensions/pg_net
pg_net: Async Networking | Supabase Docs
pg_net: an async networking extension for PostgreSQL.
ok i got it to work!!!
If it helps anyone this worked
Thank you @garyaustin again!
Also put my learnings here -> https://github.com/mauerbac/supabase_auth_slackbot/tree/main
GitHub
GitHub - mauerbac/supabase_auth_slackbot: Tutorial to get alerts in...
Tutorial to get alerts in Slack when users signup with Supabase Auth - mauerbac/supabase_auth_slackbot
funnily i broke my email auth from testing so much

Are you using the built in SMTP?
Yes
that should reset with time right?
If so you should get your own provider. Yes 1 hour.
yeah i should set that up
thanks again
thanks, this really helped me!