SES-89 Connected to backend for login #28
@ -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 { SignInComponent } from './app/components/sign-in/sign-in.component';
|
||||||
import { RegistrationComponent } from './app/components/registration/registration.component';
|
import { RegistrationComponent } from './app/components/registration/registration.component';
|
||||||
import {GameMasterDashboardComponent} from './app/components/game-master-dashboard/game-master-dashboard.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 = [
|
const routes: Routes = [
|
||||||
{
|
{
|
||||||
@ -24,6 +25,11 @@ const routes: Routes = [
|
|||||||
path: 'gamemaster',
|
path: 'gamemaster',
|
||||||
component: GameMasterDashboardComponent,
|
component: GameMasterDashboardComponent,
|
||||||
pathMatch: 'full'
|
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 { SignInComponent } from './components/sign-in/sign-in.component';
|
||||||
import { RegistrationComponent } from './components/registration/registration.component';
|
import { RegistrationComponent } from './components/registration/registration.component';
|
||||||
import { GameMasterDashboardComponent} from './components/game-master-dashboard/game-master-dashboard.component';
|
import { GameMasterDashboardComponent} from './components/game-master-dashboard/game-master-dashboard.component';
|
||||||
|
import {PlayerDashboardComponent} from './components/player-dashboard/player-dashboard.component';
|
||||||
import {
|
import {
|
||||||
MatCardModule,
|
MatCardModule,
|
||||||
MatTabsModule,
|
MatTabsModule,
|
||||||
@ -18,6 +19,7 @@ import {
|
|||||||
MatCheckboxModule,
|
MatCheckboxModule,
|
||||||
MatIconModule, MatSidenavModule, MatToolbarModule, MatListModule
|
MatIconModule, MatSidenavModule, MatToolbarModule, MatListModule
|
||||||
} from '@angular/material';
|
} from '@angular/material';
|
||||||
|
import {UserService} from '../services/user.service';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
@ -26,6 +28,7 @@ import {
|
|||||||
SignInComponent,
|
SignInComponent,
|
||||||
RegistrationComponent,
|
RegistrationComponent,
|
||||||
GameMasterDashboardComponent,
|
GameMasterDashboardComponent,
|
||||||
|
PlayerDashboardComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
|
BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
|
||||||
@ -45,7 +48,9 @@ BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
|
|||||||
MatToolbarModule,
|
MatToolbarModule,
|
||||||
MatListModule,
|
MatListModule,
|
||||||
],
|
],
|
||||||
providers: [],
|
providers: [
|
||||||
|
UserService
|
||||||
|
],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
export class AppModule { }
|
export class AppModule { }
|
||||||
|
@ -31,6 +31,9 @@
|
|||||||
</mat-error>
|
</mat-error>
|
||||||
</mat-form-field>
|
</mat-form-field>
|
||||||
|
|
||||||
|
<mat-error *ngIf="apiError">
|
||||||
|
{{apiErrorMessage}}
|
||||||
|
</mat-error>
|
||||||
<button
|
<button
|
||||||
mat-raised-button
|
mat-raised-button
|
||||||
class="btn-primary form-container"
|
class="btn-primary form-container"
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
|
import { FormGroup, Validators, FormBuilder } from '@angular/forms';
|
||||||
import { Router } from '@angular/router';
|
import { Router } from '@angular/router';
|
||||||
|
import {UserService} from '../../../services/user.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-sign-in',
|
selector: 'app-sign-in',
|
||||||
@ -9,8 +10,10 @@ import { Router } from '@angular/router';
|
|||||||
})
|
})
|
||||||
export class SignInComponent {
|
export class SignInComponent {
|
||||||
isExpanded = false;
|
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({
|
public signInFormGroup: FormGroup = this.formBuilder.group({
|
||||||
signIn: this.formBuilder.group({
|
signIn: this.formBuilder.group({
|
||||||
@ -21,8 +24,16 @@ export class SignInComponent {
|
|||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
onLoginButtonClick(){
|
async onLoginButtonClick() {
|
||||||
//TODO connect with backend and added router
|
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(){
|
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
|
namespace SessionCompanion.Controllers
|
||||||
{
|
{
|
||||||
|
using SessionCompanion.Extensions.EitherType;
|
||||||
|
using SessionCompanion.ViewModels.ApiResponses;
|
||||||
|
using SessionCompanion.ViewModels.UserViewModels;
|
||||||
|
|
||||||
[Route("api/user")]
|
[Route("api/user")]
|
||||||
[ApiController]
|
[ApiController]
|
||||||
public class UserController : Controller
|
public class UserController : Controller
|
||||||
@ -15,12 +19,27 @@ namespace SessionCompanion.Controllers
|
|||||||
this._service = service;
|
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")]
|
[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);
|
UserViewModel user = await _service.SearchUserByNickname(userName);
|
||||||
if (User.Password == password) { return Json(User.Id); }
|
|
||||||
return BadRequest();
|
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>
|
<param name="id">Identyfikator postaci</param>
|
||||||
<returns>ViewModel Postaci</returns>
|
<returns>ViewModel Postaci</returns>
|
||||||
</member>
|
</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">
|
<member name="F:SessionCompanion.Hubs.SessionHub.ConnectedCharacters">
|
||||||
<summary>
|
<summary>
|
||||||
Lista zalogowanych graczy i identyfikator wybranej postaci
|
Lista zalogowanych graczy i identyfikator wybranej postaci
|
||||||
|
Loading…
Reference in New Issue
Block a user