Skip to content

Commit a37e374

Browse files
feat: Adds auth
1 parent c1bb34f commit a37e374

29 files changed

+589
-190
lines changed

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
"!*/__tests__"
5050
],
5151
"dependencies": {
52+
"@loopback/authentication": "^7.2.0",
53+
"@loopback/authentication-jwt": "^0.8.1",
5254
"@loopback/boot": "^3.3.0",
5355
"@loopback/build": "^6.3.0",
5456
"@loopback/core": "^2.15.0",
@@ -59,6 +61,7 @@
5961
"@loopback/service-proxy": "^3.1.0",
6062
"@loopback/testlab": "^3.3.0",
6163
"@types/node": "^10.17.55",
64+
"@types/validator": "^13.1.3",
6265
"dotenv": "^8.2.0",
6366
"eslint": "^7.22.0",
6467
"loopback-connector-mongodb": "^5.2.3",

Diff for: src/application.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import {AuthenticationComponent} from '@loopback/authentication';
2+
import {
3+
JWTAuthenticationComponent,
4+
UserServiceBindings,
5+
} from '@loopback/authentication-jwt';
16
import {BootMixin} from '@loopback/boot';
27
import {ApplicationConfig} from '@loopback/core';
8+
import {RepositoryMixin} from '@loopback/repository';
9+
import {RestApplication} from '@loopback/rest';
310
import {
411
RestExplorerBindings,
512
RestExplorerComponent,
613
} from '@loopback/rest-explorer';
7-
import {RepositoryMixin} from '@loopback/repository';
8-
import {RestApplication} from '@loopback/rest';
914
import {ServiceMixin} from '@loopback/service-proxy';
1015
import path from 'path';
16+
import {MongoDataSource} from './datasources';
1117
import {MySequence} from './sequence';
1218

1319
export {ApplicationConfig};
@@ -40,5 +46,10 @@ export class MovieStoreApplication extends BootMixin(
4046
nested: true,
4147
},
4248
};
49+
this.component(AuthenticationComponent);
50+
// Mount jwt component
51+
this.component(JWTAuthenticationComponent);
52+
// Bind datasource
53+
this.dataSource(MongoDataSource, UserServiceBindings.DATASOURCE_NAME);
4354
}
4455
}

Diff for: src/controllers/actor-info.controller.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {ActorInfo} from '../models';
2122
import {ActorInfoRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class ActorInfoController {
2426
constructor(
2527
@repository(ActorInfoRepository)
26-
public actorInfoRepository : ActorInfoRepository,
28+
public actorInfoRepository: ActorInfoRepository,
2729
) {}
2830

2931
@post('/actor-infos')
@@ -106,7 +108,8 @@ export class ActorInfoController {
106108
})
107109
async findById(
108110
@param.path.number('id') id: number,
109-
@param.filter(ActorInfo, {exclude: 'where'}) filter?: FilterExcludingWhere<ActorInfo>
111+
@param.filter(ActorInfo, {exclude: 'where'})
112+
filter?: FilterExcludingWhere<ActorInfo>,
110113
): Promise<ActorInfo> {
111114
return this.actorInfoRepository.findById(id, filter);
112115
}

Diff for: src/controllers/actor.controller.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {Actor} from '../models';
2122
import {ActorRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class ActorController {
2426
constructor(
2527
@repository(ActorRepository)
26-
public actorRepository : ActorRepository,
28+
public actorRepository: ActorRepository,
2729
) {}
2830

2931
@post('/actors')
@@ -52,9 +54,7 @@ export class ActorController {
5254
description: 'Actor model count',
5355
content: {'application/json': {schema: CountSchema}},
5456
})
55-
async count(
56-
@param.where(Actor) where?: Where<Actor>,
57-
): Promise<Count> {
57+
async count(@param.where(Actor) where?: Where<Actor>): Promise<Count> {
5858
return this.actorRepository.count(where);
5959
}
6060

@@ -70,9 +70,7 @@ export class ActorController {
7070
},
7171
},
7272
})
73-
async find(
74-
@param.filter(Actor) filter?: Filter<Actor>,
75-
): Promise<Actor[]> {
73+
async find(@param.filter(Actor) filter?: Filter<Actor>): Promise<Actor[]> {
7674
return this.actorRepository.find(filter);
7775
}
7876

@@ -106,7 +104,8 @@ export class ActorController {
106104
})
107105
async findById(
108106
@param.path.number('id') id: number,
109-
@param.filter(Actor, {exclude: 'where'}) filter?: FilterExcludingWhere<Actor>
107+
@param.filter(Actor, {exclude: 'where'})
108+
filter?: FilterExcludingWhere<Actor>,
110109
): Promise<Actor> {
111110
return this.actorRepository.findById(id, filter);
112111
}

Diff for: src/controllers/address.controller.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {Address} from '../models';
2122
import {AddressRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class AddressController {
2426
constructor(
2527
@repository(AddressRepository)
26-
public addressRepository : AddressRepository,
28+
public addressRepository: AddressRepository,
2729
) {}
2830

2931
@post('/addresses')
@@ -52,9 +54,7 @@ export class AddressController {
5254
description: 'Address model count',
5355
content: {'application/json': {schema: CountSchema}},
5456
})
55-
async count(
56-
@param.where(Address) where?: Where<Address>,
57-
): Promise<Count> {
57+
async count(@param.where(Address) where?: Where<Address>): Promise<Count> {
5858
return this.addressRepository.count(where);
5959
}
6060

@@ -106,7 +106,8 @@ export class AddressController {
106106
})
107107
async findById(
108108
@param.path.number('id') id: number,
109-
@param.filter(Address, {exclude: 'where'}) filter?: FilterExcludingWhere<Address>
109+
@param.filter(Address, {exclude: 'where'})
110+
filter?: FilterExcludingWhere<Address>,
110111
): Promise<Address> {
111112
return this.addressRepository.findById(id, filter);
112113
}

Diff for: src/controllers/category.controller.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {Category} from '../models';
2122
import {CategoryRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class CategoryController {
2426
constructor(
2527
@repository(CategoryRepository)
26-
public categoryRepository : CategoryRepository,
28+
public categoryRepository: CategoryRepository,
2729
) {}
2830

2931
@post('/categories')
@@ -52,9 +54,7 @@ export class CategoryController {
5254
description: 'Category model count',
5355
content: {'application/json': {schema: CountSchema}},
5456
})
55-
async count(
56-
@param.where(Category) where?: Where<Category>,
57-
): Promise<Count> {
57+
async count(@param.where(Category) where?: Where<Category>): Promise<Count> {
5858
return this.categoryRepository.count(where);
5959
}
6060

@@ -106,7 +106,8 @@ export class CategoryController {
106106
})
107107
async findById(
108108
@param.path.number('id') id: number,
109-
@param.filter(Category, {exclude: 'where'}) filter?: FilterExcludingWhere<Category>
109+
@param.filter(Category, {exclude: 'where'})
110+
filter?: FilterExcludingWhere<Category>,
110111
): Promise<Category> {
111112
return this.categoryRepository.findById(id, filter);
112113
}

Diff for: src/controllers/city.controller.ts

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {City} from '../models';
2122
import {CityRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class CityController {
2426
constructor(
2527
@repository(CityRepository)
26-
public cityRepository : CityRepository,
28+
public cityRepository: CityRepository,
2729
) {}
2830

2931
@post('/cities')
@@ -52,9 +54,7 @@ export class CityController {
5254
description: 'City model count',
5355
content: {'application/json': {schema: CountSchema}},
5456
})
55-
async count(
56-
@param.where(City) where?: Where<City>,
57-
): Promise<Count> {
57+
async count(@param.where(City) where?: Where<City>): Promise<Count> {
5858
return this.cityRepository.count(where);
5959
}
6060

@@ -70,9 +70,7 @@ export class CityController {
7070
},
7171
},
7272
})
73-
async find(
74-
@param.filter(City) filter?: Filter<City>,
75-
): Promise<City[]> {
73+
async find(@param.filter(City) filter?: Filter<City>): Promise<City[]> {
7674
return this.cityRepository.find(filter);
7775
}
7876

@@ -106,7 +104,7 @@ export class CityController {
106104
})
107105
async findById(
108106
@param.path.number('id') id: number,
109-
@param.filter(City, {exclude: 'where'}) filter?: FilterExcludingWhere<City>
107+
@param.filter(City, {exclude: 'where'}) filter?: FilterExcludingWhere<City>,
110108
): Promise<City> {
111109
return this.cityRepository.findById(id, filter);
112110
}

Diff for: src/controllers/country.controller.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import {authenticate} from '@loopback/authentication';
12
import {
23
Count,
34
CountSchema,
@@ -7,23 +8,24 @@ import {
78
Where,
89
} from '@loopback/repository';
910
import {
10-
post,
11-
param,
11+
del,
1212
get,
1313
getModelSchemaRef,
14+
param,
1415
patch,
16+
post,
1517
put,
16-
del,
1718
requestBody,
1819
response,
1920
} from '@loopback/rest';
2021
import {Country} from '../models';
2122
import {CountryRepository} from '../repositories';
2223

24+
@authenticate('jwt')
2325
export class CountryController {
2426
constructor(
2527
@repository(CountryRepository)
26-
public countryRepository : CountryRepository,
28+
public countryRepository: CountryRepository,
2729
) {}
2830

2931
@post('/countries')
@@ -52,9 +54,7 @@ export class CountryController {
5254
description: 'Country model count',
5355
content: {'application/json': {schema: CountSchema}},
5456
})
55-
async count(
56-
@param.where(Country) where?: Where<Country>,
57-
): Promise<Count> {
57+
async count(@param.where(Country) where?: Where<Country>): Promise<Count> {
5858
return this.countryRepository.count(where);
5959
}
6060

@@ -106,7 +106,8 @@ export class CountryController {
106106
})
107107
async findById(
108108
@param.path.number('id') id: number,
109-
@param.filter(Country, {exclude: 'where'}) filter?: FilterExcludingWhere<Country>
109+
@param.filter(Country, {exclude: 'where'})
110+
filter?: FilterExcludingWhere<Country>,
110111
): Promise<Country> {
111112
return this.countryRepository.findById(id, filter);
112113
}

0 commit comments

Comments
 (0)