mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-19 06:01:59 +08:00
Modify login flow to support dynamic OAuth 2.0 authorization URL
This commit is contained in:
parent
bd8656542b
commit
e7967dfeff
@ -112,8 +112,10 @@ func Login(c *gin.Context) {
|
||||
url.QueryEscape(clientID),
|
||||
url.QueryEscape(redirectURI))
|
||||
|
||||
// 重定向到授权页面
|
||||
c.Redirect(http.StatusTemporaryRedirect, authURL)
|
||||
// 返回授权 URL 而不是直接重定向
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"auth_url": authURL,
|
||||
})
|
||||
}
|
||||
|
||||
func Logout(c *gin.Context) {
|
||||
|
@ -20,7 +20,7 @@
|
||||
</span>
|
||||
<el-button @click="handleLogout">退出</el-button>
|
||||
</template>
|
||||
<el-button v-else type="primary" @click="$router.push('/login')">登录</el-button>
|
||||
<el-button v-else type="primary" @click="handleLogin" :loading="loading">登录</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</el-header>
|
||||
@ -46,10 +46,12 @@ import { ref, onMounted, provide } from 'vue'
|
||||
import { User } from '@element-plus/icons-vue'
|
||||
import axios from 'axios'
|
||||
import { useRouter, useRoute } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
|
||||
const router = useRouter()
|
||||
const route = useRoute()
|
||||
const globalUser = ref(null)
|
||||
const loading = ref(false)
|
||||
|
||||
const updateGlobalUser = (user) => {
|
||||
globalUser.value = user
|
||||
@ -57,6 +59,26 @@ const updateGlobalUser = (user) => {
|
||||
|
||||
provide('updateGlobalUser', updateGlobalUser)
|
||||
|
||||
const handleLogin = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const { data } = await axios.post('/api/auth/login')
|
||||
if (data.auth_url) {
|
||||
// 直接重定向到授权页面
|
||||
window.location.href = data.auth_url
|
||||
} else {
|
||||
// 处理开发环境下的直接登录
|
||||
const { data: userData } = await axios.get('/api/auth/status')
|
||||
updateGlobalUser(userData.user)
|
||||
ElMessage.success('登录成功')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to login:', error)
|
||||
ElMessage.error('登录失败')
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
try {
|
||||
const { data } = await axios.get('/api/auth/status')
|
||||
|
@ -24,15 +24,20 @@ const updateGlobalUser = inject('updateGlobalUser')
|
||||
const handleLogin = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
await axios.post('/api/auth/login')
|
||||
const { data } = await axios.get('/api/auth/status')
|
||||
updateGlobalUser(data.user)
|
||||
const { data } = await axios.post('/api/auth/login')
|
||||
if (data.auth_url) {
|
||||
// 直接重定向到授权页面
|
||||
window.location.href = data.auth_url
|
||||
} else {
|
||||
// 处理开发环境下的直接登录
|
||||
const { data: userData } = await axios.get('/api/auth/status')
|
||||
updateGlobalUser(userData.user)
|
||||
ElMessage.success('登录成功')
|
||||
router.push('/prices')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to login:', error)
|
||||
ElMessage.error('登录失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user