My Collections
@@ -57,12 +59,22 @@
@@ -73,18 +85,47 @@
diff --git a/pinry-spa/src/components/api.js b/pinry-spa/src/components/api.js
index 1099e5a..3625a17 100644
--- a/pinry-spa/src/components/api.js
+++ b/pinry-spa/src/components/api.js
@@ -1,4 +1,5 @@
import axios from 'axios';
+import storage from './utils/storage';
const API_PREFIX = '/api/v2/';
@@ -32,7 +33,51 @@ function fetchPinsForBoard(boardId) {
);
}
+const User = {
+ storageKey: 'pinry.user',
+ logOut() {
+ const self = this;
+ return new Promise(
+ (resolve) => {
+ axios.get('/api-auth/logout/?next=/api/v2/').then(
+ () => {
+ storage.set(self.storageKey, null, 1);
+ resolve();
+ },
+ );
+ },
+ );
+ },
+ fetchUserInfo() {
+ /* returns null if user not logged in */
+ const self = this;
+ const userInfo = storage.get(self.storageKey);
+ if (userInfo !== null) {
+ return new Promise(
+ resolve => resolve(userInfo),
+ );
+ }
+ const url = `${API_PREFIX}users/`;
+ return new Promise(
+ (resolve) => {
+ axios.get(url).then(
+ (resp) => {
+ const users = resp.data;
+ if (users.length === 0) {
+ return resolve(null);
+ }
+ const value = users[0];
+ storage.set(self.storageKey, value, 60 * 5 * 1000);
+ return resolve(users[0]);
+ },
+ );
+ },
+ );
+ },
+};
+
export default {
fetchPins,
fetchPinsForBoard,
+ User,
};
diff --git a/pinry-spa/src/components/utils/storage.js b/pinry-spa/src/components/utils/storage.js
new file mode 100644
index 0000000..119929e
--- /dev/null
+++ b/pinry-spa/src/components/utils/storage.js
@@ -0,0 +1,20 @@
+/* from https://github.com/liesislukas/localstorage-ttl/blob/master/index.js */
+const storage = {
+ set(key, value, ttlMs) {
+ const data = { value, expires_at: new Date().getTime() + ttlMs / 1 };
+ localStorage.setItem(key.toString(), JSON.stringify(data));
+ },
+ get(key) {
+ const data = JSON.parse(localStorage.getItem(key.toString()));
+ if (data !== null) {
+ if (data.expires_at !== null && data.expires_at < new Date().getTime()) {
+ localStorage.removeItem(key.toString());
+ } else {
+ return data.value;
+ }
+ }
+ return null;
+ },
+};
+
+export default storage;