I'm using Ember JS with Sails JS and i'm having a weird problem that drove me crazy for the last two days
Now as the setup i'm using sails-generate-ember-blueprints in sails js aside with jwt, bcrypt and express-jwt
On the Ember side i configured my adapters as follow
in the adapter folder "application.js"
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
export default DS.RESTAdapter.extend(DataAdapterMixin, {
authorizer: 'authorizer:oauth2',
host: 'http://localhost:1338',
});
in the authenticators folder "oauth2.js"
import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';
export default OAuth2PasswordGrant.extend({
serverTokenEndpoint:'http://localhost:1338/auths/login'
});
and the authorizer folder "oauth2.js"
import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer';
export default OAuth2Bearer.extend();
so far so good. now i created an authentication controller in the sails project "AuthController.js" and pasted this code which was taken from another project
var jwt = require("jsonwebtoken");
var bcrypt = require("bcrypt");
module.exports = {
login: function(req, res) {
if (!req.body.username || !req.body.password) {
res.badRequest({error:'missing info!'});
return;
}
User.findByEmail(req.body.username).exec(function(err, user) {
if (err) {
return res.badRequest({
error: err
});
}
if (!user || user.length < 1) {
return res.badRequest({
error: 'No such user'
});
}
bcrypt.compare(req.body.password, user[0].password,
function(err, result) {
if (err || !result) {
return res.badRequest({
error: 'invalid Password'
});
} else {
issueTokens(user, res);
}
});
});
},
logout: function(req, res) {
req.logout();
res.send({
success: true,
message: 'logout Successful'
});
}
};
function issueTokens(user, res) {
var token = jwt.sign(user[0], sails.config.jwt.secret, {
expiresIn: sails.config.jwt.expiration_time_in_minutes
});
res.send({
user: user[0],
access_token: token
});
};
i have a model in the Ember Application "user.js" which have an email and password attributes both set as DS.attr('string')
i registered my first user using postman and when i tried to login it gives me an error from the bcrypt.compare function i thought it might be because bcrypt is trying to compare a password that isn't encrypted in the first place so i tried to register a user simply by
signUp(email, password){
this.store.createRecord('user', {
email : email,
password : password
}).save();
}
it did register new user and bcrypted the password, but when i try to login using the same user i created it i get an error
POST : localhost:1338/auths/login/ net::ERR_EMPTY_RESPONSE
and in the terminal from the sails side i get this error
/Users/Haboosh/Desktop/pulse/pulse-server/node_modules/jsonwebtoken/sign.js:97 throw err; ^
Error: Expected "payload" to be a plain object. at validate (/Users/Haboosh/Desktop/pulse/pulse-server/node_modules/jsonwebtoken/sign.js:34:11) at validatePayload (/Users/Haboosh/Desktop/pulse/pulse-server/node_modules/jsonwebtoken/sign.js:56:10) at Object.module.exports [as sign] (/Users/Haboosh/Desktop/pulse/pulse-server/node_modules/jsonwebtoken/sign.js:108:7) at issueTokens (/Users/Haboosh/Desktop/pulse/pulse-server/api/controllers/AuthController.js:53:18) at /Users/Haboosh/Desktop/pulse/pulse-server/api/controllers/AuthController.js:36:9
what am i doing wrong people ?
my login function is like this
login(email, password){
this.get('session').authenticate('authenticator:oauth2', email, password).then(() => {
this.transitionToRoute('index')
})
};
Aucun commentaire:
Enregistrer un commentaire