1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-08-08 06:55:28 +02:00

Fix sso authentication error by refactoring folders route

This commit is contained in:
zinyando 2016-07-07 14:11:03 +02:00
parent a7894d6800
commit f41517872b
4 changed files with 95 additions and 98 deletions

View file

@ -7,10 +7,9 @@ export default Ember.Route.extend({
this.get("session").authenticate('authenticator:documize', token) this.get("session").authenticate('authenticator:documize', token)
.then(() => { .then(() => {
this.transitionTo('folders.folder'); this.transitionTo('folders.folder');
}) }, () => {
.catch(() => {
this.transitionTo('auth.login'); this.transitionTo('auth.login');
console.log(">>>>> Documize SSO failure"); console.log(">>>>> Documize SSO failure");
}); });
} },
}); });

View file

@ -6,50 +6,55 @@ export default Ember.Route.extend(AuthenticatedRouteMixin, {
localStorage: Ember.inject.service(), localStorage: Ember.inject.service(),
folder: {}, folder: {},
model: function() { model: function () {
return this.get('folderService').getAll(); return this.get('folderService').getAll();
}, },
afterModel: function(model) { afterModel: function (model) {
let self = this; // TODO: replace with ES6 features (remove self this)
// TODO: replace is.* with Ember utilities
// TODO: flatten if/else
// TODO: make sure chain is maintained by returning promies
if (is.empty(this.paramsFor('folders.folder'))) { if (is.empty(this.paramsFor('folders.folder'))) {
var lastFolder = this.get('localStorage').getSessionItem("folder"); let lastFolder = this.get('localStorage').getSessionItem("folder");
if (is.not.undefined(lastFolder)) { //If folder lastFolder is defined
this.get('folderService').getFolder(lastFolder).then(function(folder) { if (Ember.isPresent(lastFolder)) {
if (is.undefined(folder) || is.null(folder)) { return this.get('folderService').getFolder(lastFolder).then((folder) => {
self.transitionTo('auth.login'); //if Response is null or undefined redirect to login else transitionTo dashboard
} if (Ember.isNone(folder)) {
self.folder = folder; this.transitionTo('auth.login');
self.transitionTo('folders.folder', folder.get('id'), folder.get('slug'));
}, function() {
if (model.length > 0) {
var folder = model[0];
self.folder = folder;
self.transitionTo('folders.folder', folder.get('id'), folder.get('slug'));
} else {
self.transitionTo('auth.login');
} }
this.folder = folder;
this.transitionTo('folders.folder', folder.get('id'), folder.get('slug'));
}).catch(() => {
//if there was an error redirect to login
this.transitionTo('auth.login');
}); });
} else { }
// If model has any folders redirect to dashboard
if (model.length > 0) { if (model.length > 0) {
var folder = model[0]; let folder = model[0];
self.folder = folder; this.folder = folder;
self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); this.transitionTo('folders.folder', folder.get('id'), folder.get('slug'));
} else }
{
// has no folders, create default folder // has no folders, create default folder
this.get('folderService').add({ name: "My Space" }).then(function(folder) { return this.get('folderService').add({ name: "My Space" }).then((folder) => {
self.folder = folder; this.folder = folder;
self.transitionTo('folders.folder', folder.get('id'), folder.get('slug')); this.transitionTo('folders.folder', folder.get('id'), folder.get('slug'));
}); });
} }
}
} else { //If folder route has params
var folderId = this.paramsFor('folders.folder').folder_id; if (Ember.isPresent(this.paramsFor('folders.folder'))) {
this.get('folderService').getFolder(folderId).then(function(folder) {
self.folder = folder; let folderId = this.paramsFor('folders.folder').folder_id;
return this.get('folderService').getFolder(folderId).then((folder) => {
this.folder = folder;
}); });
} }

View file

@ -7,6 +7,8 @@ export default function () {
this.namespace = 'api'; // make this `api`, for example, if your API is namespaced this.namespace = 'api'; // make this `api`, for example, if your API is namespaced
// this.timing = 400; // delay for each request, automatically set to 0 during testing // this.timing = 400; // delay for each request, automatically set to 0 during testing
this.logging = true;
this.get('/public/meta', function (schema) { this.get('/public/meta', function (schema) {
return schema.db.meta[0]; return schema.db.meta[0];
}); });
@ -128,37 +130,7 @@ export default function () {
let expectedAuthorization = "Basic OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw=="; let expectedAuthorization = "Basic OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw==";
if (expectedAuthorization == authorization) { if (expectedAuthorization == authorization) {
return { console.log("SSO login success");
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0",
"user": {
"id": "VzMuyEw_3WqiafcE",
"created": "2016-05-11T15:08:24Z",
"revised": "2016-05-11T15:08:24Z",
"firstname": "Lennex",
"lastname": "Zinyando",
"email": "brizdigital@gmail.com",
"initials": "LZ",
"active": true,
"editor": true,
"admin": true,
"accounts": [{
"id": "VzMuyEw_3WqiafcF",
"created": "2016-05-11T15:08:24Z",
"revised": "2016-05-11T15:08:24Z",
"admin": true,
"editor": true,
"userId": "VzMuyEw_3WqiafcE",
"orgId": "VzMuyEw_3WqiafcD",
"company": "EmberSherpa",
"title": "EmberSherpa",
"message": "This Documize instance contains all our team documentation",
"domain": ""
}]
}
};
} else if (expectedAuthorization != authorization) {
return new Mirage.Response(400);
} else {
return { return {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0", "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0",
"user": { "user": {
@ -189,6 +161,39 @@ export default function () {
}; };
} }
if (expectedAuthorization != authorization) {
return new Mirage.Response(401, { 'Content-Type': 'application/json' }, { message: 'Bad Request' });
}
return {
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkb21haW4iOiIiLCJleHAiOjE0NjQwMjM2NjcsImlzcyI6IkRvY3VtaXplIiwib3JnIjoiVnpNdXlFd18zV3FpYWZjRCIsInN1YiI6IndlYmFwcCIsInVzZXIiOiJWek11eUV3XzNXcWlhZmNFIn0.NXZ6bo8mtvdZF_b9HavbidVUJqhmBA1zr0fSAPvbah0",
"user": {
"id": "VzMuyEw_3WqiafcE",
"created": "2016-05-11T15:08:24Z",
"revised": "2016-05-11T15:08:24Z",
"firstname": "Lennex",
"lastname": "Zinyando",
"email": "brizdigital@gmail.com",
"initials": "LZ",
"active": true,
"editor": true,
"admin": true,
"accounts": [{
"id": "VzMuyEw_3WqiafcF",
"created": "2016-05-11T15:08:24Z",
"revised": "2016-05-11T15:08:24Z",
"admin": true,
"editor": true,
"userId": "VzMuyEw_3WqiafcE",
"orgId": "VzMuyEw_3WqiafcD",
"company": "EmberSherpa",
"title": "EmberSherpa",
"message": "This Documize instance contains all our team documentation",
"domain": ""
}]
}
};
}); });
this.get('/users/VzMuyEw_3WqiafcE/permissions', (schema) => { this.get('/users/VzMuyEw_3WqiafcE/permissions', (schema) => {
@ -440,15 +445,4 @@ export default function () {
return {}; return {};
}); });
/**
very helpful for debugging
*/
this.handledRequest = function (verb, path) {
console.log(`👊${verb} ${path}`);
};
this.unhandledRequest = function (verb, path) {
console.log(`🔥${verb} ${path}`);
};
} }

View file

@ -34,7 +34,6 @@ test('successful sso login authenticates redirects to dashboard', function (asse
server.createList('folder', 2); server.createList('folder', 2);
visit('/auth/sso/OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw=='); visit('/auth/sso/OmJyaXpkaWdpdGFsQGdtYWlsLmNvbTp6aW55YW5kbzEyMw==');
// return pauseTest();
andThen(function () { andThen(function () {
assert.equal(currentURL(), '/s/VzMuyEw_3WqiafcG/my-project', 'SSO login successful'); assert.equal(currentURL(), '/s/VzMuyEw_3WqiafcG/my-project', 'SSO login successful');