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