-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvkauthtest.html
90 lines (62 loc) · 1.98 KB
/
vkauthtest.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VK auth test</title>
<meta name="description" content="">
<meta name="author" content="Sergei Sokolov">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<button id="btn-login">Войти через ВК</button>
<script>
document.getElementById('btn-login').addEventListener('click', () => {
getVKLoginPromise (4096 /* messages */, 6721365)
.then (r => {
console.log("Got new token:" + r.access_token);
document.body.appendChild(document.createTextNode("OK! Got new token"));
// Save to LS and procede from here
})
.catch(err => {
console.error("VK login failed", err);
document.body.appendChild(document.createTextNode("FAIL. Some error occured or user declined access"));
});
});
function getVKLoginPromise(scope, client_id) {
return new Promise((resolve, reject) => {
const params = {
client_id : client_id,
redirect_uri : 'https://deserter.io/messages.save/verify.html',
display : 'popup',
scope : scope,
response_type: 'token',
v : '5.85',
state : 12345,
revoke : 1,
};
var queryString = Object.keys(params)
.map(key => encodeURIComponent(key) + '=' + encodeURIComponent(params[key]))
.join('&')
;
window.open(
'https://oauth.vk.com/authorize?' + queryString,
'VK login',
'menubar=0,toolbar=0,status=0,width=650,height=400'
);
// get feedback
function onMessage(m) {
console.log("Message received:", m);
window.removeEventListener('message', onMessage);
const data = m.data;
if (data.access_token) {
resolve(data);
} else if (data.error) {
reject(data);
}
}
window.addEventListener('message', onMessage);
});
};
</script>
</body>
</html>