当前位置: 首页 > 知识库问答 >
问题:

如何使用Okta auth和Composition API设置Vue应用程序

平庆
2023-03-14

我正在使用Okta设置一个基本的Vue身份验证应用程序,基于以下内容:https://github.com/okta/okta-vue.我试图将处理路由和登录重定向的逻辑从Options API转换为Composition API。换句话说,应用程序中的登录/注销功能。vue,目前是这样写的:

    async login () {
      await this.$auth.signInWithRedirect()
      this.$router.push('/protected')
    },
    async logout () {
      await this.$auth.signOut()
    }

https://github.com/okta/okta-vue#show-login-and-logout-buttons

在Composition API中可能看起来像这样(我认为):

    setup() {
      const router = useRouter()
      
      const login = () => {
        this.$auth.signInWithRedirect()
      }

      const logout = () => {
        this.$auth.signOut()
      }

      return {login, logout, router}
    }

然而,我不知道如何改变这一点$啊。登录WithRedirect()$啊。signOut()使用Composition API,因为$auth似乎不是可接受的属性。如何设置Okta方法signInWithRedirect()signOut()来使用composition API?我不熟悉Typescript中的合成API和Vue。感谢您的反馈!

下面是代码的其余部分:

应用程序。vue

<template>
  <div id="app">
    <button v-if='authState && authState.isAuthenticated' @click='logout' id='logout-button'> Logout </button>
    <button v-else @click='login' id='login-button'> Login </button>
    <router-view/>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { useRouter } from 'vue-router'

export default defineComponent({
  name: 'app',
    setup() {
      const router = useRouter()
      
      const login = () => {
        this.$auth.signInWithRedirect()
        router.push('/protected')
      }

      const logout = () => {
        this.$auth.signOut()
      }

      return {login, logout, router}
    }
  
})
</script>

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { OktaAuth } from '@okta/okta-auth-js'
import OktaVue from '@okta/okta-vue'

const oktaAuth = new OktaAuth({
    issuer: 'https://{dev-id}.okta.com/oauth2/default',
    clientId: '{clientId}',
    redirectUri: 'http://localhost:8080/login/callback',
    scopes: ['openid', 'profile', 'email']
  })

  const app = createApp(App)
  app.use(OktaVue, { oktaAuth })
  app.use(router)
  app.mount('#app')

路由器/索引。ts

import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
import { LoginCallback } from '@okta/okta-vue'
import { navigationGuard } from '@okta/okta-vue'
import Protected from '../views/Protected.vue'

const routes: Array<RouteRecordRaw> = [
  { 
    path: '/login/callback', 
    component: LoginCallback 
  },
  {
    path: '/protected',
    name: 'Protected',
    component: Protected,
    meta: {
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach(navigationGuard)

export default router

暂时还没有答案

 类似资料:
  • 设置应用程序 Nest is built with features from both ES6 and ES7 (decorators, async / await). It means, that the easiest way to start adventure with it is to use Babel or TypeScript. In this tutorial I will u

  • 轻应用的使用者支持按照组织机构或单人进行添加,同时可以设置添加范围和移除权限。 设置组织机构:轻应用-设置使用组织结构(勾选组织机构节点后,所有自组织机构及成员都将订阅此应用) 设置使用者:轻应用-新增使用者/删除使用者 设置添加范围和移除权限

  • 我的任务是创建一个支持typescript的vue3应用程序,我还必须使用vuex进行状态管理,使用vue路由器进行基本路由。但我无法在此项目中使用vue cli 我目前的代码: 由于我从未在vue项目中使用过typescript 您能否向我推荐一些博客、教程,其中有人使用这些工具从头开始构建vue3应用程序,但没有vue cli? 全速行驶: 在VueJS 3.0框架上使用TypeScript编

  • 问题内容: 如何为Android应用程序设置IntelliJ IDEA? 问题答案: 我花了整整一天的时间尝试将所有内容组合在一起,涉及数百个站点和教程,但是它们都跳过了琐碎的步骤。 因此,这里是完整的指南: 下载并安装 Java JDK (选择Java平台) 下载并安装 Android SDK (建议安装程序) android SD完成安装后,在 Android SDK Tools* 下打开 S

  • 了解如何在 Apple TV(第 4 代或更新机型)上下载和设置 Lightroom 应用程序。 注意:App Store 不再提供 Lightroom for Apple TV 版本 1.2.3。此版本已替换为较新版本的 Lightroom for Apple TV 应用程序,且该较新版本的应用程序与 iPhone 版 Lightroom 捆绑在一起。 如果您当前使用的是 Lightroom f

  • 如何设置电子应用程序的应用程序图标? 我正在尝试