diff --git a/src/mapmarkers/mapmarker.entity.ts b/src/mapmarkers/mapmarker.entity.ts index 41a955d3998702c15ca0429b9e92bad936ced49c..bc6d33112149f23e52e493c374be19ca0f18e536 100644 --- a/src/mapmarkers/mapmarker.entity.ts +++ b/src/mapmarkers/mapmarker.entity.ts @@ -1,6 +1,6 @@ import { Entity, Column, PrimaryGeneratedColumn, Timestamp, ManyToOne } from 'typeorm'; -import { PersonEntity } from 'src/user/user.entity' +import { PersonEntity } from '../user/user.entity' /* Entity: MapMarker diff --git a/src/mapmarkers/mapmarker.service.ts b/src/mapmarkers/mapmarker.service.ts index c47ddf0f9ce2773addb34c05e8a77d296b8cf713..b0abf869d6fae57db261a9b904b8fa6a7e1faf39 100644 --- a/src/mapmarkers/mapmarker.service.ts +++ b/src/mapmarkers/mapmarker.service.ts @@ -4,7 +4,7 @@ import { InjectRepository } from "@nestjs/typeorm"; import { MapMarkerEntity } from './mapmarker.entity'; import { MapMarkerDTO } from './mapmarker.dto'; -import { PersonEntity } from 'dist/user/user.entity'; +import { PersonEntity } from '../user/user.entity'; import { userInfo } from 'os'; @Injectable() diff --git a/src/mapmarkers/mapmarkers.controller.ts b/src/mapmarkers/mapmarkers.controller.ts index 6aa89c9f3949e1fa39e64779bbc073df46ac0c6b..d2336b71fe5db29257c72a4a68c44c5de4336cbc 100644 --- a/src/mapmarkers/mapmarkers.controller.ts +++ b/src/mapmarkers/mapmarkers.controller.ts @@ -2,8 +2,8 @@ import { Controller, Body, Get, Put, UseGuards } from '@nestjs/common'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerDTO } from './mapmarker.dto'; -import { AuthGuard } from 'dist/shared/auth.guard'; -import { User } from 'src/user/user.decorator'; +import { AuthGuard, RolesGuard } from '../shared/auth.guard'; +import { User } from '../user/user.decorator'; @Controller('mapmarkers') export class MapMarkersController { @@ -23,6 +23,20 @@ export class MapMarkersController { // return all markers through service @Get('getall') async getAll(){ - return this.mapmarkerservice.getAllMarkers(); + try{ + return this.mapmarkerservice.getAllMarkers(); + }catch(error){ + return error.message; + } + } + + @Get('test') + @UseGuards(new RolesGuard()) + async test(){ + try { + + } catch (error) { + return error.message; + } } } diff --git a/src/mapmarkers/mapmarkers.module.ts b/src/mapmarkers/mapmarkers.module.ts index 06c45c209fa8bd7f876359fa447e69c084ed3915..9fda8c9309e32239634c65ce72c4816860aa8cec 100644 --- a/src/mapmarkers/mapmarkers.module.ts +++ b/src/mapmarkers/mapmarkers.module.ts @@ -4,7 +4,7 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { MapMarkersController } from './mapmarkers.controller'; import { MapMarkerService } from './mapmarker.service'; import { MapMarkerEntity } from './mapmarker.entity'; -import { PersonEntity } from 'src/user/user.entity'; +import { PersonEntity } from '../user/user.entity'; @Module({ imports: [TypeOrmModule.forFeature([MapMarkerEntity, PersonEntity])], diff --git a/src/shared/auth.guard.ts b/src/shared/auth.guard.ts index 78d66f86757f1f5803338440839afeb3610ed1d7..43fb6f0c3fa79992f66f46575d2d57a11c9cf238 100644 --- a/src/shared/auth.guard.ts +++ b/src/shared/auth.guard.ts @@ -4,27 +4,51 @@ import * as jwt from 'jsonwebtoken'; @Injectable() export class AuthGuard implements CanActivate { + // check for logged in user async canActivate(context: ExecutionContext): Promise<boolean> { + // get request const request = context.switchToHttp().getRequest(); + // check for authorization header if (!request.headers.authorization) { return false; } + // validate token request.user = await this.validateToken(request.headers.authorization); return true; } + // validate token async validateToken(auth: string) { + // check if header contains Bearer if (auth.split(" ")[0] !== 'Bearer') { throw new HttpException('Invalid token', HttpStatus.FORBIDDEN); } + // get the token const token = auth.split(" ")[1]; try { + // return token. return await jwt.verify(token, process.env.SECRET); } catch (err) { const message = `Token error: ${err.message || err.name}` throw new HttpException(message, HttpStatus.FORBIDDEN); } } +} + +export class RolesGuard implements CanActivate{ + // check for logged in user + async canActivate(context: ExecutionContext): Promise<boolean> { + // get request + const request = context.switchToHttp().getRequest(); + // check for authorization header + if (!request.headers.authorization) { + return false; + } + + // check for role + + return true; + } } \ No newline at end of file diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 363e894a9e7fd0cb34df6dbd8a81b496f623bdea..2910ed9c445715f94c698935428bf2c85ef6d717 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -2,8 +2,8 @@ import { Controller, Post, Body, UsePipes, Get, UseGuards } from '@nestjs/common import { UserService } from './user.service'; import { UserDTO } from './user.dto'; -import { AuthGuard } from 'src/shared/auth.guard'; -import { ValidationPipe } from 'src/shared/validation.pipe'; +import { AuthGuard } from '../shared/auth.guard'; +import { ValidationPipe } from '../shared/validation.pipe'; @Controller('user') export class UserController { diff --git a/src/user/user.entity.ts b/src/user/user.entity.ts index e718ce4834b859a3b4bd316a3b78efb313437035..616da0095ed802139da620778c22662e2d2ed344 100644 --- a/src/user/user.entity.ts +++ b/src/user/user.entity.ts @@ -1,7 +1,7 @@ import { Entity, Column, PrimaryGeneratedColumn, BeforeInsert, OneToMany } from 'typeorm'; import * as bcrypt from 'bcryptjs'; import * as jwt from 'jsonwebtoken'; -import { MapMarkerEntity } from 'src/mapmarkers/mapmarker.entity'; +import { MapMarkerEntity } from '../mapmarkers/mapmarker.entity'; @Entity('Person') export class PersonEntity { diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 0b42bf9689f1cf7a697baa5c0093e02105794e6a..eb9985a651e292b97ba2b7bc993e3a35da57b66d 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -21,14 +21,18 @@ export class UserService { } async login(data: UserDTO) { - const { name, password } = data; - const user = await this.userRepository.findOne({ where: { name }}); - if (!user || !(await user.comparePassword(password))) { - throw new HttpException( - 'Invalid username/password', - HttpStatus.BAD_REQUEST, - ); + try{ + const { name, password } = data; + const user = await this.userRepository.findOne({ where: { name }}); + if (!user || !(await user.comparePassword(password))) { + throw new HttpException( + 'Invalid username/password', + HttpStatus.BAD_REQUEST, + ); + } + return user.tokenObject(); + }catch(error){ + return error.message; } - return user.tokenObject(); } }