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:
@@ -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,8 +23,14 @@ onBeforeMount(async () => {
|
||||
await saveCredentials(token)
|
||||
}
|
||||
|
||||
const returnPath = consumeGithubOAuthReturnPath()
|
||||
|
||||
if (!hasError.value && returnPath) {
|
||||
router.replace(returnPath)
|
||||
} else {
|
||||
router.replace({ name: "Home" })
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
7
src/modules/user/service/oauthReturnPath.ts
Normal file
7
src/modules/user/service/oauthReturnPath.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user