Redirect to previous url after login

This commit is contained in:
Simone Gotti 2019-09-26 10:03:44 +02:00
parent 1a582b5cff
commit 4af98fb75b
6 changed files with 332 additions and 275 deletions

View File

@ -149,14 +149,6 @@ export default {
};
},
watch: {
user: function(user) {
if (user) {
this.$router.push({
name: "user",
params: { username: this.user.username }
});
}
},
$route: function() {
this.userDropdownActive = false;
this.createDropdownActive = false;

View File

@ -1,14 +1,12 @@
import '@/css/tailwind.scss'
import "@/css/tailwind.scss";
import { getUser } from "@/util/auth";
import "@mdi/font/css/materialdesignicons.css";
import Vue from "vue";
import Vue2Filters from "vue2-filters";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { getUser } from "@/util/auth";
Vue.use(Vue2Filters);
// TODO(sgotti) use vuex for login/logout
@ -16,11 +14,11 @@ new Vue({
router,
store,
created: function() {
let user = getUser()
let user = getUser();
if (user) {
store.dispatch('setUser', user)
store.dispatch("setUser", user);
}
store.dispatch("setRegisterUser", null)
store.dispatch("setRegisterUser", null);
},
render: h => h(App)
}).$mount("#app");

View File

@ -2,6 +2,7 @@ import store from "@/store";
const ID_TOKEN_KEY = 'id_token';
const USER_KEY = 'user';
const LOGIN_REDIRECT_KEY = 'login_redirect';
let API_URL = window.CONFIG.API_URL;
let API_BASE_PATH = window.CONFIG.API_BASE_PATH;
@ -125,3 +126,15 @@ export function isLoggedIn() {
const idToken = getIdToken();
return !!idToken;
}
export function getLoginRedirect() {
return sessionStorage.getItem(LOGIN_REDIRECT_KEY);
}
export function setLoginRedirect(url) {
sessionStorage.setItem(LOGIN_REDIRECT_KEY, url);
}
export function unsetLoginRedirect() {
return sessionStorage.removeItem(LOGIN_REDIRECT_KEY);
}

View File

@ -3,10 +3,13 @@ import { apiurl, fetch as authfetch, loginapi, registerapi } from "@/util/auth";
export async function fetch(url, init, signal) {
try {
let res = await authfetch(url, init, signal)
let res = await authfetch(url, init, signal);
if (!res.ok) {
if (res.status === 401) {
router.push({ name: "login" })
router.push({
name: "login",
query: { redirect: router.currentRoute.fullPath }
});
// if we return a response containing an error what happens is
// that router.push mounts the login view before the calling
// component processed the response and the calling component
@ -19,21 +22,21 @@ export async function fetch(url, init, signal) {
// TODO(sgotti) find a way to make this cleaner. The solution
// could be to find a way to firstly let the component handle
// the error and the do the router push...
return
return;
}
let data = await res.json()
return { data: null, error: data.message }
let data = await res.json();
return { data: null, error: data.message };
} else {
if (res.status == 204) {
return { data: null, error: null }
return { data: null, error: null };
}
return { data: await res.json(), error: null }
return { data: await res.json(), error: null };
}
} catch (e) {
if (e.name == 'AbortError') {
return { data: null, error: null, aborted: true, }
if (e.name == "AbortError") {
return { data: null, error: null, aborted: true };
}
return { data: null, error: "api call failed: " + e.message }
return { data: null, error: "api call failed: " + e.message };
}
}
@ -45,22 +48,27 @@ export async function login(username, password, remotesourcename) {
login_name: username,
password: password
})
}
};
try {
let res = await loginapi(init)
let res = await loginapi(init);
if (!res.ok) {
let data = await res.json()
return { data: null, error: data.message }
let data = await res.json();
return { data: null, error: data.message };
} else {
return { data: await res.json(), error: null }
return { data: await res.json(), error: null };
}
} catch (e) {
return { data: null, error: "api call failed: " + e.message }
return { data: null, error: "api call failed: " + e.message };
}
}
export async function register(username, remotesourcename, remoteloginname, remotepassword) {
export async function register(
username,
remotesourcename,
remoteloginname,
remotepassword
) {
let init = {
method: "POST",
body: JSON.stringify({
@ -69,44 +77,44 @@ export async function register(username, remotesourcename, remoteloginname, remo
remote_source_login_name: remoteloginname,
remote_source_login_password: remotepassword
})
}
};
try {
let res = await registerapi(init)
let res = await registerapi(init);
if (!res.ok) {
let data = await res.json()
return { data: null, error: data.message }
let data = await res.json();
return { data: null, error: data.message };
} else {
return { data: await res.json(), error: null }
return { data: await res.json(), error: null };
}
} catch (e) {
return { data: null, error: "api call failed: " + e.message }
return { data: null, error: "api call failed: " + e.message };
}
}
export async function fetchCurrentUser(signal) {
let path = "/user"
let path = "/user";
return await fetch(apiurl(path), null, signal);
}
export async function fetchOrgMembers(orgref, signal) {
let path = "/orgs/" + orgref + "/members"
let path = "/orgs/" + orgref + "/members";
return await fetch(apiurl(path), null, signal);
}
export async function fetchRuns(group, startRunID, lastrun, signal) {
let u = apiurl("/runs");
if (group) {
u.searchParams.append("group", group)
u.searchParams.append("group", group);
}
if (lastrun) {
u.searchParams.append("lastrun", true)
u.searchParams.append("lastrun", true);
}
if (startRunID) {
u.searchParams.append("start", startRunID)
u.searchParams.append("start", startRunID);
}
return await fetch(u, null, signal)
return await fetch(u, null, signal);
}
export async function fetchRun(runid, signal) {
@ -114,46 +122,46 @@ export async function fetchRun(runid, signal) {
}
export async function fetchTask(runid, taskid, signal) {
return await fetch(apiurl("/runs/" + runid + "/tasks/" + taskid), signal)
return await fetch(apiurl("/runs/" + runid + "/tasks/" + taskid), signal);
}
export async function fetchUser(username, signal) {
let path = "/users/" + username
let path = "/users/" + username;
return await fetch(apiurl(path), null, signal);
}
export async function fetchProjectGroup(projectgroupref, signal) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref)
let path = "/projectgroups/" + encodeURIComponent(projectgroupref);
return await fetch(apiurl(path), null, signal);
}
export async function fetchProjectGroupSubgroups(projectgroupref, signal) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref)
let path = "/projectgroups/" + encodeURIComponent(projectgroupref);
path += "/subgroups";
return await fetch(apiurl(path), null, signal);
}
export async function fetchProjectGroupProjects(projectgroupref, signal) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref)
let path = "/projectgroups/" + encodeURIComponent(projectgroupref);
path += "/projects";
return await fetch(apiurl(path), null, signal);
}
export async function fetchProject(ref, signal) {
let path = "/projects/" + encodeURIComponent(ref)
let path = "/projects/" + encodeURIComponent(ref);
return await fetch(apiurl(path), null, signal);
}
export async function fetchSecrets(ownertype, ref, all, signal) {
let path
let path;
if (ownertype == "project") {
path = "/projects/"
path = "/projects/";
} else if (ownertype == "projectgroup") {
path = "/projectgroups/"
path = "/projectgroups/";
}
path += encodeURIComponent(ref);
path += "/secrets";
@ -164,11 +172,11 @@ export async function fetchSecrets(ownertype, ref, all, signal) {
}
export async function fetchVariables(ownertype, ref, all, signal) {
let path
let path;
if (ownertype == "project") {
path = "/projects/"
path = "/projects/";
} else if (ownertype == "projectgroup") {
path = "/projectgroups/"
path = "/projectgroups/";
}
path += encodeURIComponent(ref);
path += "/variables";
@ -179,114 +187,120 @@ export async function fetchVariables(ownertype, ref, all, signal) {
}
export async function createOrganization(orgname, visibility, signal) {
let path = "/orgs"
let path = "/orgs";
let init = {
method: "POST",
body: JSON.stringify({
name: orgname,
visibility: visibility,
visibility: visibility
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function createUserToken(username, tokenname, signal) {
let path = "/users/" + username + "/tokens"
let path = "/users/" + username + "/tokens";
let init = {
method: "POST",
body: JSON.stringify({
token_name: tokenname,
token_name: tokenname
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function deleteUserToken(username, tokenname, signal) {
let path = "/users/" + username + "/tokens/" + tokenname
let path = "/users/" + username + "/tokens/" + tokenname;
let init = {
method: "DELETE",
}
return await fetch(apiurl(path), init, signal)
method: "DELETE"
};
return await fetch(apiurl(path), init, signal);
}
export async function createUserLinkedAccount(username, remotesourcename, loginname, password, signal) {
let path = "/users/" + username + "/linkedaccounts"
export async function createUserLinkedAccount(
username,
remotesourcename,
loginname,
password,
signal
) {
let path = "/users/" + username + "/linkedaccounts";
let init = {
method: "POST",
body: JSON.stringify({
remote_source_name: remotesourcename,
remote_source_login_name: loginname,
remote_source_login_password: password,
remote_source_login_password: password
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function deleteLinkedAccount(username, laid, signal) {
let path = "/users/" + username + "/linkedaccounts/" + laid
let path = "/users/" + username + "/linkedaccounts/" + laid;
let init = {
method: "DELETE",
}
return await fetch(apiurl(path), init, signal)
method: "DELETE"
};
return await fetch(apiurl(path), init, signal);
}
export async function restartRun(runid, fromStart, signal) {
let path = "/runs/" + runid + "/actions"
let path = "/runs/" + runid + "/actions";
let init = {
method: "PUT",
body: JSON.stringify({
action_type: "restart",
from_start: fromStart
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function cancelRun(runid, signal) {
let path = "/runs/" + runid + "/actions"
let path = "/runs/" + runid + "/actions";
let init = {
method: "PUT",
body: JSON.stringify({
action_type: "cancel"
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function stopRun(runid, signal) {
let path = "/runs/" + runid + "/actions"
let path = "/runs/" + runid + "/actions";
let init = {
method: "PUT",
body: JSON.stringify({
action_type: "stop"
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function approveTask(runid, taskid, signal) {
let path = "/runs/" + runid + "/tasks/" + taskid + "/actions"
let path = "/runs/" + runid + "/tasks/" + taskid + "/actions";
let init = {
method: "PUT",
body: JSON.stringify({
action_type: "approve"
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function fetchRemoteSources(signal) {
let path = "/remotesources"
let path = "/remotesources";
return await fetch(apiurl(path), null, signal);
}
export async function userRemoteRepos(remotesourceid, signal) {
let path = "/user/remoterepos/" + remotesourceid
let path = "/user/remoterepos/" + remotesourceid;
return await fetch(apiurl(path, null, signal));
}
export async function createProjectGroup(parentref, name, visibility, signal) {
let path = "/projectgroups"
let path = "/projectgroups";
let init = {
method: "POST",
body: JSON.stringify({
@ -294,24 +308,36 @@ export async function createProjectGroup(parentref, name, visibility, signal) {
parent_ref: parentref,
visibility: visibility
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function updateProjectGroup(projectgroupref, name, visibility, signal) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref)
export async function updateProjectGroup(
projectgroupref,
name,
visibility,
signal
) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref);
let init = {
method: "PUT",
body: JSON.stringify({
name: name,
visibility: visibility,
visibility: visibility
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function createProject(parentref, name, visibility, remotesourcename, remoterepopath, signal) {
let path = "/projects"
export async function createProject(
parentref,
name,
visibility,
remotesourcename,
remoterepopath,
signal
) {
let path = "/projects";
let init = {
method: "POST",
body: JSON.stringify({
@ -319,44 +345,45 @@ export async function createProject(parentref, name, visibility, remotesourcenam
parent_ref: parentref,
visibility: visibility,
remote_source_name: remotesourcename,
repo_path: remoterepopath,
repo_path: remoterepopath
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function updateProject(projectref, name, visibility, signal) {
let path = "/projects/" + encodeURIComponent(projectref)
let path = "/projects/" + encodeURIComponent(projectref);
let init = {
method: "PUT",
body: JSON.stringify({
name: name,
visibility: visibility,
visibility: visibility
})
}
return await fetch(apiurl(path), init, signal)
};
return await fetch(apiurl(path), init, signal);
}
export async function deleteProject(projectref, signal) {
let path = "/projects/" + encodeURIComponent(projectref)
let path = "/projects/" + encodeURIComponent(projectref);
let init = {
method: "DELETE",
}
return await fetch(apiurl(path), init, signal)
method: "DELETE"
};
return await fetch(apiurl(path), init, signal);
}
export async function projectUpdateRepoLinkedAccount(projectref, signal) {
let path = "/projects/" + encodeURIComponent(projectref) + "/updaterepolinkedaccount"
let path =
"/projects/" + encodeURIComponent(projectref) + "/updaterepolinkedaccount";
let init = {
method: "PUT",
}
return await fetch(apiurl(path), init, signal)
method: "PUT"
};
return await fetch(apiurl(path), init, signal);
}
export async function deleteProjectGroup(projectgroupref, signal) {
let path = "/projectgroups/" + encodeURIComponent(projectgroupref)
let path = "/projectgroups/" + encodeURIComponent(projectgroupref);
let init = {
method: "DELETE",
}
return await fetch(apiurl(path), init, signal)
method: "DELETE"
};
return await fetch(apiurl(path), init, signal);
}

View File

@ -38,7 +38,12 @@
<script>
import { fetchRemoteSources, login } from "@/util/data";
import { setLoggedUser, doLogout } from "@/util/auth";
import {
setLoggedUser,
unsetLoginRedirect,
setLoginRedirect,
doLogout
} from "@/util/auth";
import LoginForm from "@/components/loginform";
@ -63,6 +68,9 @@ export default {
this.remotesources = data;
},
async doLogin(username, password, remotesourcename) {
unsetLoginRedirect();
let redirect = this.$route.query["redirect"];
this.error = null;
let { data, error } = await login(username, password, remotesourcename);
@ -72,12 +80,20 @@ export default {
return;
}
if (data.oauth2_redirect) {
if (redirect) {
setLoginRedirect(redirect);
}
window.location = data.oauth2_redirect;
return;
}
setLoggedUser(data.token, data.user);
if (redirect) {
unsetLoginRedirect();
this.$router.push(redirect);
} else {
this.$router.push({ name: "home" });
}
}
},
mounted: function() {
this.$store.dispatch("setError", null);

View File

@ -13,7 +13,12 @@
<script>
import { fetch } from "@/util/data";
import { oauth2callbackurl, setLoggedUser } from "@/util/auth";
import {
oauth2callbackurl,
setLoggedUser,
unsetLoginRedirect,
getLoginRedirect
} from "@/util/auth";
export default {
components: {},
@ -41,7 +46,13 @@ export default {
if (data.request_type === "loginuser") {
setLoggedUser(data.response.token, data.response.user);
this.$router.push("/");
let redirect = getLoginRedirect(redirect);
if (redirect) {
unsetLoginRedirect();
this.$router.push(redirect);
} else {
this.$router.push({ name: "home" });
}
} else if (data.request_type === "authorize") {
this.$store.dispatch("setRegisterUser", data.response);
this.$router.push("/register");