Fix broken login when backend changed in config.

If the backend was changed in the config, the user from the old backend
would be passed to the new backend, which could cause errors if the
format was different.
This commit is contained in:
Caleb 2017-08-29 13:08:15 -06:00
parent 53e5dfee7c
commit e69c8dd3fc

View File

@ -66,8 +66,9 @@ const slugFormatter = (template = "{{slug}}", entryData) => {
};
class Backend {
constructor(implementation, authStore = null) {
constructor(implementation, backendName, authStore = null) {
this.implementation = implementation;
this.backendName = backendName;
this.authStore = authStore;
if (this.implementation === null) {
throw new Error("Cannot instantiate a Backend with no implementation");
@ -77,10 +78,12 @@ class Backend {
currentUser() {
if (this.user) { return this.user; }
const stored = this.authStore && this.authStore.retrieve();
if (stored) {
if (stored && stored.backendName === this.backendName) {
return Promise.resolve(this.implementation.setUser(stored)).then((user) => {
this.authStore.store(user);
return user;
const newUser = {...user, backendName: this.backendName};
// return confirmed/rehydrated user object instead of stored
this.authStore.store(newUser);
return newUser;
});
}
return Promise.resolve(null);
@ -92,8 +95,9 @@ class Backend {
authenticate(credentials) {
return this.implementation.authenticate(credentials).then((user) => {
if (this.authStore) { this.authStore.store(user); }
return user;
const newUser = {...user, backendName: this.backendName};
if (this.authStore) { this.authStore.store(newUser); }
return newUser;
});
}
@ -299,11 +303,11 @@ export function resolveBackend(config) {
switch (name) {
case "test-repo":
return new Backend(new TestRepoBackend(config), authStore);
return new Backend(new TestRepoBackend(config), name, authStore);
case "github":
return new Backend(new GitHubBackend(config), authStore);
return new Backend(new GitHubBackend(config), name, authStore);
case "git-gateway":
return new Backend(new GitGatewayBackend(config), authStore);
return new Backend(new GitGatewayBackend(config), name, authStore);
default:
throw new Error(`Backend not found: ${ name }`);
}