From e47c3b977c06205f50da03e20bbccc409373ad31 Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Tue, 21 May 2019 11:06:38 +0200 Subject: [PATCH] register: move api to data/auth.js and handle errors --- src/util/auth.js | 13 +++++++++++++ src/util/data.js | 26 +++++++++++++++++++++++++- src/views/Register.vue | 35 +++++++++++++++++++---------------- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/util/auth.js b/src/util/auth.js index a672893..f017ee3 100644 --- a/src/util/auth.js +++ b/src/util/auth.js @@ -61,6 +61,19 @@ export async function loginapi(init) { } } +export async function registerapi(init) { + if (init === undefined) { + init = {} + } + + try { + let res = await window.fetch(registerurl(), init) + return res + } catch (e) { + throw e + } +} + export async function fetch(url, init) { if (init === undefined) { init = {} diff --git a/src/util/data.js b/src/util/data.js index 20e7512..65d23ce 100644 --- a/src/util/data.js +++ b/src/util/data.js @@ -1,4 +1,4 @@ -import { apiurl, loginapi } from "@/util/auth"; +import { apiurl, loginapi, registerapi } from "@/util/auth"; import { fetch as authfetch } from "@/util/auth"; export async function fetch(url, init) { @@ -41,6 +41,30 @@ export async function login(username, password, remotesourcename) { } } +export async function register(username, remotesourcename, remoteloginname, remotepassword) { + let init = { + method: "POST", + body: JSON.stringify({ + username: username, + remote_source_name: remotesourcename, + remote_source_login_name: remoteloginname, + remote_source_login_password: remotepassword + }) + } + + try { + let res = await registerapi(init) + if (!res.ok) { + let data = await res.json() + return { data: null, error: data.message } + } else { + return { data: await res.json(), error: null } + } + } catch (e) { + return { data: null, error: "api call failed: " + e } + } +} + export async function fetchCurrentUser() { let path = "/user" return await fetch(apiurl(path)); diff --git a/src/views/Register.vue b/src/views/Register.vue index 9d0f091..d1354e7 100644 --- a/src/views/Register.vue +++ b/src/views/Register.vue @@ -31,7 +31,7 @@ import { mapGetters } from "vuex"; import LoginForm from "@/components/loginform"; import RegisterForm from "@/components/registerform"; -import { fetchRemoteSources } from "@/util/data"; +import { fetchRemoteSources, register } from "@/util/data"; import { authorizeurl, registerurl, fetch, doLogout } from "@/util/auth"; @@ -43,6 +43,7 @@ export default { }, data: function() { return { + error: null, remotesources: null }; }, @@ -58,12 +59,12 @@ export default { } this.remotesources = data; }, - async doAuthorize(rsName, username, password) { + async doAuthorize(remotesourcename, username, password) { let u = authorizeurl(); let res = await (await fetch(u, { method: "POST", body: JSON.stringify({ - remote_source_name: rsName, + remote_source_name: remotesourcename, login_name: username, password: password }) @@ -81,24 +82,26 @@ export default { }); }, async doRegister( - rsName, + remotesourcename, username, remote_login_name, remote_login_password ) { - let u = registerurl(); - let res = await (await fetch(u, { - method: "POST", - body: JSON.stringify({ - username: username, - remote_source_name: rsName, - remote_source_login_name: remote_login_name, - remote_source_login_password: remote_login_password - }) - })).json(); + this.error = null; - if (res.oauth2_redirect) { - window.location = res.oauth2_redirect; + let { data, error } = await register( + username, + remotesourcename, + remote_login_name, + remote_login_password + ); + if (error) { + // set local login error on failed login. + this.error = error; + return; + } + if (data.oauth2_redirect) { + window.location = data.oauth2_redirect; return; } this.$router.push({ name: "home" });