Skip to content

Commit 76497cf

Browse files
committed
Fixed login callback issue
1 parent 53e4225 commit 76497cf

5 files changed

Lines changed: 14 additions & 9 deletions

File tree

src/app.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { User } from '$lib/types/user';
12
/// <reference types="@sveltejs/kit" />
23

34
// See https://kit.svelte.dev/docs/types#app
@@ -8,3 +9,10 @@ declare namespace App {
89
// interface Session {}
910
// interface Stuff {}
1011
}
12+
13+
declare global {
14+
interface Window {
15+
// eslint-disable-next-line no-unused-vars
16+
telegramCallback: (data: User) => void;
17+
}
18+
}

src/lib/Login.svelte

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* } | {
1515
* authType?: 'callback'; // Authorization type, Callback means that you will get an event containing user info when the user is logged in, you'll need to use onauth to handle it. Redirect means that user will be redirected to the URL you specify in `redirectURL` parameter.
1616
* redirectURL?: never; // If authType is set to redirect, this is the URL that user will be redirected to after logging in.
17-
* onauth: (user: import('../lib/types/user').user) => void;
17+
* onauth: (user: import('../lib/types/user').User) => void;
1818
* })}
1919
*/
2020
let {
@@ -32,7 +32,7 @@
3232
/** @type {HTMLScriptElement | undefined}*/
3333
let script = $state();
3434
35-
function telegramCallback(/** @type {import('../lib/types/user').user}*/ user) {
35+
function telegramCallback(/** @type {import('../lib/types/user').User}*/ user) {
3636
onauth(user);
3737
}
3838
function cleanStart() {
@@ -64,11 +64,8 @@
6464
}
6565
6666
onMount(() => {
67+
window.telegramCallback = telegramCallback;
6768
cleanStart();
68-
if ('telegramCallback' in window) {
69-
window.telegramCallback = telegramCallback;
70-
console.log('telegramCallback is already defined');
71-
}
7269
});
7370
</script>
7471

src/lib/types/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @typedef {Object} user
2+
* @typedef {Object} User
33
* @property {number} id
44
* @property {string} first_name
55
* @property {string} username

src/routes/+page.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
/** @type {string}*/
3838
let loginUsername = 'ComputlyBot';
3939
40-
async function telegramLogin(/** @type {import('../lib/types/user').user}*/ user) {
40+
async function telegramLogin(/** @type {import('$lib/types/user').User}*/ user) {
4141
const res = await fetch(window.location.origin + '/bot', {
4242
method: 'POST',
4343
headers: {

src/routes/bot/+server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { error, json } from '@sveltejs/kit';
33

44
/** @type {import('@sveltejs/kit').RequestHandler} */
55
export async function POST({ request }) {
6-
/** @type {import('../../lib/types/user').user}*/
6+
/** @type {import('$lib/types/user').User}*/
77
const body = await request.json();
88
try {
99
const message = [

0 commit comments

Comments
 (0)