Skip to content

Commit 048ccfc

Browse files
committed
token is returned to client ❤️
1 parent 5d4d43a commit 048ccfc

4 files changed

Lines changed: 30 additions & 61 deletions

File tree

background/authorize.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,3 @@ function validate(redirectURL) {
6363
}
6464

6565

66-
67-
/**
68-
Authenticate and authorize using browser.identity.launchWebAuthFlow().
69-
If successful, this resolves with a redirectURL string that contains
70-
an access token.
71-
*/
72-

background/main.js

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,28 @@ var token
88
// "message": `Hi ${user.name}`
99
// });}
1010

11-
function sendnow (tabs) {
12-
browser.tabs.sendMessage(tabs[0].id, {
13-
command: "token",
14-
token: token
15-
});
16-
}
17-
18-
19-
async function sendToken (data) {
20-
console.log(data.url)
21-
token = await validate(data.url)
22-
23-
console.log("FINAL "+token)
24-
25-
browser.tabs.query({active: true, currentWindow: true},
26-
function (tabs) {
27-
browser.tabs.sendMessage(tabs[0].id, {
28-
command: "token",
29-
token: token
30-
});
31-
})
32-
33-
.catch(logError);
34-
35-
}
3611

3712
function logError(error) {
3813
console.error(`Error: ${error}`);
3914
}
4015

41-
/**
42-
When the button's clicked:
43-
- get an access token using the identity API
44-
- use it to get the user's info
45-
- show a notification containing some of it
46-
*/
47-
browser.runtime.onMessage.addListener(async function (message,sender,res){
48-
console.log(message.url)
49-
token = await validate(message.url)
50-
51-
console.log("FINAL "+token)
16+
browser.runtime.onMessage.addListener(recivemessage)
17+
18+
function recivemessage (message,sender,sendResponse){
19+
if(message.command === "tokenurl"){
20+
console.log(message.data)
21+
token = validate(message.data).catch(logError).then((val)=>{
22+
console.log("val "+val)
23+
sendResponse({response: val})
24+
25+
})
26+
27+
28+
return true
29+
30+
}
5231

53-
browser.tabs.query({active: true, currentWindow: true},
54-
function (tabs) {
55-
browser.tabs.sendMessage(tabs[0].id, {
56-
command: "token",
57-
token: token
58-
});
59-
})
60-
61-
.catch(logError);
62-
63-
});
32+
}
6433

6534

6635

icons/License

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>Icons made by <a href="https://www.flaticon.com/authors/pixel-perfect" title="Pixel perfect">Pixel perfect</a> from <a href="https://www.flaticon.com/" title="Flaticon">www.flaticon.com</a> is licensed by <a href="http://creativecommons.org/licenses/by/3.0/" title="Creative Commons BY 3.0" target="_blank">CC 3.0 BY</a></div>

popup/stackpopup.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@ const AUTH_URL =
1010
//&scope=${encodeURIComponent(SCOPES.join(' '))}`;
1111

1212

13-
browser.runtime.onMessage.addListener((message) => {
14-
console.log(message + " mess")
15-
if (message.command === "token") {
16-
tokenSet(message.token);
17-
}
18-
});
13+
function handleResponse(message) {
14+
console.log(`background script sent a response: ${message.response}`);
15+
}
16+
17+
function handleError(error) {
18+
console.log(`Error: ${error}`);
19+
}
20+
1921

2022
document.getElementById('tokenbtn').addEventListener("click", async function getToken () {
2123

2224
var tokenurl = await browser.identity.launchWebAuthFlow({
2325
interactive: true,
2426
url: AUTH_URL
2527
})
26-
browser.runtime.sendMessage({url: tokenurl});
28+
var token = browser.runtime.sendMessage({
29+
command: "tokenurl" ,
30+
data: tokenurl })
31+
token.then(handleResponse,handleError)
32+
2733
})
2834

2935
function tokenSet (req) {

0 commit comments

Comments
 (0)