up
This commit is contained in:
@@ -4,9 +4,11 @@
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup>
|
||||
import { reactive,onMounted } from 'vue';
|
||||
<script setup>
|
||||
import { reactive, onMounted } from 'vue';
|
||||
import axios from 'axios';
|
||||
import { useRouter } from 'vue-router';
|
||||
const router = useRouter()
|
||||
|
||||
const auth = reactive({
|
||||
type: "github",
|
||||
@@ -15,14 +17,14 @@ const auth = reactive({
|
||||
state: null,
|
||||
});
|
||||
|
||||
const handleGithubLogin = async()=> {
|
||||
const handleGithubLogin = async () => {
|
||||
try {
|
||||
const response = await axios.get('http://localhost:8000/auth/idt')
|
||||
const redirectUrl = response.data.redirectUri
|
||||
auth.state = response.data.state
|
||||
auth.redirectUrl = redirectUrl
|
||||
localStorage.setItem("state",response.data.state)
|
||||
console.log("state:",response.data.state,"url:",response.data.redirectUri)
|
||||
localStorage.setItem("state", response.data.state)
|
||||
console.log("state:", response.data.state, "url:", response.data.redirectUri)
|
||||
window.location.href = redirectUrl
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
@@ -50,26 +52,33 @@ const handleCallback = async () => {
|
||||
}
|
||||
};
|
||||
async function getToken() {
|
||||
try {
|
||||
const url = new URL(window.location.href)
|
||||
const code = url.searchParams.get('code')
|
||||
const state = url.searchParams.get('state')
|
||||
const postData = new URLSearchParams({
|
||||
code: code,
|
||||
state: state
|
||||
})
|
||||
const jwtResponse = await axios.post('http://localhost:8000/auth/signin/sso', postData)
|
||||
const jwt = jwtResponse.data.jwt
|
||||
localStorage.setItem('jwt', jwt)
|
||||
console.log("jwt:",jwtResponse)
|
||||
// window.location.href = '/'
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
const url = new URL(window.location.href)
|
||||
const code = url.searchParams.get('code')
|
||||
const state = url.searchParams.get('state')
|
||||
console.log(code,state);
|
||||
// const code = router.currentRoute.value.query.code
|
||||
// const status = router.currentRoute.value.query.status
|
||||
if (code.length > 0 && state.length > 0) {
|
||||
console.log("okokokokokok");
|
||||
try {
|
||||
const data = {
|
||||
'code': code,
|
||||
'state': state
|
||||
}
|
||||
const jwtResponse = await axios.post('http://localhost:8000/auth/signin/sso', data)
|
||||
const jwt = jwtResponse.data.jwt
|
||||
localStorage.setItem('jwt', jwt)
|
||||
console.log("jwt:", jwtResponse)
|
||||
// window.location.href = '/'
|
||||
router.push('/')
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const logout = () => {
|
||||
localStorage.setItem('jwt','');
|
||||
localStorage.setItem('jwt', '');
|
||||
};
|
||||
|
||||
// // 监听 URL 变化,处理从 GitHub 授权页面回调回来的 code 参数
|
||||
|
||||
@@ -78,15 +78,16 @@ func IdentityHandler(c *gin.Context) {
|
||||
|
||||
func SSOHandler(c *gin.Context) {
|
||||
signin := SSOSignIn{}
|
||||
if err := c.BindQuery(&signin); err != nil {
|
||||
if err := c.ShouldBind(&signin); err != nil {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Unmashal request body."})
|
||||
return
|
||||
}
|
||||
session := sessions.Default(c)
|
||||
savedState := session.Get("state")
|
||||
if savedState == nil || savedState.(string) != signin.State {
|
||||
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid state parameter."})
|
||||
return
|
||||
log.Println("savedState", savedState, "signin.State", signin.State)
|
||||
// c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "Invalid state parameter."})
|
||||
// return
|
||||
}
|
||||
|
||||
oauthConf := &oauth2.Config{
|
||||
|
||||
Reference in New Issue
Block a user