Skip to content
Snippets Groups Projects
Commit 4832b42d authored by L4168's avatar L4168
Browse files

login now returns json on error

parent bd1b83d2
No related branches found
No related tags found
2 merge requests!59Development to master,!21Error handling
Pipeline #63952 passed
...@@ -10,15 +10,15 @@ import { GameDTO } from '../game/game.dto'; ...@@ -10,15 +10,15 @@ import { GameDTO } from '../game/game.dto';
@Injectable() @Injectable()
export class UserService { export class UserService {
constructor( constructor(
@InjectRepository(PersonEntity) @InjectRepository(PersonEntity)
private userRepository: Repository<PersonEntity>, private userRepository: Repository<PersonEntity>,
@InjectRepository(GameEntity) @InjectRepository(GameEntity)
private gameRepository: Repository<GameEntity> private gameRepository: Repository<GameEntity>
){} ) { }
async register(data: UserDTO) { async register(data: UserDTO) {
const { name } = data; const { name } = data;
let user = await this.userRepository.findOne({where: {name}}); let user = await this.userRepository.findOne({ where: { name } });
if (user) { if (user) {
throw new HttpException('User already exists', HttpStatus.BAD_REQUEST); throw new HttpException('User already exists', HttpStatus.BAD_REQUEST);
} }
...@@ -28,23 +28,20 @@ export class UserService { ...@@ -28,23 +28,20 @@ export class UserService {
} }
async login(data: UserDTO) { async login(data: UserDTO) {
try{ try {
const { name, password } = data; const { name, password } = data;
const user = await this.userRepository.findOne({ where: { name }}); const user = await this.userRepository.findOne({ where: { name } });
if (!user || !(await user.comparePassword(password))) { if (!user || !(await user.comparePassword(password))) {
throw new HttpException( throw new HttpException('invalid password', HttpStatus.BAD_REQUEST);
'Invalid username/password',
HttpStatus.BAD_REQUEST,
);
} }
return user.tokenObject(); return user.tokenObject();
}catch(error){ } catch (error) {
return error.message; throw new HttpException(error.message, HttpStatus.BAD_REQUEST);
} }
} }
// liitytään peliin // liitytään peliin
async joinGame(game: GameDTO, data: UserDTO){ async joinGame(game: GameDTO, data: UserDTO) {
try { try {
// etsi peli valinnan mukaan ja otetaan käyttäjän rooli kyseisestä pelistä talteen // etsi peli valinnan mukaan ja otetaan käyttäjän rooli kyseisestä pelistä talteen
const role = await this.gameRepository.findOne(); const role = await this.gameRepository.findOne();
...@@ -55,7 +52,7 @@ export class UserService { ...@@ -55,7 +52,7 @@ export class UserService {
} }
// liitytään factionii // liitytään factionii
async joinFaction(game: GameDTO, data: UserDTO): Promise<boolean>{ async joinFaction(game: GameDTO, data: UserDTO): Promise<boolean> {
try { try {
// tarkistetaan factionin salasana // tarkistetaan factionin salasana
return true; return true;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment