|
| 1 | +import { ref, watch } from "vue"; |
| 2 | + |
| 3 | +import { mapState } from "pinia"; |
| 4 | +import { useKumiteStore } from "./store.js"; |
| 5 | + |
| 6 | +import { useRouter } from "vue-router"; |
| 7 | +import Logout from "./login-logout.js"; |
| 8 | + |
| 9 | +export default { |
| 10 | + components: { |
| 11 | + Logout, |
| 12 | + }, |
| 13 | + props: { |
| 14 | + logout: { |
| 15 | + type: String, |
| 16 | + required: false, |
| 17 | + }, |
| 18 | + }, |
| 19 | + computed: { |
| 20 | + ...mapState(useKumiteStore, ["nbAccountFetching", "account", "needsToLogin"]), |
| 21 | + ...mapState(useKumiteStore, { |
| 22 | + user(store) { |
| 23 | + return store.account; |
| 24 | + }, |
| 25 | + }), |
| 26 | + }, |
| 27 | + setup(props) { |
| 28 | + const store = useKumiteStore(); |
| 29 | + const router = useRouter(); |
| 30 | + |
| 31 | + store.loadUser(); |
| 32 | + |
| 33 | + const username = ref("11111111-1111-1111-1111-000000000000"); |
| 34 | + const password = ref("no_password"); |
| 35 | + |
| 36 | + const doLoginBasic = function () { |
| 37 | + console.info("Login BASIC"); |
| 38 | + async function fetchFromUrl(url, csrfToken) { |
| 39 | + // https://stackoverflow.com/questions/60265617/how-do-you-include-a-csrf-token-in-a-vue-js-application-with-a-spring-boot-backe |
| 40 | + const headers = { |
| 41 | + [csrfToken.header]: csrfToken.token, |
| 42 | + // https://stackoverflow.com/questions/43842793/basic-authentication-with-fetch |
| 43 | + Authorization: "Basic " + btoa(username.value + ":" + password.value), |
| 44 | + }; |
| 45 | + |
| 46 | + try { |
| 47 | + const response = await fetch(url, { |
| 48 | + method: "POST", |
| 49 | + headers: headers, |
| 50 | + }); |
| 51 | + if (!response.ok) { |
| 52 | + throw new Error("Rejected request for logout"); |
| 53 | + } |
| 54 | + |
| 55 | + const json = await response.json(); |
| 56 | + |
| 57 | + console.info("Login BASIC", json); |
| 58 | + |
| 59 | + const loginSuccessHtmlRoute = json.Location; |
| 60 | + router.push(loginSuccessHtmlRoute); |
| 61 | + } catch (e) { |
| 62 | + console.error("Issue on Network: ", e); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + store.fetchCsrfToken().then((csrfToken) => { |
| 67 | + fetchFromUrl(`/api/login/v1/basic`, csrfToken); |
| 68 | + }); |
| 69 | + }; |
| 70 | + |
| 71 | + return { username, password, doLoginBasic }; |
| 72 | + }, |
| 73 | + template: /* HTML */ ` |
| 74 | + <span v-if="needsToLogin"> |
| 75 | + <div class="input-group mb-3"> |
| 76 | + <input type="text" class="form-control" placeholder="Username" aria-label="Username" v-model="username" /> |
| 77 | + <span class="input-group-text">:</span> |
| 78 | + <input type="text" class="form-control" placeholder="Password" aria-label="Password" v-model="password" /> |
| 79 | + <button type="button" @click="doLoginBasic" class="btn btn-primary">Login fakeUser</button> |
| 80 | + </div> |
| 81 | + </span> |
| 82 | + <span v-else> |
| 83 | + <Logout /> |
| 84 | + </span> |
| 85 | + `, |
| 86 | +}; |
0 commit comments