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(clientID),
|
||||||
url.QueryEscape(redirectURI))
|
url.QueryEscape(redirectURI))
|
||||||
|
|
||||||
// 重定向到授权页面
|
// 返回授权 URL 而不是直接重定向
|
||||||
c.Redirect(http.StatusTemporaryRedirect, authURL)
|
c.JSON(http.StatusOK, gin.H{
|
||||||
|
"auth_url": authURL,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func Logout(c *gin.Context) {
|
func Logout(c *gin.Context) {
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
</span>
|
</span>
|
||||||
<el-button @click="handleLogout">退出</el-button>
|
<el-button @click="handleLogout">退出</el-button>
|
||||||
</template>
|
</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>
|
||||||
</div>
|
</div>
|
||||||
</el-header>
|
</el-header>
|
||||||
@ -46,10 +46,12 @@ import { ref, onMounted, provide } from 'vue'
|
|||||||
import { User } from '@element-plus/icons-vue'
|
import { User } from '@element-plus/icons-vue'
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import { useRouter, useRoute } from 'vue-router'
|
import { useRouter, useRoute } from 'vue-router'
|
||||||
|
import { ElMessage } from 'element-plus'
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
const globalUser = ref(null)
|
const globalUser = ref(null)
|
||||||
|
const loading = ref(false)
|
||||||
|
|
||||||
const updateGlobalUser = (user) => {
|
const updateGlobalUser = (user) => {
|
||||||
globalUser.value = user
|
globalUser.value = user
|
||||||
@ -57,6 +59,26 @@ const updateGlobalUser = (user) => {
|
|||||||
|
|
||||||
provide('updateGlobalUser', updateGlobalUser)
|
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 () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
const { data } = await axios.get('/api/auth/status')
|
const { data } = await axios.get('/api/auth/status')
|
||||||
|
@ -24,15 +24,20 @@ const updateGlobalUser = inject('updateGlobalUser')
|
|||||||
const handleLogin = async () => {
|
const handleLogin = async () => {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
try {
|
try {
|
||||||
await axios.post('/api/auth/login')
|
const { data } = await axios.post('/api/auth/login')
|
||||||
const { data } = await axios.get('/api/auth/status')
|
if (data.auth_url) {
|
||||||
updateGlobalUser(data.user)
|
// 直接重定向到授权页面
|
||||||
ElMessage.success('登录成功')
|
window.location.href = data.auth_url
|
||||||
router.push('/prices')
|
} else {
|
||||||
|
// 处理开发环境下的直接登录
|
||||||
|
const { data: userData } = await axios.get('/api/auth/status')
|
||||||
|
updateGlobalUser(userData.user)
|
||||||
|
ElMessage.success('登录成功')
|
||||||
|
router.push('/prices')
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to login:', error)
|
console.error('Failed to login:', error)
|
||||||
ElMessage.error('登录失败')
|
ElMessage.error('登录失败')
|
||||||
} finally {
|
|
||||||
loading.value = false
|
loading.value = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user