SES-89 Added api connection service and display error
This commit is contained in:
parent
7b122e160e
commit
b9928d6ce0
@ -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'
|
||||
}
|
||||
];
|
||||
|
||||
|
@ -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 { }
|
||||
|
@ -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"
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { Component } from '@angular/core';
|
||||
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
|
||||
import { Router } from '@angular/router';
|
||||
import {UserService} from '../../../services/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-sign-in',
|
||||
@ -9,8 +10,10 @@ import { Router } from '@angular/router';
|
||||
})
|
||||
export class SignInComponent {
|
||||
isExpanded = false;
|
||||
apiError = false;
|
||||
apiErrorMessage = '';
|
||||
|
||||
constructor(private router: Router, private formBuilder: FormBuilder) {}
|
||||
constructor(private router: Router, private formBuilder: FormBuilder, private userService: UserService) {}
|
||||
|
||||
public signInFormGroup: FormGroup = this.formBuilder.group({
|
||||
signIn: this.formBuilder.group({
|
||||
@ -21,8 +24,16 @@ export class SignInComponent {
|
||||
})
|
||||
});
|
||||
|
||||
onLoginButtonClick(){
|
||||
//TODO connect with backend and added router
|
||||
async onLoginButtonClick() {
|
||||
const result = await this.userService.tryLogin(
|
||||
this.signInFormGroup.get('signIn').value['username'],
|
||||
this.signInFormGroup.get('signIn').value['password']);
|
||||
if (result.isLeft) {
|
||||
await this.router.navigate(['player']);
|
||||
} else {
|
||||
this.apiError = true;
|
||||
this.apiErrorMessage = result.right.message;
|
||||
}
|
||||
}
|
||||
|
||||
onRegisterButtonClick(){
|
||||
|
@ -0,0 +1,22 @@
|
||||
import {Inject, Injectable} from '@angular/core';
|
||||
import {HttpClient, HttpParams} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {ErrorResponse} from '../types/ErrorResponse';
|
||||
import {Either} from '../types/Either';
|
||||
|
||||
Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class UserService {
|
||||
private baseUrl = 'api/user/';
|
||||
constructor(private http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
|
||||
this.baseUrl = baseUrl + this.baseUrl;
|
||||
}
|
||||
|
||||
async tryLogin(login: string, password: string): Promise<Either<number, ErrorResponse>> {
|
||||
const params = new HttpParams()
|
||||
.set('userName', login)
|
||||
.set('password', password);
|
||||
return await this.http.get<Either<number, ErrorResponse>>(this.baseUrl + 'login', { params }).toPromise();
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
export interface Either<TL, TR> {
|
||||
left: TL;
|
||||
right: TR;
|
||||
isLeft: boolean;
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
export interface ErrorResponse {
|
||||
code: number;
|
||||
message: string;
|
||||
}
|
@ -4,6 +4,10 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace SessionCompanion.Controllers
|
||||
{
|
||||
using SessionCompanion.Extensions.EitherType;
|
||||
using SessionCompanion.ViewModels.ApiResponses;
|
||||
using SessionCompanion.ViewModels.UserViewModels;
|
||||
|
||||
[Route("api/user")]
|
||||
[ApiController]
|
||||
public class UserController : Controller
|
||||
@ -15,12 +19,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"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user