Skip to content

Commit

Permalink
Merge pull request #49 from feathersjs/decoupling
Browse files Browse the repository at this point in the history
Decoupling
  • Loading branch information
ekryski authored and daffl committed Aug 29, 2018
1 parent bd3ad6c commit 7121bbe
Show file tree
Hide file tree
Showing 45 changed files with 2,815 additions and 1,189 deletions.
4 changes: 3 additions & 1 deletion packages/authentication/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
"before": true,
"beforeEach": true,
"after": true,
"afterEach": true
"afterEach": true,
"document": true,
"localStorage": true
}
}
3 changes: 2 additions & 1 deletion packages/authentication/.travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js
node_js:
- '4.2.6'
- 'node'
- 'iojs'
- '0.12'
before_install:
- sudo apt-get install python-software-properties
- sudo add-apt-repository ppa:ubuntu-toolchain-r/test -y
Expand Down
84 changes: 57 additions & 27 deletions packages/authentication/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,59 +26,89 @@ Please refer to the [Authentication documentation](http://docs.feathersjs.com/au
Here's an example of a Feathers server that uses `feathers-authentication` for local auth. It includes a `users` service that uses `feathers-mongoose`. *Note that it does NOT implement any authorization.*

```js
/* * * Import Feathers and Plugins * * */
var feathers = require('feathers');
var hooks = require('feathers-hooks');
var bodyParser = require('body-parser');
var feathersAuth = require('feathers-authentication').default;
var authHooks = require('feathers-authentication').hooks;

/* * * Prepare the Mongoose service * * */
var mongooseService = require('feathers-mongoose');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
import feathers from 'feathers';
import hooks from 'feathers-hooks';
import bodyParser from 'body-parser';
import authentication from 'feathers-authentication';
import { hooks as authHooks } from 'feathers-authentication';
import mongoose from 'mongoose';
import service from 'feathers-mongoose';

const port = 3030;
const Schema = mongoose.Schema;
const UserSchema = new Schema({
username: {type: String, required: true, unique: true},
password: {type: String, required: true },
createdAt: {type: Date, 'default': Date.now},
updatedAt: {type: Date, 'default': Date.now}
});
var UserModel = mongoose.model('User', UserSchema);
let UserModel = mongoose.model('User', UserSchema);

/* * * Connect the MongoDB Server * * */
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27017/feathers');

/* * * Initialize the App and Plugins * * */
var app = feathers()
let app = feathers()
.configure(feathers.rest())
.configure(feathers.socketio())
.configure(hooks())
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))

// Configure feathers-authentication
.configure(feathersAuth({
secret: 'feathers-rocks'
.configure(authentication({
token: {
secret: 'feathers-rocks'
},
local: {
usernameField: 'username'
},
facebook: {
clientID: '',
clientSecret: ''
}
}));

/* * * Setup the User Service and hashPassword Hook * * */
app.use('/api/users', new mongooseService('user', UserModel))
var service = app.service('/api/users');
service.before({
app.use('/users', new service('user', {Model: UserModel}))

let userService = app.service('users');
userService.before({
create: [authHooks.hashPassword('password')]
});

/* * * Start the Server * * */
var port = 3030;
var server = app.listen(port);
let server = app.listen(port);
server.on('listening', function() {
console.log(`Feathers application started on localhost:3030);
console.log(`Feathers application started on localhost:${port}`);
});
```

## Client use

You can use the client in the Browser, in NodeJS and in React Native.

```js
import io from 'socket.io-client';
import feathers from 'feathers/client';
import hooks from `feathers-hooks`;
import socketio from 'feathers-socketio/client';
import authentication from 'feathers-authentication/client';

const socket = io('http://path/to/api');
const app = feathers()
.configure(socketio(socket)) // you could use Primus or REST instead
.configure(hooks())
.configure(authentication());

app.io.on('connect', function(){
app.authenticate({
type: 'local',
'email': '[email protected]',
'password': 'admin'
}).then(function(result){
console.log('Authenticated!', result);
}).catch(function(error){
console.error('Error authenticating!', error);
});
});
```

## Changelog

Expand Down
1 change: 1 addition & 0 deletions packages/authentication/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./lib/client');
1 change: 0 additions & 1 deletion packages/authentication/example/README.md

This file was deleted.

90 changes: 90 additions & 0 deletions packages/authentication/example/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
var feathers = require('feathers');
var rest = require('feathers-rest');
var socketio = require('feathers-socketio');
var primus = require('feathers-primus');
var hooks = require('feathers-hooks');
var memory = require('feathers-memory');
var bodyParser = require('body-parser');
var authentication = require('../lib/index').default;
var authHooks = require('../lib/index').hooks;

// Passport Auth Strategies
var FacebookStrategy = require('passport-facebook').Strategy;
var GithubStrategy = require('passport-github').Strategy;

// Initialize the application
var app = feathers()
.configure(rest())
// .configure(primus({
// transformer: 'websockets'
// }))
.configure(socketio())
.configure(hooks())
// Needed for parsing bodies (login)
.use(bodyParser.json())
.use(bodyParser.urlencoded({ extended: true }))
// Configure feathers-authentication
.configure(authentication({
token: {
secret: 'feathers-rocks'
},
local: {},
facebook: {
strategy: FacebookStrategy,
"clientID": "your-facebook-client-id",
"clientSecret": "your-facebook-client-secret",
"permissions": {
authType: 'rerequest',
"scope": ["public_profile", "email"]
}
},
github: {
strategy: GithubStrategy,
"clientID": "your-github-client-id",
"clientSecret": "your-github-client-secret"
}
}))
// Initialize a user service
.use('/users', memory())
// A simple Message service that we can used for testing
.use('/messages', memory())
.use('/', feathers.static(__dirname + '/public'))
.use(function(error, req, res, next){
res.status(error.code);
res.json(error);
});

var messageService = app.service('/messages');
messageService.create({text: 'A million people walk into a Silicon Valley bar'}, {}, function(){});
messageService.create({text: 'Nobody buys anything'}, {}, function(){});
messageService.create({text: 'Bar declared massive success'}, {}, function(){});

messageService.before({
all: [
authHooks.verifyToken({secret: 'feathers-rocks'}),
authHooks.populateUser(),
authHooks.requireAuth()
]
})

var userService = app.service('/users');

// Add a hook to the user service that automatically replaces
// the password with a hash of the password before saving it.
userService.before({
create: authHooks.hashPassword()
});

// Create a user that we can use to log in
var User = {
email: '[email protected]',
password: 'admin'
};

userService.create(User, {}).then(function(user) {
console.log('Created default user', user);
});

app.listen(3030);

console.log('Feathers authentication app started on 127.0.0.1:3030');
47 changes: 0 additions & 47 deletions packages/authentication/example/example-server.js

This file was deleted.

Loading

0 comments on commit 7121bbe

Please sign in to comment.