網站首頁 編程語言 正文
前端directus對接單點登錄
只負責前端對接已經在direcuts中設置的單點登錄,不考慮如何在direcuts設置單點登錄
導入directus的sdk,調用封裝好的登錄、登出、及刷新方法。
directus.ts
import {Directus} from '@directus/sdk';
const directus = new Directus('directusUrl');
export async function login(email: string, password: string) {
let flag = false;
await directus.auth
.login({email, password})
.then((res) => {
if (typeof res.access_token !== 'undefined') {
flag = true;
}
})
return flag;
}
export async function refreshtoken() {
await directus.auth.refresh();
console.log(' >================(directus.ts:23)', 'fresh token');
}
export async function logout() {
await directus.auth.logout();
}
export async function userinfo() {
const userInfo = await directus.users.me.read();
return userInfo
}
setInterval(() => {
refreshtoken()
}, 600000)
/router/index.ts 添加路由守衛
// 路由守衛
Router.beforeEach((to) => {
const endtime: string | null = LocalStorage.getItem('auth_expires_at')
if (tools.checkexpires(typeof endtime === 'string' ? endtime : '')) {
console.log(' >================(index.ts:42)', 11);
return {name: 'login'}
}
const isAuthentication = LocalStorage.getItem('auth_token')
if (isAuthentication == null && to.name !== 'login' && to.name !== 'callback') {
return {name: 'login'}
}
});
checkexpires()
//時間戳判斷
static checkexpires(endtime: string) {
if (endtime === '') {
return true
}
const now = (new Date()).getTime()
if (String(now) < endtime) {
return false
} else {
console.log(' >================now,endtime(tools.ts:61)', now, endtime);
refreshtoken()
return true
}
登錄頁
- 可以加一個form表單實現原生directus登錄
<template>
<form>
<div class="col-3 q-col-gutter-y-md q-pt-xl">
<q-input v-model="email" type="email" label="郵箱" outlined/>
</div>
<div class="col-3 q-gutter-md">
<q-input v-model="password" type="password" label="密碼" outlined/>
</div>
</form>
</template>
<script>
const email = ref('')
const password = ref('')
function checkLogin() {
login(email.value, password.value).then((r) => {
if (r) {
router.push({
path: '/'
})
}
})
}
</script>
-
單點登錄跳轉只需添加一個超鏈接,配置好redirect參數即可
<q-btn class="btn-fixed-width" flat label="OPENID登錄" style="background:#112234;color:#fff;" :href='openidurl'></q-btn> const openidurl = `https://material-api.dev.supercomputing.link/auth/login/openid?redirect=${window.location.origin}/Oidc/callback`
-
CallBack.vue
<template> <q-page> <div class="fixed-center text-center"> <p> <!-- <q-icon name="person" size="8em" color="grey-5" /> --> <q-circular-progress indeterminate size="100px" :thickness="0.22" color="primary" track-color="grey-3" class="q-ma-md" /> </p> <p class="text-body2 text-weight-regular">正在登錄,請稍候...</p> <p></p> </div> </q-page> </template> <script lang="ts"> import {defineComponent, onMounted} from 'vue'; import {refreshtoken,} from 'src/service/directus'; export default defineComponent({ setup() { onMounted(async () => { await refreshtoken().then(() => { window.location.href = window.location.origin + '/project'; }) }); }, }); </script>
-
原文鏈接:https://blog.csdn.net/weixin_43815091/article/details/126518634
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-09-17 el-tabs表格右下角按鈕被遮擋
- 2023-05-20 工程級?React?注冊登錄全棧級流程分析_React
- 2023-07-07 更新node后項目報錯
- 2023-05-31 Pandas.DataFrame刪除指定行和列(drop)的實現_python
- 2022-07-21 解決win10系統網絡連接正常,但是網頁打不開的問題
- 2023-03-26 CSS填充和寬高詳解_基礎教程
- 2022-05-08 總結Python函數參數的六種類型_python
- 2022-06-27 python?使用ctypes調用C/C++?dll詳情_python
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支