-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #251 from codex-team/auth-guard
feat(auth): Implemented authRequired decoration for routes
- Loading branch information
Showing
15 changed files
with
189 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { useAppState } from './useAppState.ts'; | ||
import useAuth from './useAuth.ts'; | ||
import { useRouter } from 'vue-router'; | ||
|
||
/** | ||
* Function that is used in App for checking user authorization | ||
* Works only for routes with authRequired set to true in route.meta | ||
* If user is not authorized will show auth popup | ||
*/ | ||
export default function useAuthRequired(): void { | ||
const { showGoogleAuthPopup } = useAuth(); | ||
const router = useRouter(); | ||
const { user } = useAppState(); | ||
|
||
/** | ||
* Check if user is authorized | ||
* If authorization is in process we treat user as unauthorized | ||
* When oauth will work, it will be treated as he authorized manually | ||
* @returns true if user is authorized, false otherwise | ||
*/ | ||
function isUserAuthorized(): boolean { | ||
return (user.value !== null && user.value !== undefined); | ||
} | ||
|
||
/** | ||
* For each route check if auth is required | ||
*/ | ||
router.beforeEach((actualRoute, _, next) => { | ||
if (actualRoute.meta.authRequired === true && !isUserAuthorized()) { | ||
/** | ||
* If auth is required and user is not autorized | ||
* Then show google auth popup and redirect user to auth page | ||
*/ | ||
showGoogleAuthPopup(); | ||
next(`/auth?redirect=${actualRoute.fullPath?.toString()}`); | ||
} else { | ||
next(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<template> | ||
<div :class="$style['message']"> | ||
{{ t('authorize.message') }} | ||
<Button | ||
@click="showGoogleAuthPopup" | ||
> | ||
{{ t('auth.login') }} | ||
</Button> | ||
</div> | ||
</template> | ||
|
||
<script setup lang="ts"> | ||
import { useAppState } from '@/application/services/useAppState'; | ||
import useAuth from '@/application/services/useAuth'; | ||
import { useI18n } from 'vue-i18n'; | ||
import { watch } from 'vue'; | ||
import { useRouter } from 'vue-router'; | ||
import { Button } from 'codex-ui/vue'; | ||
const { user } = useAppState(); | ||
const { showGoogleAuthPopup } = useAuth(); | ||
const { t } = useI18n(); | ||
const router = useRouter(); | ||
const props = defineProps<{ | ||
/** | ||
* Link to auth guarded page | ||
* If user would authorized he will be redirected via this link | ||
*/ | ||
redirect: string; | ||
}>(); | ||
/** | ||
* Checks status of user authorization | ||
* If user had been authorized, then redirects to page that he wanted to visit | ||
*/ | ||
watch(user, () => { | ||
if (user.value !== null && user.value !== undefined) { | ||
router.push(props.redirect); | ||
} | ||
}); | ||
</script> | ||
|
||
<style lang="postcss" module> | ||
.message { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
height: 100vh; | ||
flex-direction: column; | ||
gap: var(--spacing-l) | ||
} | ||
</style> |
Oops, something went wrong.