fix redirect after login (#824)

if you are in buildView and login, go back to this buildView again
This commit is contained in:
Anbraten
2022-05-14 17:45:01 +02:00
committed by GitHub
parent d06dfc86b4
commit 70af29f9a2
7 changed files with 32 additions and 19 deletions

View File

@@ -1,13 +1,17 @@
import useConfig from '~/compositions/useConfig';
import useUserConfig from '~/compositions/useUserConfig';
export default () =>
({
isAuthenticated: useConfig().user,
isAuthenticated: !!useConfig().user,
user: useConfig().user,
authenticate(origin?: string) {
const url = `/login?url=${origin || ''}`;
window.location.href = url;
authenticate(url?: string) {
if (url) {
const config = useUserConfig();
config.setUserConfig('redirectUrl', url);
}
window.location.href = '/login';
},
} as const);

View File

@@ -4,10 +4,12 @@ const USER_CONFIG_KEY = 'woodpecker-user-config';
type UserConfig = {
isBuildFeedOpen: boolean;
redirectUrl: string;
};
const defaultUserConfig: UserConfig = {
isBuildFeedOpen: false,
redirectUrl: '',
};
function loadUserConfig(): UserConfig {

View File

@@ -1,7 +1,8 @@
import { Component } from 'vue';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router';
import useAuthentication from './compositions/useAuthentication';
import useAuthentication from '~/compositions/useAuthentication';
import useUserConfig from '~/compositions/useUserConfig';
const routes: RouteRecordRaw[] = [
{
@@ -26,7 +27,6 @@ const routes: RouteRecordRaw[] = [
name: 'repos-owner',
component: (): Component => import('~/views/ReposOwner.vue'),
props: true,
meta: { authentication: 'required' },
},
{
path: '/:repoOwner/:repoName',
@@ -133,6 +133,13 @@ const router = createRouter({
});
router.beforeEach(async (to, _, next) => {
const config = useUserConfig();
const { redirectUrl } = config.userConfig.value;
if (redirectUrl !== '') {
config.setUserConfig('redirectUrl', '');
next(redirectUrl);
}
const authentication = useAuthentication();
if (to.meta.authentication === 'required' && !authentication.isAuthenticated) {
next({ name: 'login', query: { url: to.fullPath } });

View File

@@ -53,7 +53,7 @@ export default defineComponent({
const errorMessage = ref<string>();
function doLogin() {
const url = typeof route.query.origin === 'string' ? route.query.origin : '';
const url = typeof route.query.url === 'string' ? route.query.url : '';
authentication.authenticate(url);
}

View File

@@ -39,6 +39,7 @@ import FluidContainer from '~/components/layout/FluidContainer.vue';
import Tab from '~/components/tabs/Tab.vue';
import Tabs from '~/components/tabs/Tabs.vue';
import useApiClient from '~/compositions/useApiClient';
import useAuthentication from '~/compositions/useAuthentication';
import useNotifications from '~/compositions/useNotifications';
import { RepoPermissions } from '~/lib/api/types';
import BuildStore from '~/store/builds';
@@ -72,6 +73,7 @@ export default defineComponent({
const buildStore = BuildStore();
const apiClient = useApiClient();
const notifications = useNotifications();
const { isAuthenticated } = useAuthentication();
const route = useRoute();
const router = useRouter();
@@ -86,6 +88,11 @@ export default defineComponent({
repoPermissions.value = await apiClient.getRepoPermissions(repoOwner.value, repoName.value);
if (!repoPermissions.value.pull) {
notifications.notify({ type: 'error', title: 'Not allowed to access this repository' });
// no access and not authenticated, redirect to login
if (!isAuthenticated) {
await router.replace({ name: 'login', query: { url: route.fullPath } });
return;
}
await router.replace({ name: 'home' });
return;
}