From 8731f47b99af3e6890eaab68400464bebd03db03 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 14 Nov 2022 08:50:38 +0100 Subject: User info page --- app/src/router/index.ts | 6 ++++++ app/src/store/modules/auth.ts | 42 ++++++++++++++++++++++++++++++++++++++-- app/src/views/UserInfo.vue | 45 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 app/src/views/UserInfo.vue (limited to 'app') diff --git a/app/src/router/index.ts b/app/src/router/index.ts index a3dd783..3aebf85 100644 --- a/app/src/router/index.ts +++ b/app/src/router/index.ts @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from "@ionic/vue-router"; import { RouteRecordRaw } from "vue-router"; import HomePage from "../views/HomePage.vue"; import Sign from "../views/Sign.vue"; +import UserInfo from "../views/UserInfo.vue"; const routes: Array = [ { path: "/", redirect: "/home" }, @@ -15,6 +16,11 @@ const routes: Array = [ component: Sign, name: "Sign", }, + { + path: "/me", + component: UserInfo, + name: "UserInfo", + }, ]; const router = createRouter({ diff --git a/app/src/store/modules/auth.ts b/app/src/store/modules/auth.ts index b511bff..b3e72de 100644 --- a/app/src/store/modules/auth.ts +++ b/app/src/store/modules/auth.ts @@ -48,8 +48,6 @@ const auth = { const res = { status: -1, data: null }; - console.log(credentials); - await fetch(`${api}/auth/login`, { method: "POST", headers: { "Content-Type": "application/json" }, @@ -71,6 +69,46 @@ const auth = { return res; }, + // Get my information, based on the passed Authorization token + async getMe(context: AuthContext) { + const api = context.rootState.api; + + await fetch(`${api}/users/me`, { + method: "GET", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + context.getters.accessToken, + }, + }) + .then(async (response) => { + const data = await response.json(); + if (response.status != 200) { + context.dispatch( + "toast", + { + header: data.error, + text: "", + color: "danger", + }, + { root: true } + ); + context.commit("deleteAccessToken"); + } else { + context.commit("saveUserInfo", data); + } + }) + .catch((e) => { + context.dispatch( + "toast", + { + header: e, + text: "", + color: "danger", + }, + { root: true } + ); + }); + }, logout(context: AuthContext) { context.commit("deleteAccessToken"); }, diff --git a/app/src/views/UserInfo.vue b/app/src/views/UserInfo.vue new file mode 100644 index 0000000..f317cbd --- /dev/null +++ b/app/src/views/UserInfo.vue @@ -0,0 +1,45 @@ + + + -- cgit v1.2.3-18-g5258