Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Commit

Permalink
feat: Add new login functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosPavajeau committed Feb 4, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 5a17dd9 commit 678886d
Showing 12 changed files with 49 additions and 18 deletions.
3 changes: 2 additions & 1 deletion Domain/Repositories/IApplicationUserRepository.cs
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ namespace Kaizen.Domain.Repositories
public interface IApplicationUserRepository : IRepositoryBase<ApplicationUser, string>
{
Task<IdentityResult> CreateAsync(ApplicationUser user, string password);
Task<SignInResult> Login(ApplicationUser user, string password);
Task<SignInResult> Login(ApplicationUser user, string password, bool isPersistent);
Task Logout();
Task<ApplicationUser> FindByNameAsync(string username);
Task<ApplicationUser> FindByEmailAsync(string email);
Task<ApplicationUser> FindByNameOrEmailAsync(string usernameOrEmail);
9 changes: 7 additions & 2 deletions Infrastructure/Repositories/ApplicationUserRepository.cs
Original file line number Diff line number Diff line change
@@ -30,9 +30,14 @@ IMailService mailService
_mailService = mailService;
}

public async Task<SignInResult> Login(ApplicationUser user, string password)
public async Task<SignInResult> Login(ApplicationUser user, string password, bool isPersistent)
{
return await _signInManager.PasswordSignInAsync(user, password, false, false);
return await _signInManager.PasswordSignInAsync(user, password, isPersistent, false);
}

public async Task Logout()
{
await _signInManager.SignOutAsync();
}

public async Task<ApplicationUser> FindByNameAsync(string username)
Original file line number Diff line number Diff line change
@@ -22,9 +22,11 @@ export class AuthenticationService {
return this.http.post<User>(`${AUTH_API_URL}/Login`, user);
}

logoutUser(): void {
logoutUser(): Observable<boolean> {
this.removeUser();
localStorage.removeItem('current_person');

return this.http.post<boolean>(`${AUTH_API_URL}/Logout`, null);
}

removeUser(): void {
1 change: 1 addition & 0 deletions Kaizen/ClientApp/src/app/core/models/login-request.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface LoginRequest {
usernameOrEmail: string;
password: string;
isPersistent: boolean;
}
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@
</mat-form-field>
</div>
<div class="form-element left session-toggle">
<mat-slide-toggle [color]="'primary'"
<mat-slide-toggle [color]="'primary'" formControlName="isPersistent"
><small>Mantener sesión iniciada</small></mat-slide-toggle
>
</div>
@@ -77,18 +77,17 @@
<div class="spacer"></div>
<div class="login-button">
<button
class="right"
type="submit"
mat-raised-button
class="royal_azure"
class="royal_azure right"
[appLoadingButton]="loading"
>
Iniciar sesión
</button>
</div>
</div>
<div>
<div class="text-center" sty>
<div class="text-center">
<a routerLink="/clients/new/register">
¿No estás registrado? Registrate.
</a>
Original file line number Diff line number Diff line change
@@ -34,7 +34,8 @@ export class UserLoginComponent implements OnInit, IForm {
initForm(): void {
this.loginForm = this.formBuilder.group({
usernameOrEmail: [ '', [ Validators.required, Validators.minLength(5), Validators.maxLength(15) ] ],
password: [ '', [ Validators.required, Validators.minLength(8), Validators.maxLength(30) ] ]
password: [ '', [ Validators.required, Validators.minLength(8), Validators.maxLength(30) ] ],
isPersistent: ['']
});
}

@@ -43,7 +44,8 @@ export class UserLoginComponent implements OnInit, IForm {
this.loading = true;
const loginRequest: LoginRequest = {
usernameOrEmail: this.controls['usernameOrEmail'].value,
password: this.controls['password'].value
password: this.controls['password'].value,
isPersistent: this.controls['isPersistent'].value || false
};

this.authService
Original file line number Diff line number Diff line change
@@ -64,6 +64,7 @@ <h2 class="mat-title">Ecolplag</h2>
class="nav-link"
mat-button
(click)="onLogout()"
[appLoadingButton]="isLogout"
*ngIf="isLogged"
fxShow="true"
fxHide.xs
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@ import { AuthenticationService } from '@core/authentication/authentication.servi
})
export class NavMenuComponent implements OnInit {
isLogged = false;
isLogout = false;

constructor(private authService: AuthenticationService) {}

@@ -22,8 +23,11 @@ export class NavMenuComponent implements OnInit {
}

onLogout(): void {
this.authService.logoutUser();
localStorage.removeItem('current_person');
window.location.reload();
this.isLogout = true;
this.authService.logoutUser().subscribe(result => {
if (result) {
location.reload();
}
});
}
}
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@
<mat-icon>settings</mat-icon>
<span class="nav-caption">Datos de acceso</span>
</a>
<a class="sidenav-link" mat-list-item (click)="logout()">
<a class="sidenav-link" mat-list-item (click)="logout()" [appLoadingButton]="isLogout">
<mat-icon>power_settings_new</mat-icon>
<span class="nav-caption">Cerrar sesión</span>
</a>
@@ -97,7 +97,7 @@
<mat-icon>settings</mat-icon>
<span>Datos de acceso</span>
</a>
<button mat-menu-item (click)="logout()">
<button mat-menu-item (click)="logout()" [appLoadingButton]="isLogout">
<mat-icon>power_settings_new</mat-icon>
<span class="nav-caption">Cerrar sesión</span>
</button>
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ export class DashboardLayoutComponent implements OnInit {
drawer: MatSidenav;

isSidenavClose = false;
isLogout = false;

menuOptions: DashboardCard[] = [];

@@ -51,7 +52,11 @@ export class DashboardLayoutComponent implements OnInit {
}

logout(): void {
this.authService.logoutUser();
location.reload();
this.isLogout = true;
this.authService.logoutUser().subscribe(result => {
if (result) {
location.reload();
}
});
}
}
10 changes: 9 additions & 1 deletion Kaizen/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -115,7 +115,7 @@ public async Task<ActionResult<ApplicationUserViewModel>> Login([FromBody] Login
return NotFound($"El usuario/email {loginRequest.UsernameOrEmail} no se encuentra registrado.");
}

Microsoft.AspNetCore.Identity.SignInResult result = await _userRepository.Login(user, loginRequest.Password);
Microsoft.AspNetCore.Identity.SignInResult result = await _userRepository.Login(user, loginRequest.Password, loginRequest.IsPersistent);
if (!result.Succeeded)
{
throw new IncorrectPasswordException();
@@ -124,6 +124,14 @@ public async Task<ActionResult<ApplicationUserViewModel>> Login([FromBody] Login
return await GenerateAuthenticateUser(user);
}

[HttpPost("[action]")]
[ValidateAntiForgeryToken]
public async Task<ActionResult<bool>> Logout()
{
await _userRepository.Logout();
return true;
}

private async Task<ActionResult<ApplicationUserViewModel>> GenerateAuthenticateUser(ApplicationUser user)
{
ApplicationUserViewModel userView = _mapper.Map<ApplicationUserViewModel>(user);
3 changes: 3 additions & 0 deletions Kaizen/Models/ApplicationUser/LoginRequest.cs
Original file line number Diff line number Diff line change
@@ -9,5 +9,8 @@ public class LoginRequest

[Required(ErrorMessage = "La contraseña de acceso es requerida")]
public string Password { get; set; }

[Required]
public bool IsPersistent { get; set; }
}
}

0 comments on commit 678886d

Please sign in to comment.