Skip to content

Commit 8d4aec7

Browse files
committed
fix to credential fetching issue
1 parent f3e1d94 commit 8d4aec7

File tree

4 files changed

+9
-9
lines changed

4 files changed

+9
-9
lines changed

Controllers/AuthController.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public async Task<IActionResult> GoogleCallback([FromQuery]IDictionary<string, s
238238
// If user is logged in and the auth token is not registered yet, link.
239239
if (HttpContext.Session.GetString("user") != null)
240240
{
241-
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).FirstOrDefaultAsync();
241+
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).Include("Credentials").FirstOrDefaultAsync();
242242

243243
// If someone already has that token OR there is a user that has the email but is not the same user.
244244
if (userWithMatchingToken != null || (userWithMatchingEmail != null && userWithMatchingEmail.Email != user.Email))
@@ -330,12 +330,12 @@ public async Task<IActionResult> GithubCallback([FromQuery]IDictionary<string, s
330330

331331
// Fetching data
332332
var userWithMatchingToken = await _context.Users.Where(c => c.Credentials.Any(cred => cred.Provider == AuthProvider.GITHUB && cred.Token == userinfo.Id)).FirstOrDefaultAsync();
333-
var userWithMatchingEmail = await _context.Users.Where(c => c.Email != null && c.Email == userinfo.Email).FirstOrDefaultAsync();
333+
var userWithMatchingEmail = await _context.Users.Where(c => userinfo.Email != null && c.Email == userinfo.Email).FirstOrDefaultAsync();
334334

335335
// If user is logged in and the auth token is not registered yet, link.
336336
if (HttpContext.Session.GetString("user") != null)
337337
{
338-
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).FirstOrDefaultAsync();
338+
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).Include("Credentials").FirstOrDefaultAsync();
339339

340340
// If someone already has that token OR there is a user that has the email but is not the same user.
341341
if (userWithMatchingToken != null || (userWithMatchingEmail != null && userWithMatchingEmail.Email != user.Email))
@@ -433,7 +433,7 @@ public async Task<IActionResult> RedditCallback([FromQuery]IDictionary<string, s
433433
// If user is logged in and the auth token is not registered yet, link.
434434
if (HttpContext.Session.GetString("user") != null)
435435
{
436-
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).FirstOrDefaultAsync();
436+
var user = await _context.Users.Where(c => c.Id == HttpContext.Session.GetString("user")).Include("Credentials").FirstOrDefaultAsync();
437437

438438
// If someone already has that token OR there is a user that has the email but is not the same user.
439439
if (userWithMatchingToken != null)

Controllers/ProfileController.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,15 @@ public async Task<IActionResult> Profile()
3636
return Redirect("/Auth/Login");
3737
}
3838

39-
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).FirstOrDefaultAsync();
39+
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).Include("Credentials").FirstOrDefaultAsync();
4040

4141
if (user.Credentials == null)
4242
{
4343
user.Credentials = new List<Credential>();
4444
}
4545

46-
ViewData["HasPassword"] = user.Password != null;
46+
var blankPassword = BCrypt.Net.BCrypt.Verify("", user.Password);
47+
ViewData["HasPassword"] = user.Password != null && !blankPassword;
4748

4849
ViewData["GoogleLinked"] = user.Credentials.Exists(c => { return c.Provider == AuthProvider.GOOGLE; });
4950
ViewData["GithubLinked"] = user.Credentials.Exists(c => { return c.Provider == AuthProvider.GITHUB; });
@@ -133,7 +134,7 @@ public async Task<IActionResult> ChangeOAuth(string submit)
133134
submit = submit.First().ToString().ToUpper() + submit.Substring(1);
134135

135136
// Fetching the user
136-
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).FirstOrDefaultAsync();
137+
var user = await _context.Users.Where(c => Regex.IsMatch(c.Id, HttpContext.Session.GetString("user"))).Include("Credentials").FirstOrDefaultAsync();
137138

138139
AuthProvider provider;
139140
if (!Enum.TryParse(submit.ToUpper(), out provider))

Models/DatabaseContext.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public User(string email, string password)
3434
this.Id = Guid.NewGuid().ToString();
3535
this.Email = email;
3636
this.Password = BCrypt.Net.BCrypt.HashPassword(password);
37-
this.Credentials = new List<Credential>();
3837
}
3938

4039
public User()
4140
{
41+
Credentials = new List<Credential>();
4242
}
4343

4444
public string Id { get; set; }

docker-compose-windows.yml

-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,3 @@ services:
3838
- 8080:8080
3939
volumes:
4040
db:
41-
external: true

0 commit comments

Comments
 (0)