Allowing alternative authentication methods on ohif:user package

This commit is contained in:
Bruno Alves de Faria 2017-03-24 18:45:32 -03:00
parent 0a82bfd6b6
commit dc405e53da
4 changed files with 39 additions and 12 deletions

View File

@ -2,6 +2,7 @@ import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import { SimpleSchema } from 'meteor/aldeed:simple-schema';
import { Router } from 'meteor/iron:router';
import { OHIF } from 'meteor/ohif:core';
Template.userLogin.onCreated(() => {
const instance = Template.instance();
@ -17,20 +18,30 @@ Template.userLogin.onCreated(() => {
// Get the form data
const formData = form.value();
// Call the Meteor's login method
Meteor.loginWithPassword(formData.username, formData.password, error => {
if (!error) {
const currentRoute = Router.current();
const redirect = currentRoute.params.query.redirect;
const path = redirect ? decodeURI(redirect) : '/';
return Router.go(path);
}
const { reason } = error;
// Handle errors and display the error message on the respective field
const errorHandler = error => {
const reason = (error && error.reason) || 'An error has ocurred';
const isPassword = reason && reason.toLowerCase().indexOf('password') > -1;
const displayComponent = form.item(isPassword ? 'password' : 'username');
displayComponent.error(reason);
displayComponent.$element.focus();
Meteor.defer(() => displayComponent.$element.focus());
};
// Handle success and redirect the user
const successHandler = () => {
const currentRoute = Router.current();
const redirect = currentRoute.params.query.redirect;
const path = redirect ? decodeURI(redirect) : '/';
return Router.go(path);
};
// Call the login method
const promise = OHIF.user.login(formData).then(successHandler).catch(errorHandler);
// Display loading state
OHIF.ui.showDialog('dialogLoading', {
text: 'Signing in...',
promise
});
}
};

View File

@ -1,2 +1,3 @@
import './getName';
import './login';
import './logout';

View File

@ -0,0 +1,14 @@
import { Meteor } from 'meteor/meteor';
import { OHIF } from 'meteor/ohif:core';
OHIF.user.login = params => {
return new Promise((resolve, reject) => {
Meteor.loginWithPassword(params.username, params.password, error => {
if (error) {
return reject(error);
}
resolve();
});
});
};

View File

@ -10,7 +10,8 @@ Package.onUse(function(api) {
// Meteor client and server packages
api.use([
'ecmascript',
'accounts-base'
'accounts-base',
'accounts-password'
]);
// Meteor client-only packages