Skip to content

Commit 49f3bf1

Browse files
authored
Feature/55 integrate auth (#57)
* feat(55): Adds basic implementation of Clerk auth to the frontend. * feat(55): Got backend working correctly with Clerk. * feat(55): Makes a lot of small changes. Adds more Clerk support.
1 parent aba7337 commit 49f3bf1

27 files changed

Lines changed: 452 additions & 293 deletions

.DS_Store

0 Bytes
Binary file not shown.

Breeze.Api/Budget/BudgetController.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@ public BudgetController(IConfiguration config, ILogger<BudgetController> logger,
2323
}
2424

2525
[HttpGet("{year}-{month}")]
26+
[Authorize]
2627
public IActionResult GetBudget([FromRoute] int year, [FromRoute] int month)
2728
{
28-
2929
try
3030
{
31-
var userId = User.GetObjectId();
32-
if (userId == null)
31+
var userId = User.FindFirst("sub")?.Value;
32+
33+
if (string.IsNullOrWhiteSpace(userId))
3334
{
3435
_logger.LogError(User.ToString());
3536
return Unauthorized();
3637
}
37-
return Ok(budgets.GetBudgetByDate(userId, new DateOnly(year, month, 1)));
38+
39+
var budget = budgets.GetBudgetByDate(userId, new DateOnly(year, month, 1));
40+
return Ok(budget);
3841
}
3942
catch (Exception ex)
4043
{
41-
_logger.LogError(ex.Message);
42-
return BadRequest();
44+
_logger.LogError(ex, "Failed to get budget");
45+
return BadRequest("Something went wrong.");
4346
}
4447
}
48+
4549
}
4650
}

Breeze.Api/Categories/CategoryController.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ public IActionResult GetCategoriesByBudgetId([FromRoute] int budgetId)
3232
{
3333
try
3434
{
35-
var userId = User.GetObjectId();
36-
if (userId == null)
35+
var userId = User.FindFirst("sub")?.Value;
36+
37+
if (string.IsNullOrWhiteSpace(userId))
3738
{
3839
_logger.LogError(User.ToString());
3940
return Unauthorized();
@@ -52,8 +53,9 @@ public IActionResult PostCategory(CategoryRequest categoryRequest)
5253
{
5354
try
5455
{
55-
var userId = User.GetObjectId();
56-
if (userId == null)
56+
var userId = User.FindFirst("sub")?.Value;
57+
58+
if (string.IsNullOrWhiteSpace(userId))
5759
{
5860
_logger.LogError(User.ToString());
5961
return Unauthorized();
@@ -80,8 +82,9 @@ public IActionResult PatchCategory(CategoryRequest categoryRequest)
8082
{
8183
try
8284
{
83-
var userId = User.GetObjectId();
84-
if (userId == null)
85+
var userId = User.FindFirst("sub")?.Value;
86+
87+
if (string.IsNullOrWhiteSpace(userId))
8588
{
8689
_logger.LogError(User.ToString());
8790
return Unauthorized();
@@ -108,8 +111,9 @@ public async Task<IActionResult> DeleteCategory([FromRoute] int categoryId)
108111
{
109112
try
110113
{
111-
var userId = User.GetObjectId();
112-
if (userId == null)
114+
var userId = User.FindFirst("sub")?.Value;
115+
116+
if (string.IsNullOrWhiteSpace(userId))
113117
{
114118
_logger.LogError(User.ToString());
115119
return Unauthorized();

Breeze.Api/Expenses/ExpenseController.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ public IActionResult GetExpensesForCategory([FromRoute] int CategoryId)
2929
{
3030
try
3131
{
32-
var userId = User.GetObjectId();
33-
if (userId == null)
32+
var userId = User.FindFirst("sub")?.Value;
33+
34+
if (string.IsNullOrWhiteSpace(userId))
3435
{
3536
_logger.LogError(User.ToString());
3637
return Unauthorized();
@@ -49,8 +50,9 @@ public IActionResult GetAllExpensesForUser([FromRoute] int BudgetID)
4950
{
5051
try
5152
{
52-
var userId = User.GetObjectId();
53-
if (userId == null)
53+
var userId = User.FindFirst("sub")?.Value;
54+
55+
if (string.IsNullOrWhiteSpace(userId))
5456
{
5557
_logger.LogError(User.ToString());
5658
return Unauthorized();
@@ -69,8 +71,9 @@ public IActionResult PostExpsense(ExpenseRequest expenseRequest)
6971
{
7072
try
7173
{
72-
var userId = User.GetObjectId();
73-
if (userId == null)
74+
var userId = User.FindFirst("sub")?.Value;
75+
76+
if (string.IsNullOrWhiteSpace(userId))
7477
{
7578
_logger.LogError(User.ToString());
7679
return Unauthorized();
@@ -96,8 +99,9 @@ public IActionResult PatchExpsense([FromBody] ExpenseRequest expenseRequest)
9699
{
97100
try
98101
{
99-
var userId = User.GetObjectId();
100-
if (userId == null)
102+
var userId = User.FindFirst("sub")?.Value;
103+
104+
if (string.IsNullOrWhiteSpace(userId))
101105
{
102106
_logger.LogError(User.ToString());
103107
return Unauthorized();
@@ -122,8 +126,9 @@ public IActionResult DeleteExpsense([FromRoute] int id)
122126
{
123127
try
124128
{
125-
var userId = User.GetObjectId();
126-
if (userId == null)
129+
var userId = User.FindFirst("sub")?.Value;
130+
131+
if (string.IsNullOrWhiteSpace(userId))
127132
{
128133
_logger.LogError(User.ToString());
129134
return Unauthorized();

Breeze.Api/Goals/GoalController.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using Breeze.Api.Expenses;
22
using Breeze.Api.Goals.RequestResponseObjects;
33
using Breeze.Data;
4+
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Mvc;
56
using Microsoft.Identity.Web;
6-
using Microsoft.AspNetCore.Authorization;
77

88
namespace Breeze.Api.Goals
99
{
@@ -26,8 +26,9 @@ public IActionResult GetGoalsForUser()
2626
{
2727
try
2828
{
29-
var userId = User.GetObjectId();
30-
if (userId == null)
29+
var userId = User.FindFirst("sub")?.Value;
30+
31+
if (string.IsNullOrWhiteSpace(userId))
3132
{
3233
_logger.LogError(User.ToString());
3334
return Unauthorized();
@@ -46,8 +47,9 @@ public IActionResult PostGoal(GoalRequest goalRequest)
4647
{
4748
try
4849
{
49-
var userId = User.GetObjectId();
50-
if (userId == null)
50+
var userId = User.FindFirst("sub")?.Value;
51+
52+
if (string.IsNullOrWhiteSpace(userId))
5153
{
5254
_logger.LogError(User.ToString());
5355
return Unauthorized();
@@ -66,8 +68,9 @@ public IActionResult PatchGoal([FromBody] GoalRequest goalRequest)
6668
{
6769
try
6870
{
69-
var userId = User.GetObjectId();
70-
if (userId == null)
71+
var userId = User.FindFirst("sub")?.Value;
72+
73+
if (string.IsNullOrWhiteSpace(userId))
7174
{
7275
_logger.LogError(User.ToString());
7376
return Unauthorized();
@@ -86,8 +89,9 @@ public IActionResult DeleteGoal([FromRoute] int id)
8689
{
8790
try
8891
{
89-
var userId = User.GetObjectId();
90-
if (userId == null)
92+
var userId = User.FindFirst("sub")?.Value;
93+
94+
if (string.IsNullOrWhiteSpace(userId))
9195
{
9296
_logger.LogError(User.ToString());
9397
return Unauthorized();

Breeze.Api/Incomes/IncomeController.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public IActionResult GetIncomes([FromRoute] int budgetId)
2828
{
2929
try
3030
{
31-
var userId = User.GetObjectId();
32-
if (userId == null)
31+
var userId = User.FindFirst("sub")?.Value;
32+
33+
if (string.IsNullOrWhiteSpace(userId))
3334
{
3435
_logger.LogError(User.ToString());
3536
return Unauthorized();
@@ -48,8 +49,9 @@ public IActionResult PostIncome(IncomeRequest incomeRequest)
4849
{
4950
try
5051
{
51-
var userId = User.GetObjectId();
52-
if (userId == null)
52+
var userId = User.FindFirst("sub")?.Value;
53+
54+
if (string.IsNullOrWhiteSpace(userId))
5355
{
5456
_logger.LogError(User.ToString());
5557
return Unauthorized();
@@ -79,8 +81,9 @@ public IActionResult PatchIncome([FromBody] IncomeRequest incomeRequest)
7981
{
8082
try
8183
{
82-
var userId = User.GetObjectId();
83-
if (userId == null)
84+
var userId = User.FindFirst("sub")?.Value;
85+
86+
if (string.IsNullOrWhiteSpace(userId))
8487
{
8588
_logger.LogError(User.ToString());
8689
return Unauthorized();
@@ -109,8 +112,9 @@ public IActionResult DeleteIncome([FromRoute] int id)
109112
{
110113
try
111114
{
112-
var userId = User.GetObjectId();
113-
if (userId == null)
115+
var userId = User.FindFirst("sub")?.Value;
116+
117+
if (string.IsNullOrWhiteSpace(userId))
114118
{
115119
_logger.LogError(User.ToString());
116120
return Unauthorized();

Breeze.Api/Program.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1+
using System.IdentityModel.Tokens.Jwt;
12
using Breeze.Data;
23
using Microsoft.AspNetCore.Authentication.JwtBearer;
34
using Microsoft.EntityFrameworkCore;
45
using Microsoft.Identity.Web;
6+
using Microsoft.IdentityModel.JsonWebTokens;
7+
using Microsoft.IdentityModel.Tokens;
58
using Microsoft.OpenApi.Models;
69

10+
JsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear();
11+
712
var builder = WebApplication.CreateBuilder(args);
8-
// Docs: https://learn.microsoft.com/en-us/azure/active-directory-b2c/enable-authentication-web-api?tabs=csharpclient
913
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
10-
.AddMicrosoftIdentityWebApi(options =>
11-
{
12-
builder.Configuration.Bind("AzureAdB2C", options);
13-
14-
options.TokenValidationParameters.NameClaimType = "name";
15-
},
16-
options =>
14+
.AddJwtBearer(options =>
1715
{
18-
builder.Configuration.Bind("AzureAdB2C", options);
16+
options.Authority = "https://apt-monkfish-71.clerk.accounts.dev";
17+
options.TokenValidationParameters = new TokenValidationParameters
18+
{
19+
ValidateIssuer = true,
20+
ValidIssuer = "https://apt-monkfish-71.clerk.accounts.dev",
21+
ValidateAudience = false, // Set to true if you're using custom audience
22+
ValidateLifetime = true,
23+
NameClaimType = "sub" // This is Clerk's user ID
24+
};
1925
});
2026

27+
28+
2129
builder.Services.AddAuthorization();
2230

2331
builder.Services.AddControllers();

Breeze.Web/.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ VITE_BASE_LOCAL_LOGIN_URL: 'http://localhost:5173/login'
44
VITE_BASE_HOSTED_LOGIN_URL: 'https://wwww.breeze.seannkelleyy.com/login'
55
VITE_BASE_LOCAL_API: 'https://localhost:7284'
66
VITE_BASE_HOSTED_API: 'https://breeze-api.azurewebsites.net'
7-
VITE_APP_VERSION: '1.1.0'
7+
VITE_APP_VERSION: '1.1.0'
8+
VITE_CLERK_PUBLISHABLE_KEY: 'pk_test_YXB0LW1vbmtmaXNoLTcxLmNsZXJrLmFjY291bnRzLmRldiQ'

Breeze.Web/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
name="viewport"
1212
content="width=device-width, initial-scale=1.0"
1313
/>
14-
<title>Breeze Budgeting</title>
14+
<title>Breeze Budgets By Seannkelleyy</title>
1515
</head>
1616
<body>
1717
<div id="root"></div>

Breeze.Web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"preview": "vite preview"
1111
},
1212
"dependencies": {
13+
"@clerk/clerk-react": "^5.31.4",
1314
"@hookform/resolvers": "^3.9.1",
1415
"@radix-ui/react-checkbox": "^1.1.3",
1516
"@radix-ui/react-dialog": "^1.1.4",
@@ -51,6 +52,6 @@
5152
"tailwindcss": "^3.4.17",
5253
"typescript": "^5.5.3",
5354
"typescript-eslint": "^8.7.0",
54-
"vite": "^6.2.1"
55+
"vite": "^6.3.5"
5556
}
5657
}

0 commit comments

Comments
 (0)