feat(home): get user name from vscode and set user name into vscode
This commit is contained in:
		@@ -196,6 +196,7 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
        // Global variables
 | 
			
		||||
        DEVSTAR_HOME = "https://devstar.cn"
 | 
			
		||||
        var USERTOKEN = null
 | 
			
		||||
        var USERNAME = null
 | 
			
		||||
        var REPOLIST = []
 | 
			
		||||
        var PROJECTLIST = []
 | 
			
		||||
 | 
			
		||||
@@ -216,7 +217,15 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
                console.error("Failed to get user token from vscode: ", error)
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            if (USERTOKEN) {
 | 
			
		||||
            await getUsernameFromVSCode()
 | 
			
		||||
            .then(async username => {
 | 
			
		||||
                USERNAME = username
 | 
			
		||||
            })
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
                console.error('Failed to get user name from vscode: ', error)
 | 
			
		||||
            })
 | 
			
		||||
 | 
			
		||||
            if (USERTOKEN && USERNAME) {
 | 
			
		||||
                loadPageModules()
 | 
			
		||||
            } else {
 | 
			
		||||
                // TODO : do nothing or remind user to login
 | 
			
		||||
@@ -245,6 +254,23 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async function getUsernameFromVSCode() {
 | 
			
		||||
            return new Promise(async (resolve, reject) => {
 | 
			
		||||
                await communicateVSCodeByWebview('getUsername', null)
 | 
			
		||||
                .then(async data=> {
 | 
			
		||||
                    const username = data.username
 | 
			
		||||
                    if (username === undefined) {
 | 
			
		||||
                        reject('username is undefined')
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    resolve(username)
 | 
			
		||||
                })
 | 
			
		||||
                .catch(error => {
 | 
			
		||||
                    reject(error)
 | 
			
		||||
                })
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function verifyToken(token) {
 | 
			
		||||
            return new Promise((resolve, reject) => {
 | 
			
		||||
                fetch(DEVSTAR_HOME + '/api/devcontainer/user', {
 | 
			
		||||
@@ -332,6 +358,8 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
                .catch(error => {
 | 
			
		||||
                    console.error('Error in setting user token to vscode global state:', error)
 | 
			
		||||
                })
 | 
			
		||||
                setUsernameToVSCode(username)
 | 
			
		||||
 | 
			
		||||
                closeLoginModal()
 | 
			
		||||
            })
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
@@ -351,7 +379,7 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
            return name 
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        function logout() {
 | 
			
		||||
        async function logout() {
 | 
			
		||||
            // remove token from global variable and vscode global state
 | 
			
		||||
            USERTOKEN = null
 | 
			
		||||
            communicateVSCodeByWebview('setUserToken', { userToken: ''})
 | 
			
		||||
@@ -365,9 +393,30 @@ header("Allow: GET, POST, OPTIONS, PUT, DELETE");
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
                console.error('Error in removing user token from vscode global state:', error)
 | 
			
		||||
            })
 | 
			
		||||
            await setUsernameToVSCode('')
 | 
			
		||||
 | 
			
		||||
            location.reload()
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        async function setUsernameToVSCode(username) {
 | 
			
		||||
            var removeUsername = false;
 | 
			
		||||
            if (username == '') {
 | 
			
		||||
                removeUsername = true;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            await communicateVSCodeByWebview('setUsername', {username: username})
 | 
			
		||||
            .then(result => {
 | 
			
		||||
                if (result.ok) {
 | 
			
		||||
                    console.log(removeUsername ? 'User name has been removed from vscode': 'User name has been stored in vscode')
 | 
			
		||||
                } else {
 | 
			
		||||
                    console.error(removeUsername ? 'Failed to removing user name from vscode': 'Failed to store user name in vscode')
 | 
			
		||||
                }
 | 
			
		||||
            })
 | 
			
		||||
            .catch(error => {
 | 
			
		||||
               console.error("Error happened when setting user name: ", error) 
 | 
			
		||||
            })
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // ===================================== Repo ===========================
 | 
			
		||||
        function loadRepositories() {
 | 
			
		||||
            // clear old data
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user