Skip to content

Commit

Permalink
add next url redirect to password login workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Yibaebi committed Jul 26, 2023
1 parent 340b4e6 commit cf18009
Showing 1 changed file with 39 additions and 28 deletions.
67 changes: 39 additions & 28 deletions app/authenticators/irene.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import ENV from 'irene/config/environment';
import { inject as service } from '@ember/service';
import { getOwner } from '@ember/application';


const b64EncodeUnicode = str =>
btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) => String.fromCharCode(`0x${p1}`))
)
;

const b64EncodeUnicode = (str) =>
btoa(
encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (match, p1) =>
String.fromCharCode(`0x${p1}`)
)
);
const getB64Token = (user, token) => b64EncodeUnicode(`${user}:${token}`);

const processData = (data) => {
Expand All @@ -18,55 +18,66 @@ const processData = (data) => {
};

const IreneAuthenticator = Base.extend({

ajax: service(),
router: service(),
window: service('browser/window'),

resumeTransistion(username) {
const nextRoute = this.get('router')?.currentRoute?.queryParams?.next;

if (nextRoute && username) {
const URL = `${nextRoute}&username=${username}`;
this.get('window').location = URL;

return;
}

const authenticatedRoute = getOwner(this).lookup('route:authenticated');
const lastTransition = authenticatedRoute.get('lastTransition');

resumeTransistion() {
const authenticatedRoute = getOwner(this).lookup("route:authenticated");
const lastTransition = authenticatedRoute.get("lastTransition");
if (lastTransition) {
return lastTransition.retry();
} else {
const applicationRoute = getOwner(this).lookup("route:application");
return applicationRoute.transitionTo(ENV['ember-simple-auth']["routeAfterAuthentication"]);
const applicationRoute = getOwner(this).lookup('route:application');
return applicationRoute.transitionTo(
ENV['ember-simple-auth']['routeAfterAuthentication']
);
}
},

async authenticate(identification, password, otp) {
const ajax = this.get("ajax");
const ajax = this.get('ajax');
const data = {
username: identification,
password,
otp
}
otp,
};
const url = ENV['ember-simple-auth']['loginEndPoint'];
return ajax.post(url, { data })
.then(data => {
data = processData(data);
this.resumeTransistion();
return data;
});
return ajax.post(url, { data }).then((data) => {
data = processData(data);
this.resumeTransistion(identification);
return data;
});
},

async restore(data) {
const ajax = this.get("ajax");
const ajax = this.get('ajax');
const url = ENV['ember-simple-auth']['checkEndPoint'];
await ajax.post(url, {
data: {},
headers: {
'Authorization': `Basic ${data.b64token}`
}
})
Authorization: `Basic ${data.b64token}`,
},
});
return data;
},

async invalidate() {
const ajax = this.get("ajax");
const ajax = this.get('ajax');
const url = ENV['ember-simple-auth']['logoutEndPoint'];
await ajax.post(url);
location.reload();
}
},
});


export default IreneAuthenticator;

0 comments on commit cf18009

Please sign in to comment.