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/store/modules/auth.ts | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) (limited to 'app/src/store/modules/auth.ts') 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"); }, -- cgit v1.2.3-18-g5258