register: move api to data/auth.js and handle errors
This commit is contained in:
parent
6d0899681c
commit
e47c3b977c
|
@ -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 = {}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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" });
|
||||
|
|
Loading…
Reference in New Issue