Skip to content

Commit

Permalink
Merge pull request #78 from BinaryStudioAcademy/hotfix/login-error
Browse files Browse the repository at this point in the history
hotfix/login-error
  • Loading branch information
tatianahutii committed Sep 4, 2023
2 parents 7cd76ac + 9f7661e commit 46fabdd
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public interface IUserService
{
public Task<UserDto> CreateUserAsync(NewUserDto userDto);
public Task<bool> CheckIsExistingEmail(string email);
public Task<UserDto> GetUserByUidAsync(string uid);
public string GetCurrentUserUid();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Http.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="6.0.0" />
</ItemGroup>

Expand All @@ -16,5 +17,4 @@
<ProjectReference Include="..\LeetWars.Core.Common\LeetWars.Core.Common.csproj" />
<ProjectReference Include="..\LeetWars.Core.DAL\LeetWars.Core.DAL.csproj" />
</ItemGroup>

</Project>
19 changes: 18 additions & 1 deletion backend/LeetWars.Core/LeetWars.Core.BLL/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using System.Security.Claims;
using AutoMapper;
using LeetWars.Core.Common.DTO.User;
using LeetWars.Core.DAL.Context;
using LeetWars.Core.DAL.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services;

public class UserService : BaseService, IUserService
{
public UserService(LeetWarsCoreContext context, IMapper mapper) : base(context, mapper)
private readonly IHttpContextAccessor _httpContextAccessor;
public UserService(LeetWarsCoreContext context, IMapper mapper, IHttpContextAccessor httpContextAccessor) : base(context, mapper)
{
_httpContextAccessor = httpContextAccessor;
}

public async Task<UserDto> CreateUserAsync(NewUserDto userDto)
Expand Down Expand Up @@ -43,4 +47,17 @@ public async Task<bool> CheckIsExistingEmail(string email)
bool isExistingEmail = await _context.Users.AnyAsync(u => u.Email.ToLower() == email.ToLower());
return isExistingEmail;
}

public async Task<UserDto> GetUserByUidAsync(string uid)
{
var user = await _context.Users.FirstOrDefaultAsync(u => u.Uid == uid)
?? throw new InvalidOperationException($"A user with uid {uid} is not found.");

return _mapper.Map<UserDto>(user);
}

public string GetCurrentUserUid()
{
return _httpContextAccessor.HttpContext?.User.Claims.First(i => i.Type == ClaimTypes.NameIdentifier).Value!;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Security.Claims;
using LeetWars.Core.BLL.Services;
using LeetWars.Core.Common.DTO.User;
using Microsoft.AspNetCore.Authorization;
Expand Down Expand Up @@ -32,4 +33,12 @@ public async Task<ActionResult<bool>> CheckEmail([FromQuery] string email)
var createdUser = await _userService.CheckIsExistingEmail(email);
return Ok(createdUser);
}

[HttpGet("current")]
public async Task<ActionResult<UserDto>> GetCurrentUser()
{
var uid = _userService.GetCurrentUserUid();
var user = await _userService.GetUserByUidAsync(uid);
return Ok(user);
}
}
1 change: 1 addition & 0 deletions backend/LeetWars.Core/LeetWars.Core.WebAPI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
.Build();

builder.Services.AddControllers();
builder.Services.AddHttpContextAccessor();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddLeetWarsCoreContext(builder.Configuration);
builder.Services.AddRabbitMqServices(builder.Configuration);
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AngularFireModule } from '@angular/fire/compat';
import { AngularFireAuthModule } from '@angular/fire/compat/auth';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CoreModule } from '@core/core.module';
import { environment } from '@env/environment';
import { NgxChartsModule } from '@swimlane/ngx-charts';
import { MonacoEditorModule } from 'ngx-monaco-editor-v2';
Expand All @@ -20,6 +21,7 @@ import { AppRoutingModule } from './app-routing.module';
imports: [
BrowserModule,
AppRoutingModule,
CoreModule,
BrowserAnimationsModule,
ToastrModule.forRoot(),
NgxChartsModule,
Expand Down
31 changes: 15 additions & 16 deletions frontend/src/app/core/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ export class AuthService {

// TODO: change parameters to DTO
public login(email: string, password: string) {
return this.createUser(
from(this.afAuth.signInWithEmailAndPassword(email, password)).pipe(
first(),
catchError((error) => throwError(error.message)),
),
return from(this.afAuth.signInWithEmailAndPassword(email, password)).pipe(
first(),
catchError((error) => throwError(error.message)),
switchMap(() => this.userService.getCurrentUser()),
tap((user) => this.setUserInfo(user)),
);
}

Expand Down Expand Up @@ -88,17 +88,16 @@ export class AuthService {

// TODO: Implemented only firebase part
public changePassword(password: string): Observable<void> {
return from(this.afAuth.currentUser)
.pipe(
first(),
switchMap((user) => {
if (user) {
return user.updatePassword(password);
}

throw new Error('User is not authorized');
}),
);
return from(this.afAuth.currentUser).pipe(
first(),
switchMap((user) => {
if (user) {
return user.updatePassword(password);
}

throw new Error('User is not authorized');
}),
);
}

public sendVerificationMail(): Observable<void> {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/app/core/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ export class UserService {
return this.httpService.postRequest<User>('/users', newUser);
}

public getCurrentUser(): Observable<User> {
return this.httpService.getRequest<User>('/users/current');
}

public checkEmail(email: string): Observable<boolean> {
return this.httpService.getRequest<boolean>(`/users/is-existing-email?email=${email}`);
}
Expand Down

0 comments on commit 46fabdd

Please sign in to comment.