Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017-2021 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | //import cookie from 'cookie'; |
| 21 | |
| 22 | class AuthManager { |
| 23 | constructor() { |
| 24 | console.log("AuthManager()") |
| 25 | this.authenticated = true//'connect.sid' in cookie.parse(document.cookie) |
| 26 | this.authenticating = false |
| 27 | this.tasks = [] |
| 28 | this.onAuthChanged = undefined |
| 29 | } |
| 30 | setOnAuthChanged(onAuthChanged) { |
| 31 | this.onAuthChanged = onAuthChanged |
| 32 | } |
| 33 | |
| 34 | isAuthenticated() { |
| 35 | return this.authenticated |
| 36 | } |
| 37 | |
| 38 | authenticate() { |
| 39 | if (this.authenticating) |
| 40 | return |
| 41 | console.log("Starting authentication") |
| 42 | this.authenticating = true |
| 43 | fetch('/api/localLogin?username=local&password=local', { method:"POST" }) |
| 44 | .then(response => { |
| 45 | console.log(response) |
| 46 | this.authenticating = false |
| 47 | this.authenticated = response.ok && response.status === 200 |
| 48 | if (this.onAuthChanged) |
| 49 | this.onAuthChanged(this.authenticated) |
| 50 | while (this.tasks.length !== 0) { |
| 51 | const task = this.tasks.shift() |
| 52 | if (this.authenticated) |
| 53 | fetch(task.url, task.init).then(res => task.resolve(res)) |
| 54 | else |
| 55 | task.reject(new Error("Authentication failed")) |
| 56 | } |
| 57 | }) |
| 58 | } |
| 59 | |
| 60 | disconnect() { |
| 61 | console.log("Disconnect") |
| 62 | this.authenticated = false |
| 63 | if (this.onAuthChanged) |
| 64 | this.onAuthChanged(this.authenticated) |
| 65 | } |
| 66 | |
| 67 | fetch(url, init) { |
| 68 | console.log(`get ${url}`) |
| 69 | if (!this.authenticated) { |
| 70 | return new Promise((resolve, reject) => this.tasks.push({url, init, resolve, reject})) |
| 71 | } |
| 72 | return fetch(url, init) |
| 73 | .then(response => { |
| 74 | console.log(`Got status ${response.status}`) |
| 75 | if (response.status === 401) { |
| 76 | this.disconnect() |
| 77 | return this.fetch(url, init) |
| 78 | } |
| 79 | return response |
| 80 | }) |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | export default new AuthManager() |