Skip to content

Commit 614c869

Browse files
author
Daniel Medin
committed
Send Slack webhook when a user registers
1 parent 18204a9 commit 614c869

8 files changed

Lines changed: 82 additions & 1 deletion

File tree

BitStoreWeb/BitStoreWeb.Net9/Controllers/AccountController.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ public class AccountController : Controller
1616
private const string RegisterMode = "register";
1717

1818
private readonly IUserAuthService _userAuthService;
19+
private readonly IUserRegistrationNotifier _userRegistrationNotifier;
1920
private readonly AppDbContext _db;
2021

21-
public AccountController(IUserAuthService userAuthService, AppDbContext db)
22+
public AccountController(
23+
IUserAuthService userAuthService,
24+
IUserRegistrationNotifier userRegistrationNotifier,
25+
AppDbContext db)
2226
{
2327
_userAuthService = userAuthService;
28+
_userRegistrationNotifier = userRegistrationNotifier;
2429
_db = db;
2530
}
2631

@@ -88,6 +93,11 @@ await HttpContext.SignInAsync(
8893
new ClaimsPrincipal(claimsIdentity),
8994
authProperties);
9095

96+
if (authMode == RegisterMode)
97+
{
98+
await _userRegistrationNotifier.NotifyUserRegisteredAsync(user);
99+
}
100+
91101
return RedirectToLocal(returnUrl);
92102
}
93103

BitStoreWeb/BitStoreWeb.Net9/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
});
4444
builder.Services.AddScoped<IPasswordHasher<BitStoreWeb.Net9.Models.AppUser>, PasswordHasher<BitStoreWeb.Net9.Models.AppUser>>();
4545
builder.Services.AddScoped<IUserAuthService, UserAuthService>();
46+
builder.Services.AddHttpClient<IUserRegistrationNotifier, SlackUserRegistrationNotifier>();
4647
builder.Services.AddEndpointsApiExplorer();
4748
builder.Services.AddSwaggerGen(options =>
4849
{
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using BitStoreWeb.Net9.Models;
2+
3+
namespace BitStoreWeb.Net9.Services;
4+
5+
public interface IUserRegistrationNotifier
6+
{
7+
Task NotifyUserRegisteredAsync(AppUser user);
8+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using System.Net.Http.Json;
2+
using BitStoreWeb.Net9.Models;
3+
4+
namespace BitStoreWeb.Net9.Services;
5+
6+
public class SlackUserRegistrationNotifier : IUserRegistrationNotifier
7+
{
8+
private const string WebhookConfigKey = "Slack:RegistrationWebhookUrl";
9+
10+
private readonly HttpClient _httpClient;
11+
private readonly IConfiguration _configuration;
12+
private readonly ILogger<SlackUserRegistrationNotifier> _logger;
13+
14+
public SlackUserRegistrationNotifier(
15+
HttpClient httpClient,
16+
IConfiguration configuration,
17+
ILogger<SlackUserRegistrationNotifier> logger)
18+
{
19+
_httpClient = httpClient;
20+
_configuration = configuration;
21+
_logger = logger;
22+
}
23+
24+
public async Task NotifyUserRegisteredAsync(AppUser user)
25+
{
26+
var webhookUrl = _configuration[WebhookConfigKey];
27+
if (string.IsNullOrWhiteSpace(webhookUrl))
28+
{
29+
return;
30+
}
31+
32+
var payload = new
33+
{
34+
text = $"New BitStore account registered: {user.UserName} (role: {user.Role}) at {DateTime.UtcNow:yyyy-MM-dd HH:mm:ss} UTC."
35+
};
36+
37+
try
38+
{
39+
using var response = await _httpClient.PostAsJsonAsync(webhookUrl, payload);
40+
if (!response.IsSuccessStatusCode)
41+
{
42+
_logger.LogWarning(
43+
"Slack registration webhook failed with status {StatusCode}.",
44+
(int)response.StatusCode);
45+
}
46+
}
47+
catch (Exception ex)
48+
{
49+
_logger.LogError(ex, "Slack registration webhook call failed.");
50+
}
51+
}
52+
}

BitStoreWeb/BitStoreWeb.Net9/appsettings.Development.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"DefaultConnection": "Data Source=bitstore.db"
44
},
55
"DatabaseProvider": "Sqlite",
6+
"Slack": {
7+
"RegistrationWebhookUrl": ""
8+
},
69
"Logging": {
710
"LogLevel": {
811
"Default": "Information",

BitStoreWeb/BitStoreWeb.Net9/appsettings.Production.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"DatabaseProvider": "SqlServer",
3+
"Slack": {
4+
"RegistrationWebhookUrl": ""
5+
},
36
"Logging": {
47
"LogLevel": {
58
"Default": "Information",

BitStoreWeb/BitStoreWeb.Net9/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"DefaultConnection": "Data Source=bitstore.db"
44
},
55
"DatabaseProvider": "Sqlite",
6+
"Slack": {
7+
"RegistrationWebhookUrl": ""
8+
},
69
"Logging": {
710
"LogLevel": {
811
"Default": "Information",

BitStoreWeb/docs/DEPLOY_AZURE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ In App Service -> **Environment variables**, add:
4646

4747
- `DatabaseProvider` = `SqlServer`
4848
- `ConnectionStrings__DefaultConnection` = your Azure SQL connection string
49+
- `Slack__RegistrationWebhookUrl` = your Slack Incoming Webhook URL (optional, for new-user alerts)
4950

5051
Example connection string:
5152

0 commit comments

Comments
 (0)