-
-
Notifications
You must be signed in to change notification settings - Fork 750
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from feathersjs/decoupling
Decoupling
- Loading branch information
Showing
45 changed files
with
2,815 additions
and
1,189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/client'); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.