DmitrMakeev commited on
Commit
bdd6051
·
verified ·
1 Parent(s): 0ad03c6

Update user.html

Browse files
Files changed (1) hide show
  1. user.html +83 -41
user.html CHANGED
@@ -17,54 +17,96 @@
17
  align-items: center;
18
  height: 100vh;
19
  }
20
-
 
 
 
 
21
  </style>
22
  </head>
23
  <body>
24
- <div>
25
- <script src="https://unpkg.com/@vkid/sdk@<3.0.0/dist-sdk/umd/index.js"></script>
26
- <script type="text/javascript">
27
- if ("VKIDSDK" in window) {
28
- const VKID = window.VKIDSDK;
29
 
30
- VKID.Config.init({
31
- "app": 52295022,
32
- "redirectUrl": "https://dmtuit-psy-vk.hf.space/pages",
33
- "source": VKID.ConfigSource.LOWCODE,
34
- });
35
 
36
- const oneTap = new VKID.OneTap();
37
 
38
- oneTap.render({
39
- "container": document.currentScript.parentElement,
40
- "showAlternativeLogin": true,
41
- "styles": {
42
- "width": 360
43
- },
44
- "oauthList": [
45
- "ok_ru",
46
- "mail_ru"
47
- ]
48
- })
49
- .on(VKID.WidgetEvents.ERROR, vkidOnError)
50
- .on(VKID.OneTapInternalEvents.LOGIN_SUCCESS, function (payload) {
51
- const code = payload.code;
52
- const deviceId = payload.device_id;
53
 
54
- VKID.Auth.exchangeCode(code, deviceId)
55
- .then(vkidOnSuccess)
56
- .catch(vkidOnError);
57
- });
58
- }
 
 
 
 
 
 
 
 
59
 
60
- function vkidOnSuccess(data) {
61
- // Обработка полученного результата
62
- }
63
-
64
- function vkidOnError(error) {
65
- // Обработка ошибки
66
- }
67
- </script>
68
- </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  </body>
70
  </html>
 
17
  align-items: center;
18
  height: 100vh;
19
  }
20
+ .vkid-container {
21
+ width: 360px;
22
+ height: 56px;
23
+ margin: auto;
24
+ }
25
  </style>
26
  </head>
27
  <body>
28
+ <div class="vkid-container">
29
+ <script src="https://unpkg.com/@vkid/sdk@<3.0.0/dist-sdk/umd/index.js"></script>
30
+ <script type="text/javascript">
31
+ if ("VKIDSDK" in window) {
32
+ const VKID = window.VKIDSDK;
33
 
34
+ VKID.Config.init({
35
+ "app": 52295022,
36
+ "redirectUrl": "https://diamonik7777-up-fail.hf.space/pages",
37
+ "source": VKID.ConfigSource.LOWCODE,
38
+ });
39
 
40
+ const oneTap = new VKID.OneTap();
41
 
42
+ oneTap.render({
43
+ "container": document.currentScript.parentElement,
44
+ "showAlternativeLogin": true,
45
+ "styles": {
46
+ "width": 360
47
+ },
48
+ "oauthList": [
49
+ "ok_ru",
50
+ "mail_ru"
51
+ ]
52
+ })
53
+ .on(VKID.WidgetEvents.ERROR, vkidOnError)
54
+ .on(VKID.OneTapInternalEvents.LOGIN_SUCCESS, function (payload) {
55
+ const code = payload.code;
56
+ const deviceId = payload.device_id;
57
 
58
+ VKID.Auth.exchangeCode(code, deviceId)
59
+ .then(vkidOnSuccess)
60
+ .catch(vkidOnError);
61
+ });
62
+ }
63
+
64
+ function vkidOnSuccess(data) {
65
+ const accessToken = data.access_token;
66
+ localStorage.setItem('vkidAccessToken', accessToken);
67
+ console.log('Access Token:', accessToken);
68
+ // Используем токен для выполнения запросов к API ВКонтакте
69
+ getVkUserInfo(accessToken);
70
+ }
71
 
72
+ function vkidOnError(error) {
73
+ console.error('VKID Error:', error);
74
+ }
75
+
76
+ function getVkUserInfo(token) {
77
+ const url = `https://api.vk.com/method/users.get?v=5.131&access_token=${token}`;
78
+ fetch(url)
79
+ .then(response => response.json())
80
+ .then(data => {
81
+ if (data.response && data.response.length > 0) {
82
+ const userId = data.response[0].id;
83
+ console.log('User ID:', userId);
84
+ // Используем полученный ID пользователя для дальнейших действий
85
+ } else {
86
+ console.error('Failed to get user info:', data);
87
+ }
88
+ })
89
+ .catch(error => {
90
+ console.error('Error:', error);
91
+ });
92
+ }
93
+
94
+ // Открытие окна авторизации в той же вкладке
95
+ function openAuthWindow() {
96
+ const authUrl = VKID.Auth.getAuthUrl();
97
+ window.open(authUrl, '_self');
98
+ }
99
+
100
+ // Проверка авторизации при загрузке страницы
101
+ window.onload = function() {
102
+ const accessToken = localStorage.getItem('vkidAccessToken');
103
+ if (!accessToken) {
104
+ openAuthWindow();
105
+ } else {
106
+ getVkUserInfo(accessToken);
107
+ }
108
+ };
109
+ </script>
110
+ </div>
111
  </body>
112
  </html>