|
1 | 1 | <script lang="ts" setup>
|
| 2 | +import { NButton, NFlex } from 'naive-ui'; |
| 3 | +
|
| 4 | +import { onMounted, ref } from 'vue'; |
| 5 | +
|
| 6 | +import { useRoute, useRouter } from 'vue-router'; |
| 7 | +
|
| 8 | +import type { LoginAuthResponse } from '~/models/login/login-auth-response'; |
| 9 | +
|
| 10 | +import type { TraktDeviceAuthentication } from '~/models/trakt/trakt-authentication.model'; |
| 11 | +
|
| 12 | +import { isLoginAuthResponseSuccess } from '~/models/login/login-auth-response'; |
| 13 | +
|
| 14 | +import { traktService } from '~/services/trakt-client/trakt-client.service'; |
2 | 15 | import { useI18n } from '~/utils';
|
3 | 16 |
|
| 17 | +type LoginQueryParams = { |
| 18 | + redirect: string; |
| 19 | +} & LoginAuthResponse; |
| 20 | +
|
4 | 21 | const i18n = useI18n('global');
|
| 22 | +
|
| 23 | +const authResponse = ref<LoginQueryParams>(); |
| 24 | +const route = useRoute(); |
| 25 | +const router = useRouter(); |
| 26 | +
|
| 27 | +onMounted(async () => { |
| 28 | + authResponse.value = route.query as LoginQueryParams; |
| 29 | +
|
| 30 | + if (isLoginAuthResponseSuccess(authResponse.value)) { |
| 31 | + const response = await traktService.exchangeCodeForToken(authResponse.value.code); |
| 32 | + if (authResponse.value.redirect) await router.push(authResponse.value.redirect); |
| 33 | + } |
| 34 | +}); |
| 35 | +
|
| 36 | +const auth = ref<TraktDeviceAuthentication>(); |
| 37 | +
|
| 38 | +const redirect = async () => { |
| 39 | + try { |
| 40 | + const response = await traktService.redirectToAuthentication(); |
| 41 | + await chrome.tabs.create({ url: response.url }); |
| 42 | + } catch (error) { |
| 43 | + console.error('Error:', error); |
| 44 | + } |
| 45 | +}; |
| 46 | +
|
| 47 | +const login = async () => { |
| 48 | + const device = await traktService.getDeviceCode(); |
| 49 | + auth.value = device; |
| 50 | +
|
| 51 | + const response = await traktService.pollWithDeviceCode(device); |
| 52 | + console.info('response', response); |
| 53 | +}; |
| 54 | +
|
| 55 | +const writeToClipboard = (text: string) => { |
| 56 | + navigator.clipboard.writeText(text); |
| 57 | +}; |
5 | 58 | </script>
|
6 | 59 |
|
7 | 60 | <template>
|
8 |
| - <img alt="Vue logo" class="logo" src="/assets/logo.svg" width="125" height="125" /> |
9 |
| - <span>This is a login component</span> |
| 61 | + <NFlex vertical justify="space-around" align="center"> |
| 62 | + <img alt="Vue logo" class="logo" src="/assets/logo.svg" width="125" height="125" /> |
| 63 | + <span>This is a login component</span> |
| 64 | + <NButton @click="redirect">Redirect Url</NButton> |
| 65 | + <NButton @click="login">Generate Codes</NButton> |
| 66 | + <template v-if="auth"> |
| 67 | + {{ JSON.stringify(auth, undefined, 2) }} |
| 68 | + <NButton @click="writeToClipboard(auth.user_code)">{{ auth.user_code }}</NButton> |
| 69 | + <NButton tag="a" :href="auth.verification_url" target="_blank">{{ auth.verification_url }}</NButton> |
| 70 | + </template> |
| 71 | + </NFlex> |
10 | 72 | </template>
|
11 | 73 |
|
12 | 74 | <style lang="scss" scoped>
|
|
0 commit comments