Skip to content

Commit 52d3bfc

Browse files
authored
Merge pull request #7 from eug-vs/auth
Setup authentication
2 parents 1860396 + 1ab1995 commit 52d3bfc

File tree

10 files changed

+225
-3
lines changed

10 files changed

+225
-3
lines changed

app.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import feathers from '@feathersjs/feathers';
22
import express from '@feathersjs/express';
33
import socketio from '@feathersjs/socketio';
4+
import configuration from '@feathersjs/configuration'
45
import '@feathersjs/transport-commons';
56
import cors from 'cors';
67

@@ -15,6 +16,7 @@ app.use(express.static(__dirname));
1516
app.use(express.errorHandler());
1617
app.use(cors());
1718

19+
app.configure(configuration());
1820
app.configure(express.rest());
1921
app.configure(socketio());
2022
app.configure(services);

config/default.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"authentication": {
3+
"secret": "2JtbBy2ycQ1V3kQfPcSgXYmFsB7",
4+
"service": "users",
5+
"authStrategies": [
6+
"jwt",
7+
"local"
8+
],
9+
"local": {
10+
"usernameField": "name",
11+
"passwordField": "password"
12+
}
13+
}
14+
}

models/users/user.schema.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ export interface User {
66
age?: number;
77
}
88

9-
export interface UserSchema extends Document, User {}
9+
export interface UserSchema extends Document, User {
10+
password: string;
11+
}
1012

1113
export const userSchema = new Schema({
1214
name: String,
15+
password: String,
1316
avatarUrl: {
1417
type: String,
1518
required: false

package-lock.json

Lines changed: 165 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"author": "",
1111
"license": "ISC",
1212
"dependencies": {
13+
"@feathersjs/authentication": "^4.5.3",
14+
"@feathersjs/authentication-local": "^4.5.4",
15+
"@feathersjs/configuration": "^4.5.3",
1316
"@feathersjs/express": "^4.5.4",
1417
"@feathersjs/feathers": "^4.5.3",
1518
"@feathersjs/socketio": "^4.5.4",

populateDb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ const createPoll = (authorId: string): Promise<PollSchema> => {
4646
const createUser = (name: string): Promise<UserSchema> => {
4747
return app.service('users').create({
4848
avatarUrl: _.sample(imageUrls) || '',
49+
password: 'supersecret',
4950
name
5051
});
5152
};

services/auth/auth.service.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {
2+
AuthenticationService,
3+
JWTStrategy
4+
} from '@feathersjs/authentication';
5+
import { LocalStrategy } from '@feathersjs/authentication-local';
6+
import { Application } from '@feathersjs/express';
7+
8+
export default (app: Application): void => {
9+
const authentication = new AuthenticationService(app);
10+
11+
authentication.register('local', new LocalStrategy());
12+
authentication.register('jwt', new JWTStrategy());
13+
14+
app.use('/authentication', authentication);
15+
};
16+

services/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import { Application } from '@feathersjs/express';
22
import Users from './users/users.service';
33
import Polls from './polls/polls.service';
44
import Profiles from './profiles/profiles.service';
5+
import Auth from './auth/auth.service';
56

67
export default (app: Application): void => {
8+
app.configure(Auth);
79
app.configure(Users);
810
app.configure(Polls);
911
app.configure(Profiles);

services/users/users.hooks.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { hooks } from '@feathersjs/authentication-local';
2+
3+
const hashPassword = hooks.hashPassword('password');
4+
const protectPassword = hooks.protect('password');
5+
6+
export default {
7+
after: {
8+
all: [protectPassword]
9+
},
10+
before: {
11+
create: [hashPassword],
12+
patch: [hashPassword],
13+
update: [hashPassword]
14+
}
15+
};
16+

services/users/users.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Application } from '@feathersjs/express';
22
import service from 'feathers-mongoose';
33
import Model from '../../models/users/user.model';
4+
import hooks from './users.hooks';
45

56
const UserService = service({ Model });
67

78
export default (app: Application): void => {
89
app.use('/users', UserService);
10+
app.service('users').hooks(hooks);
911
};
1012

0 commit comments

Comments
 (0)