Modify login flow to support dynamic OAuth 2.0 authorization URL

This commit is contained in:
wood chen 2025-02-08 20:21:39 +08:00
parent bd8656542b
commit e7967dfeff
3 changed files with 38 additions and 9 deletions

View File

@ -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) {

View File

@ -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')

View File

@ -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
}
}