generated from bot-base/telegram-bot-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add auth with telegram sdk and validate data
- Loading branch information
Showing
12 changed files
with
1,183 additions
and
954 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { findUserById } from "#root/bot/models/user"; | ||
import { config } from "#root/config"; | ||
import { validate } from "@tma.js/init-data-node"; | ||
import { FastifyInstance } from "fastify"; | ||
|
||
const authHandler = ( | ||
fastify: FastifyInstance, | ||
_options: unknown, | ||
done: () => void, | ||
) => { | ||
fastify.post("/:userId", async (request, _reply) => { | ||
const { userId } = request.params as any; | ||
if (!userId) { | ||
return { error: "No userId provided" }; | ||
} | ||
const { initData } = request.body as any; | ||
if (!initData) { | ||
return { error: `No initData or hash provided` }; | ||
} | ||
try { | ||
validate(initData, config.BOT_TOKEN, { expiresIn: 86_400 }); | ||
const user = await findUserById(userId); | ||
if (!user) { | ||
return { error: "User not found" }; | ||
} | ||
return { id: user.id, language: user.language, wallet: user.wallet }; | ||
} catch (error_) { | ||
return { error: error_ }; | ||
} | ||
}); | ||
|
||
done(); | ||
}; | ||
export default authHandler; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ref } from "vue"; | ||
import { authenticateUser } from "../services/auth-service"; | ||
import { unknown } from "zod"; | ||
|
||
export const useAuth = (initData: string, userId: number) => { | ||
const user = ref(); | ||
const error = ref(); | ||
|
||
const login = async () => { | ||
try { | ||
const userString = sessionStorage.getItem("user"); | ||
if (userString) { | ||
const userModel = JSON.parse(userString); | ||
user.value = userModel; | ||
return; | ||
} | ||
const userModel = await authenticateUser(initData, userId); | ||
user.value = userModel; | ||
sessionStorage.setItem("user", JSON.stringify(userModel)); | ||
} catch (error_) { | ||
error.value = error_ | ||
} | ||
}; | ||
|
||
return { | ||
user, | ||
error, | ||
login, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import axios from "axios"; | ||
|
||
export const authenticateUser = async (initData: string, userId: number) => { | ||
try { | ||
const response = await axios.post(`/api/auth/${userId}`, { initData }); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error authenticating user:', error); | ||
throw error; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters