Integración usuarios, errores solucionados

This commit is contained in:
Juan Ferrer Toribio 2017-05-21 18:36:07 +02:00
parent a06d1a579c
commit 5cfdd51cf4
19 changed files with 135 additions and 52 deletions

View File

@ -3,7 +3,7 @@
<div class="box">
<img src="./logo.svg"/>
<form name="form" ng-submit="$ctrl.submit()">
<vn-textfield vn-id="userField" label="User" model="$ctrl.email"></vn-textfield>
<vn-textfield vn-id="userField" label="User" model="$ctrl.user"></vn-textfield>
<vn-textfield label="Password" model="$ctrl.password" type="password"></vn-textfield>
<div class="footer">
<vn-submit label="Enter"></vn-submit>

View File

@ -9,15 +9,15 @@ export default class Controller {
this.$http = $http;
}
submit() {
if (!(this.email && this.password)) {
if (!(this.user && this.password)) {
this.focusUser();
this.showMessage('Please insert your email and password');
this.showMessage('Please insert your user and password');
return;
}
this.loading = true;
let params = {
email: this.email,
user: this.user,
password: this.password,
location: this.$window.location.href
};
@ -70,7 +70,7 @@ export default class Controller {
this.$.userField.focus();
}
showMessage(message) {
this.snackbar.show({message: message});
this.$.snackbar.show({message: message});
}
}
Controller.$inject = ['$element', '$scope', '$window', '$http'];

View File

@ -43,7 +43,6 @@ export default class Autocomplete extends Component {
this.data.push(value);
}
}
mdlUpdate() {
let mdlField = this.element.firstChild.MaterialTextfield;
if (mdlField)
@ -344,7 +343,6 @@ export default class Autocomplete extends Component {
this.item = item;
this.mdlUpdate();
}
}
Autocomplete.$inject = ['$element', '$scope', '$http', 'vnPopover'];

View File

@ -5,6 +5,7 @@ vn-textfield {
right: -6px;
margin: 22px 0px;
visibility: hidden;
background-color: white;
}
.material-icons {
font-size: 18px;

View File

@ -5,7 +5,3 @@ import './config';
import './run';
import './components';
import './styles/index';
import {bootstrap} from './bootstrap';
bootstrap();

View File

@ -4,4 +4,4 @@ export {NAME as ACTIONS, COMPONENT as ACTIONS_COMPONENT} from './components/left
export {NAME as LEFT_MENU, COMPONENT as LEFTMENU_COMPONENT} from './components/left-menu/left-menu';
export {NAME as MENU_ITEM, COMPONENT as MENU_ITEM_COMPONENT} from './components/left-menu/menu-item';
export {NAME as TOPBAR, COMPONENT as TOPBAR_COMPONENT} from './components/topbar/topbar';
export {NAME as SEARCHBAR, COMPONENT as SEARCHBAR_COMPONENT} from './components/searchbar/searchbar';
export {NAME as SEARCHBAR, COMPONENT as SEARCHBAR_COMPONENT} from './components/searchbar/searchbar';

View File

@ -29,8 +29,8 @@ function vnAppInterceptor($q, $rootScope, logger, $translate, $cookies) {
return {
request: function(config) {
$rootScope.loading = true;
let token = $cookies.get('vnToken');
if (token)
config.headers.Authorization = token;
@ -50,8 +50,17 @@ function vnAppInterceptor($q, $rootScope, logger, $translate, $cookies) {
},
responseError: function(rejection) {
$rootScope.loading = false;
let message = rejection.data.error.message;
logger.showError(message);
let data = rejection.data;
let error;
if (data && data.error instanceof Object)
error = data.error.message;
else if (rejection.status === -1)
error = $translate.instant(`Can't contact with server`);
else
error = `${rejection.status}: ${rejection.statusText}`;
logger.showError(error);
return $q.reject(rejection);
}
};

View File

@ -5,5 +5,6 @@
"Change language": "Change language",
"Profile": "Profile",
"Data saved!": "Data saved!",
"Can't contact with server": "Can't contact with server",
"Push on applications menu": "To open a module push on applications menu"
}

View File

@ -5,5 +5,6 @@
"Change language": "Cambiar idioma",
"Profile": "Perfil",
"Data saved!": "¡Datos guardados!",
"Can't contact with server": "No se pudo contactar con el servidor",
"Push on applications menu": "Para abrir un módulo pulsa en el menú de aplicaciones"
}

View File

@ -0,0 +1,36 @@
{
"name": "Account",
"base": "PersistedModel",
"validateUpsert": true,
"properties": {
"id": {
"type": "number",
"required": true
},
"name": {
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"active": {
"type": "boolean"
}
},
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
}
]
}

View File

@ -16,6 +16,7 @@
"loopback-component-explorer": "^2.4.0",
"loopback-connector-mysql": "^3.0.0",
"loopback-datasource-juggler": "^2.39.0",
"md5": "^2.2.1",
"serve-favicon": "^2.0.1",
"strong-error-handler": "^1.0.1"
},

View File

@ -1,6 +1,8 @@
var url = require ('url');
var url = require('url');
var md5 = require('md5');
module.exports = function(app) {
let User = app.models.User;
let applications = app.get('applications');
app.get('/',function(req, res){
@ -8,36 +10,66 @@ module.exports = function(app) {
});
app.post('/login', function(req, res) {
app.models.User.login({
email: req.body.email,
password: req.body.password
}, 'user', function(err, token) {
if (err) {
res.status (401);
res.send(JSON.stringify({
title: 'Login failed',
content: err,
redirectTo: '/',
redirectToLinkText: 'Try again'
}));
} else {
let query = url.parse(req.body.location, true).query;
let loginUrl = applications[query.apiKey];
if (!loginUrl)
loginUrl = applications.default;
let user = req.body.user;
let password = req.body.password;
let syncOnFail = true;
res.send(JSON.stringify({
token: token.id,
continue: query.continue,
loginUrl: loginUrl,
}));
login();
function login() {
let loginInfo = {
username: user,
password: password
};
User.login(loginInfo, 'user', loginCb);
}
function loginCb(err, token) {
if (err) {
if(syncOnFail) {
syncOnFail = false;
let filter = {where: {name: user}};
app.models.Account.findOne(filter, findCb);
}
else
badLogin();
return;
}
});
let query = url.parse(req.body.location, true).query;
let loginUrl = applications[query.apiKey];
if (!loginUrl)
loginUrl = applications.default;
res.send(JSON.stringify({
token: token.id,
continue: query.continue,
loginUrl: loginUrl,
}));
}
function findCb(err, instance) {
if(!instance || instance.password !== md5(password)) {
badLogin();
return;
}
let where = {username: user};
let userData = {
username: user,
password: password,
email: `${user}@verdnatura.es`
};
User.upsertWithWhere(where, userData, login);
}
function badLogin() {
res.status(401);
res.send(JSON.stringify({
message: 'Login failed'
}));
}
});
app.get('/logout', function (req, res) {
app.models.User.logout(req.accessToken.id, function(err) {
res.redirect('/');
});
User.logout(req.accessToken.id,
() => res.redirect('/'));
});
};

View File

@ -1,7 +1,8 @@
{
"db": {
"name": "db",
"connector": "memory"
"connector": "memory",
"file": "db.json"
},
"auth": {
"name": "mysql",

View File

@ -27,5 +27,8 @@
},
"Role": {
"dataSource": "auth"
},
"Account": {
"dataSource": "auth"
}
}

View File

@ -11,6 +11,10 @@
"type": "string",
"required": true
},
"password": {
"type": "string",
"required": true
},
"active": {
"type": "boolean"
}

View File

@ -34,4 +34,4 @@
"permission": "DENY"
}
]
}
}

View File

@ -33,6 +33,9 @@
"dataSource": "auth",
"public": false
},
"Account": {
"dataSource": "auth"
},
"Client": {
"dataSource": "vn"
},
@ -54,9 +57,6 @@
"Country": {
"dataSource": "vn"
},
"Account": {
"dataSource": "vn"
},
"ContactChannel": {
"dataSource": "vn"
},

View File

@ -4,8 +4,8 @@
<meta charset="UTF-8">
<title>Salix</title>
</head>
<body>
<vn-app id="app"></vn-app>
<body ng-app="salix">
<vn-app></vn-app>
<script type="text/javascript"
src="/static/routes.js">
</script>
@ -16,8 +16,7 @@
src="/static/bundle.vendor.js">
</script>
<script type="text/javascript"
src="/static/bundle.salix.js"
selector="#app">
src="/static/bundle.salix.js">
</script>
</body>
</html>

View File

@ -59,7 +59,8 @@ function encodeUri(object) {
if (object[key]) {
if (uri.length > 0)
uri += '&';
uri += encodeURIComponent(key) + '=' + encodeURIComponent(object[key]);
uri += encodeURIComponent(key) + '=';
uri += encodeURIComponent(object[key]);
}
return uri;
}