3
3
using Infrastructure . Auth0 . Abstractions ;
4
4
using Infrastructure . Auth0 . Configuration ;
5
5
using Infrastructure . Auth0 . Models ;
6
+ using Infrastructure . Persistence . MassTransit . Analytics ;
6
7
using Microsoft . Extensions . Logging ;
7
8
using Microsoft . Extensions . Options ;
8
9
@@ -11,11 +12,13 @@ namespace Infrastructure.Auth0;
11
12
public class AuthService (
12
13
IOptions < Auth0Options > auth0Options ,
13
14
IAuthenticationApiClient authenticationClient ,
15
+ IUserAnalyticsClient userAnalyticsClient ,
14
16
ILogger < AuthService > logger ) : IAuthService
15
17
{
16
18
private readonly Auth0Options _auth0Options = auth0Options . Value ??
17
19
throw new ArgumentNullException ( nameof ( auth0Options ) ) ;
18
20
private readonly IAuthenticationApiClient _auth0Client = authenticationClient ;
21
+ private readonly IUserAnalyticsClient _userAnalyticsClient = userAnalyticsClient ;
19
22
private readonly ILogger < AuthService > _logger = logger ;
20
23
21
24
public async Task < TokenInfoDto ? > GetUserToken ( LoginRequestDto req , CancellationToken ct )
@@ -33,14 +36,15 @@ public class AuthService(
33
36
ClientSecret = _auth0Options . ClientSecret ,
34
37
Scope = "openid"
35
38
} , ct ) ;
36
-
39
+
37
40
var authToken = auth0Response ? . AccessToken ;
38
41
if ( string . IsNullOrEmpty ( authToken ) )
39
42
{
40
43
_logger . LogWarning ( "Failed to get token for user {Login}" , req . Login ) ;
41
44
return null ;
42
45
}
43
-
46
+
47
+ await _userAnalyticsClient . SendUserSignIn ( req . Login , ct ) ;
44
48
return new TokenInfoDto ( authToken , auth0Response ? . ExpiresIn ?? 0 ) ;
45
49
}
46
50
catch ( Exception ex )
@@ -50,11 +54,11 @@ public class AuthService(
50
54
}
51
55
}
52
56
53
- public async Task < TokenInfoDto ? > RegisterUser ( RegisterRequestDto req , CancellationToken ct )
57
+ public async Task < UserInfoDto ? > RegisterUser ( RegisterRequestDto req , CancellationToken ct )
54
58
{
55
59
try
56
60
{
57
- var username = $ "{ req . FullName [ 0 ] } -{ Guid . NewGuid ( ) . ToString ( ) [ ..6 ] } ";
61
+ var username = $ "{ req . FullName [ 0 ] } -{ Guid . NewGuid ( ) . ToString ( ) [ ..6 ] } ". ToLower ( ) ;
58
62
await _auth0Client . SignupUserAsync ( new SignupUserRequest
59
63
{
60
64
Username = username ,
@@ -71,8 +75,18 @@ await _auth0Client.SignupUserAsync(new SignupUserRequest
71
75
Connection = _auth0Options . Realm ,
72
76
ClientId = _auth0Options . ClientId
73
77
} , ct ) ;
74
-
75
- return await GetUserToken ( new LoginRequestDto ( req . Email , req . Password ) , ct ) ;
78
+
79
+ // By now, all users registered are recipients.
80
+ // Administrators are created manually via Auth0 management console.
81
+ await _userAnalyticsClient . SendUserSignUp ( username , req . Email , req . FullName , "recipient" , ct ) ;
82
+ var tokenInfo = await GetUserToken ( new LoginRequestDto ( req . Email , req . Password ) , ct ) ;
83
+ if ( tokenInfo is null )
84
+ {
85
+ _logger . LogWarning ( "Failed to register user {Login}" , req . Login ) ;
86
+ return null ;
87
+ }
88
+
89
+ return new UserInfoDto ( username , tokenInfo ) ;
76
90
}
77
91
catch ( Exception ex )
78
92
{
0 commit comments