register: move api to data/auth.js and handle errors

This commit is contained in:
Simone Gotti 2019-05-21 11:06:38 +02:00
parent 6d0899681c
commit e47c3b977c
3 changed files with 57 additions and 17 deletions

View File

@ -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) { export async function fetch(url, init) {
if (init === undefined) { if (init === undefined) {
init = {} init = {}

View File

@ -1,4 +1,4 @@
import { apiurl, loginapi } from "@/util/auth"; import { apiurl, loginapi, registerapi } from "@/util/auth";
import { fetch as authfetch } from "@/util/auth"; import { fetch as authfetch } from "@/util/auth";
export async function fetch(url, init) { 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() { export async function fetchCurrentUser() {
let path = "/user" let path = "/user"
return await fetch(apiurl(path)); return await fetch(apiurl(path));

View File

@ -31,7 +31,7 @@ import { mapGetters } from "vuex";
import LoginForm from "@/components/loginform"; import LoginForm from "@/components/loginform";
import RegisterForm from "@/components/registerform"; import RegisterForm from "@/components/registerform";
import { fetchRemoteSources } from "@/util/data"; import { fetchRemoteSources, register } from "@/util/data";
import { authorizeurl, registerurl, fetch, doLogout } from "@/util/auth"; import { authorizeurl, registerurl, fetch, doLogout } from "@/util/auth";
@ -43,6 +43,7 @@ export default {
}, },
data: function() { data: function() {
return { return {
error: null,
remotesources: null remotesources: null
}; };
}, },
@ -58,12 +59,12 @@ export default {
} }
this.remotesources = data; this.remotesources = data;
}, },
async doAuthorize(rsName, username, password) { async doAuthorize(remotesourcename, username, password) {
let u = authorizeurl(); let u = authorizeurl();
let res = await (await fetch(u, { let res = await (await fetch(u, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
remote_source_name: rsName, remote_source_name: remotesourcename,
login_name: username, login_name: username,
password: password password: password
}) })
@ -81,24 +82,26 @@ export default {
}); });
}, },
async doRegister( async doRegister(
rsName, remotesourcename,
username, username,
remote_login_name, remote_login_name,
remote_login_password remote_login_password
) { ) {
let u = registerurl(); this.error = null;
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();
if (res.oauth2_redirect) { let { data, error } = await register(
window.location = res.oauth2_redirect; 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; return;
} }
this.$router.push({ name: "home" }); this.$router.push({ name: "home" });