SES-84 Created User registration method #27

Merged
s426134 merged 3 commits from SES-84 into master 2020-12-13 17:07:56 +01:00
10 changed files with 128 additions and 25 deletions
Showing only changes of commit c9a4e7aa0d - Show all commits

View File

@ -3,6 +3,7 @@ import { SelectRoleComponent } from './app/components/select-role/select-role.co
import { SignInComponent } from './app/components/sign-in/sign-in.component';
import { RegistrationComponent } from './app/components/registration/registration.component';
import {GameMasterDashboardComponent} from './app/components/game-master-dashboard/game-master-dashboard.component';
import {PlayerDashboardComponent} from './app/components/player-dashboard/player-dashboard.component';
const routes: Routes = [
{
@ -24,6 +25,11 @@ const routes: Routes = [
path: 'gamemaster',
component: GameMasterDashboardComponent,
pathMatch: 'full'
},
{
path: 'player',
component: PlayerDashboardComponent,
pathMatch: 'full'
}
];

View File

@ -9,6 +9,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { SignInComponent } from './components/sign-in/sign-in.component';
import { RegistrationComponent } from './components/registration/registration.component';
import { GameMasterDashboardComponent} from './components/game-master-dashboard/game-master-dashboard.component';
import {PlayerDashboardComponent} from './components/player-dashboard/player-dashboard.component';
import {
MatCardModule,
MatTabsModule,
@ -18,6 +19,7 @@ import {
MatCheckboxModule,
MatIconModule, MatSidenavModule, MatToolbarModule, MatListModule
} from '@angular/material';
import {UserService} from '../services/user.service';
@NgModule({
declarations: [
@ -26,6 +28,7 @@ import {
SignInComponent,
RegistrationComponent,
GameMasterDashboardComponent,
PlayerDashboardComponent
],
imports: [
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
@ -45,7 +48,9 @@ BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
MatToolbarModule,
MatListModule,
],
providers: [],
providers: [
UserService
],
bootstrap: [AppComponent]
})
export class AppModule { }

View File

@ -2,11 +2,11 @@
<div formGroupName="newAccount" class="container">
<mat-icon matSuffix class="arrow-back" (click)="GoToLoginPage()">arrow_back</mat-icon>
<div class="primary-text header">Create an Account</div>
<mat-form-field class="form-container">
<input
matInput
formControlName="username"
formControlName="username"
placeholder="Username"
type="text"
required
@ -18,9 +18,9 @@
</mat-form-field>
<mat-form-field class="form-container">
<input
matInput
formControlName="password"
<input
matInput
formControlName="password"
required
placeholder="Password"
type="password"
@ -32,10 +32,10 @@
</mat-form-field>
<mat-form-field class="form-container">
<input
matInput
formControlName="confirmPassword"
required
<input
matInput
formControlName="confirmPassword"
required
placeholder="Confirm Password"
type="password"
name="confirmPassword"/>
@ -55,4 +55,4 @@
<mat-icon matSuffix class="arrow-forward">arrow_forward</mat-icon>
</button>
</div>
</div>
</div>

View File

@ -6,7 +6,7 @@
<mat-form-field class="form-container">
<input
matInput
formControlName="username"
formControlName="username"
placeholder="Username"
type="text"
required
@ -18,9 +18,9 @@
</mat-form-field>
<mat-form-field class="form-container">
<input
matInput
formControlName="password"
<input
matInput
formControlName="password"
required
placeholder="Password"
type="password"
@ -31,6 +31,9 @@
</mat-error>
</mat-form-field>
<mat-error *ngIf="apiError">
{{apiErrorMessage}}
</mat-error>
<button
mat-raised-button
class="btn-primary form-container"

View File

@ -1,16 +1,23 @@
import { Component } from '@angular/core';
import { Component, OnDestroy } from '@angular/core';
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
import { Router } from '@angular/router';
import {UserService} from '../../../services/user.service';
import {ErrorResponse} from '../../../types/ErrorResponse';
import {Subscription} from 'rxjs';
@Component({
selector: 'app-sign-in',
templateUrl: './sign-in.component.html',
styleUrls: ['./sign-in.component.css']
})
export class SignInComponent {
isExpanded = false;
export class SignInComponent implements OnDestroy {
allSubscriptions = new Subscription();
constructor(private router: Router, private formBuilder: FormBuilder) {}
isExpanded = false;
apiError = false;
apiErrorMessage = '';
constructor(private router: Router, private formBuilder: FormBuilder, private userService: UserService) {}
public signInFormGroup: FormGroup = this.formBuilder.group({
signIn: this.formBuilder.group({
@ -21,8 +28,19 @@ export class SignInComponent {
})
});
onLoginButtonClick(){
//TODO connect with backend and added router
onLoginButtonClick() {
this.allSubscriptions.add(
this.userService.tryLogin(
this.signInFormGroup.get('signIn').value['username'],
this.signInFormGroup.get('signIn').value['password']).subscribe(
(success) => {
this.router.navigate(['player']);
},
(error: ErrorResponse) => {
this.apiError = true;
this.apiErrorMessage = error.message;
}
));
}
onRegisterButtonClick(){
@ -41,4 +59,8 @@ export class SignInComponent {
toggle() {
this.isExpanded = !this.isExpanded;
}
ngOnDestroy() {
this.allSubscriptions.unsubscribe();
}
}

View File

@ -0,0 +1,31 @@
import {Inject, Injectable} from '@angular/core';
import {HttpClient, HttpParams} from '@angular/common/http';
import {Observable, of, throwError} from 'rxjs';
import {ErrorResponse} from '../types/ErrorResponse';
import {Either} from '../types/Either';
import {switchMap} from 'rxjs/operators';
Injectable({
providedIn: 'root'
})
export class UserService {
private baseUrl = 'api/user/';
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
this.baseUrl = baseUrl + this.baseUrl;
}
tryLogin(login: string, password: string): Observable<number> {
const params = new HttpParams()
.set('userName', login)
.set('password', password);
return this.http.get<Either<number, ErrorResponse>>(this.baseUrl + 'login', { params }).pipe(
switchMap(response => {
if (response.isLeft) {
return of(response.left);
} else {
return throwError(response.right);
}
})
);
}
}

View File

@ -0,0 +1,5 @@
export interface Either<TL, TR> {
left: TL;
right: TR;
isLeft: boolean;
}

View File

@ -0,0 +1,4 @@
export interface ErrorResponse {
code: number;
message: string;
}

View File

@ -7,6 +7,10 @@ using SessionCompanion.ViewModels.ApiResponses;
namespace SessionCompanion.Controllers
{
using SessionCompanion.Extensions.EitherType;
using SessionCompanion.ViewModels.ApiResponses;
using SessionCompanion.ViewModels.UserViewModels;
[Route("api/user")]
[ApiController]
public class UserController : Controller
@ -18,12 +22,27 @@ namespace SessionCompanion.Controllers
this._service = service;
}
/// <summary>
/// Metoda przyjmuje login oraz hasło i sprawdza czy istnieje użytkownik o podanych parametrach
/// </summary>
/// <param name="userName"> Nazwa użytkownika </param>
/// <param name="password"> Hasło </param>
/// <returns>Id użytkownika lub wiadomość błędu</returns>
[HttpGet("login")]
public async Task<IActionResult> Login(string userName, string password)
public async Task<Either<int, ErrorResponse>> Login(string userName, string password)
{
var User = await _service.SearchUserByNickname(userName);
if (User.Password == password) { return Json(User.Id); }
return BadRequest();
UserViewModel user = await _service.SearchUserByNickname(userName);
if (user != null && user.Password.Equals(password))
{
return user.Id;
}
return new ErrorResponse()
{
StatusCode = 403,
Message = "User name not found or incorrect password"
};
}

View File

@ -11,6 +11,14 @@
<param name="id">Identyfikator postaci</param>
<returns>ViewModel Postaci</returns>
</member>
<member name="M:SessionCompanion.Controllers.UserController.Login(System.String,System.String)">
<summary>
Metoda przyjmuje login oraz hasło i sprawdza czy istnieje użytkownik o podanych parametrach
</summary>
<param name="userName"> Nazwa użytkownika </param>
<param name="password"> Hasło </param>
<returns>Id użytkownika lub wiadomość błędu</returns>
</member>
<member name="F:SessionCompanion.Hubs.SessionHub.ConnectedCharacters">
<summary>
Lista zalogowanych graczy i identyfikator wybranej postaci