fix(auth): return to origin path after GitHub login

Sign-in saves the current path in sessionStorage so the OAuth callback
can route back instead of always landing on Home.
This commit is contained in:
Julien Calixte
2026-05-29 16:49:44 +02:00
parent a09e541fa8
commit d99672dbd8
3 changed files with 30 additions and 2 deletions

View File

@@ -3,6 +3,7 @@ import { onBeforeMount, ref } from "vue"
import { useRoute, useRouter } from "vue-router"
import { useGitHubLogin } from "@/hooks/useGitHubLogin.hook"
import { consumeGithubOAuthReturnPath } from "@/modules/user/service/oauthReturnPath"
import { signIn } from "@/modules/user/service/signIn"
const route = useRoute()
@@ -22,7 +23,13 @@ onBeforeMount(async () => {
await saveCredentials(token)
}
router.replace({ name: "Home" })
const returnPath = consumeGithubOAuthReturnPath()
if (!hasError.value && returnPath) {
router.replace(returnPath)
} else {
router.replace({ name: "Home" })
}
}
})
</script>

View File

@@ -1,20 +1,34 @@
<script lang="ts" setup>
import { useRoute } from "vue-router"
import { GITHUB_OAUTH_RETURN_PATH_KEY } from "@/modules/user/service/oauthReturnPath"
const GITHUB_URL = "https://github.com/login/oauth/authorize"
const CLIENT_ID = "Iv1.12dc43d013ce3623"
const SCOPE = "repo%20workflow"
const REDIRECT_URI = window.location.origin
const route = useRoute()
const url = new URL(GITHUB_URL)
url.searchParams.set("client_id", CLIENT_ID)
url.searchParams.set("scope", SCOPE)
url.searchParams.set("redirect_uri", REDIRECT_URI)
const href = url.toString()
const saveReturnPath = () => {
sessionStorage.setItem(GITHUB_OAUTH_RETURN_PATH_KEY, route.fullPath)
}
</script>
<template>
<a :href="href" class="sign-in-github btn btn-sm btn-primary">
<a
:href="href"
class="sign-in-github btn btn-sm btn-primary"
@click="saveReturnPath"
>
Sign in with
<svg
xmlns="http://www.w3.org/2000/svg"

View File

@@ -0,0 +1,7 @@
export const GITHUB_OAUTH_RETURN_PATH_KEY = "github-oauth-return-path"
export const consumeGithubOAuthReturnPath = (): string | null => {
const path = sessionStorage.getItem(GITHUB_OAUTH_RETURN_PATH_KEY)
sessionStorage.removeItem(GITHUB_OAUTH_RETURN_PATH_KEY)
return path
}