2017-05-24 17:56:13 +02:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
2019-12-20 01:45:24 +01:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2017-05-24 17:56:13 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2020-05-14 04:41:41 +02:00
|
|
|
import dis from '../dispatcher/dispatcher';
|
2017-05-15 15:52:19 +02:00
|
|
|
import {Store} from 'flux/utils';
|
2017-05-12 13:02:45 +02:00
|
|
|
|
2017-05-26 18:23:02 +02:00
|
|
|
const INITIAL_STATE = {
|
|
|
|
cachedPassword: localStorage.getItem('mx_pass'),
|
|
|
|
};
|
|
|
|
|
2017-05-12 13:02:45 +02:00
|
|
|
/**
|
|
|
|
* A class for storing application state to do with the session. This is a simple flux
|
|
|
|
* store that listens for actions and updates its state accordingly, informing any
|
2017-05-15 15:52:19 +02:00
|
|
|
* listeners (views) of state changes.
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* ```
|
|
|
|
* sessionStore.addListener(() => {
|
|
|
|
* this.setState({ cachedPassword: sessionStore.getCachedPassword() })
|
|
|
|
* })
|
|
|
|
* ```
|
2017-05-12 13:02:45 +02:00
|
|
|
*/
|
2017-05-15 15:52:19 +02:00
|
|
|
class SessionStore extends Store {
|
2017-05-12 16:58:44 +02:00
|
|
|
constructor() {
|
2017-05-15 15:52:19 +02:00
|
|
|
super(dis);
|
2017-05-12 13:02:45 +02:00
|
|
|
|
2017-05-12 16:58:44 +02:00
|
|
|
// Initialise state
|
2017-05-26 18:23:02 +02:00
|
|
|
this._state = INITIAL_STATE;
|
2017-05-12 16:58:44 +02:00
|
|
|
}
|
2017-05-12 13:02:45 +02:00
|
|
|
|
2017-05-12 16:58:44 +02:00
|
|
|
_update() {
|
|
|
|
// Persist state to localStorage
|
|
|
|
if (this._state.cachedPassword) {
|
|
|
|
localStorage.setItem('mx_pass', this._state.cachedPassword);
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem('mx_pass', this._state.cachedPassword);
|
|
|
|
}
|
2017-05-12 13:02:45 +02:00
|
|
|
|
2017-05-15 15:52:19 +02:00
|
|
|
this.__emitChange();
|
2017-05-12 13:02:45 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 16:58:44 +02:00
|
|
|
_setState(newState) {
|
|
|
|
this._state = Object.assign(this._state, newState);
|
|
|
|
this._update();
|
2017-05-12 13:02:45 +02:00
|
|
|
}
|
|
|
|
|
2017-05-15 15:52:19 +02:00
|
|
|
__onDispatch(payload) {
|
2017-05-12 16:58:44 +02:00
|
|
|
switch (payload.action) {
|
|
|
|
case 'cached_password':
|
|
|
|
this._setState({
|
|
|
|
cachedPassword: payload.cachedPassword,
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case 'password_changed':
|
|
|
|
this._setState({
|
|
|
|
cachedPassword: null,
|
|
|
|
});
|
|
|
|
break;
|
2019-07-04 00:46:37 +02:00
|
|
|
case 'on_client_not_viable':
|
2017-05-25 18:16:16 +02:00
|
|
|
case 'on_logged_out':
|
2017-05-31 11:03:16 +02:00
|
|
|
this._setState({
|
|
|
|
cachedPassword: null,
|
|
|
|
});
|
2017-05-25 18:16:16 +02:00
|
|
|
break;
|
2017-05-12 16:58:44 +02:00
|
|
|
}
|
2017-05-26 18:27:31 +02:00
|
|
|
}
|
2017-05-26 18:23:02 +02:00
|
|
|
|
2017-05-12 16:58:44 +02:00
|
|
|
getCachedPassword() {
|
|
|
|
return this._state.cachedPassword;
|
|
|
|
}
|
|
|
|
}
|
2017-05-12 13:02:45 +02:00
|
|
|
|
|
|
|
let singletonSessionStore = null;
|
2017-05-12 16:50:01 +02:00
|
|
|
if (!singletonSessionStore) {
|
|
|
|
singletonSessionStore = new SessionStore();
|
2017-05-12 13:02:45 +02:00
|
|
|
}
|
2019-12-20 01:45:24 +01:00
|
|
|
export default singletonSessionStore;
|