Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Turnstile #36

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"format": "prettier --write src/"
},
"dependencies": {
"@vicons/fa": "^0.12.0",
"@vicons/fluent": "^0.12.0",
"@vicons/ionicons4": "^0.12.0",
"@vicons/ionicons5": "^0.12.0",
"@vicons/material": "^0.12.0",
"@vicons/fa": "^0.12.0",
"@vicons/fluent": "^0.12.0",
"axios": "^1.7.7",
"highlight.js": "^11.10.0",
"js-cookie": "^3.0.5",
Expand All @@ -33,6 +33,7 @@
"vue-native-websocket": "^2.0.15",
"vue-route": "^1.5.1",
"vue-router": "^4.4.5",
"vue-turnstile": "^1.0.11",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using https://github.com/Astrian/cfturnstile-vue3
vue-turnstile is designed for Vue 2.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/ruigomeseu/vue-turnstile

It's designed for Vue 3 instead of Vue 2, if you have any questions about the Options API, please refer to: https://cn.vuejs.org/guide/introduction.html#options-api

"vuex": "^4.1.0"
},
"devDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions src/api/v2/auth/login.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import { post } from '@/utils/request'
import base from '@/api/base'

const login = async (username: string, password: string) => {
const login = async (username: string, password: string, captcha_response_token: string) => {
const rs = post(`${base.api_v2_url}/auth/login`, {
username: username,
password: password
password: password,
captcha_response_token: captcha_response_token
})
return base.buildResponse(await rs)
}
Expand Down
63 changes: 30 additions & 33 deletions src/views/auth/LoginView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,25 @@
>
没有账户?去注册
</n-button>
<n-button type="primary" @click="login"> 登录</n-button>
<n-button type="primary" @click="showTurnstile = true"> 登录</n-button>
<!-- Turnstile -->
<n-modal
v-model:show="showTurnstile"
:mask-closable="false"
preset="card"
title="请完成人机验证"
style="min-width: 300px; width: min-content"
>
<vue-turnstile
site-key="0x4AAAAAAAEXAhvwOKerpBsb"
v-model="token"
@error="(code) => {
showTurnstile = false
sendErrorMessage(`验证码加载失败,错误代码: ${code}`)
}"
@unsupported="sendErrorMessage('您的浏览器不支持加载验证码,请更换或升级浏览器后重试')"
/>
</n-modal>
</n-space>
</n-space>
</div>
Expand All @@ -53,21 +71,25 @@
</template>

<script setup>
import { ref } from 'vue'
import { ref, watch } from 'vue'
import { useLoadingBar, useMessage } from 'naive-ui'
import router from '@router'
import userData from '@/utils/stores/userData/store'
import { sendErrorMessage } from '@/utils/message'
import logger from '@/utils/logger'
import api from '@/api'
import { getUrlKey } from '@/utils/request'
import VueTurnstile from 'vue-turnstile';

const formRef = ref(null)
const message = useMessage()
const ldb = useLoadingBar()
const qqLoginLoading = ref(false)
// const oauthLogin_loading = ref(false)

let token = ref('')
const showTurnstile = ref(false)

const model = ref([
{
username: '',
Expand All @@ -81,38 +103,13 @@ if (redirect !== null) {
logger.info('Redirect after login: ' + redirect)
}

// if (token !== null) {
// onMounted(async () => {
// other_login.value = true
// let rs
// try {
// rs = await api.v2.auth.oauth.login.token(token)
// } catch (e) {
// sendErrorMessage('登录失败: ' + e)
// router.push('/auth/login')
// }
// if (!rs) return
// if (rs.status === 200) {
// message.success(rs.data.username + ',欢迎回来!')
// userData.commit('set_token', rs.data.token)
// userData.commit('set_user_info', rs.data)
// router.push(redirect || '/dashboard')
// }
// })
// }

// LoCyan OAuth 2.0
// There is no need
// function oauthLogin() {
// oauthLogin_loading.value = true
// window.location.href =
// 'https://api-v2.locyanfrp.cn/api/v2/oauth/authorize?redirectUrl=http://' +
// window.location.host +
// '/auth/login'
// }
watch(token, (newToken, _) => {
showTurnstile.value = false
login(newToken)
})

// 登录
async function login() {
async function login(turnstileToken) {
ldb.start()
if (
model.value.username === null ||
Expand All @@ -126,7 +123,7 @@ async function login() {
}
let rs
try {
rs = await api.v2.auth.login(model.value.username, model.value.password)
rs = await api.v2.auth.login(model.value.username, model.value.password, turnstileToken)
} catch (e) {
sendErrorMessage('请求失败: ' + e)
}
Expand Down