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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 32 additions and 19 deletions

View file

@ -38,12 +38,7 @@ func HandleLogin(c *gin.Context) {
if err := r.FormValue("error"); err != "" {
http.Redirect(w, r, "/login/error?code="+err, 303)
} else {
intendedURL := r.URL.Query()["url"]
if len(intendedURL) > 0 {
http.Redirect(w, r, "/authorize?url="+intendedURL[0], 303)
} else {
http.Redirect(w, r, "/authorize", 303)
}
http.Redirect(w, r, "/authorize", 303)
}
}
@ -141,12 +136,7 @@ func HandleAuth(c *gin.Context) {
httputil.SetCookie(c.Writer, c.Request, "user_sess", tokenString)
intendedURL := c.Request.URL.Query()["url"]
if len(intendedURL) > 0 {
c.Redirect(303, intendedURL[0])
} else {
c.Redirect(303, "/")
}
c.Redirect(303, "/")
}
func GetLogout(c *gin.Context) {

View file

@ -26,6 +26,9 @@ var (
// Generate an SVG badge based on a build
func Generate(build *model.Build) string {
if build == nil {
return badgeNone
}
switch build.Status {
case model.StatusSuccess:
return badgeSuccess

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;
}