This plugin implements Telegram WebApp Auth and Telegram Login Widget for the pocketbase
go get github.com/iamelevich/pocketbase-plugin-telegram-auth
Next fields will be filled from telegram in auth collection record if exists:
name - stringfirst_name - stringlast_name - stringtelegram_username - stringtelegram_id - stringlanguage_code - stringYou can check examples in examples folder
package main
import (
tgAuthPlugin "github.com/iamelevich/pocketbase-plugin-telegram-auth"
"log"
"github.com/pocketbase/pocketbase"
)
func main() {
app := pocketbase.New()
// Setup tg auth for users collection
tgAuthPlugin.MustRegister(app, &tgAuthPlugin.Options{
BotToken: "YOUR_SUPER_SECRET_BOT_TOKEN", // Better to use ENV variable for that
CollectionKey: "users",
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
After that new route POST /api/collections/users/auth-with-telegram will be available.
Simple usage with js. You can check react example here
const pb = new PocketBase('http://127.0.0.1:8090');
pb.send('/api/collections/users/auth-with-telegram', {
method: 'POST',
body: {
data: window.Telegram.WebApp.initData
}
}).then(res => {
pb.authStore.save(res.token, res.record);
});
import "github.com/iamelevich/pocketbase-plugin-telegram-auth"
Options defines optional struct to customize the default plugin behavior.
type Options struct {
// BotToken is a Telegram bot token.
// You can get it from @BotFather.
BotToken string
// CollectionKey is a collection key (name or id) for PocketBase auth collection.
CollectionKey string
}
type Plugin struct {
// contains filtered or unexported fields
}
func MustRegister(app core.App, options *Options) *Plugin
MustRegister is a helper function to register plugin and panic if error occurred.
func Register(app core.App, options *Options) (*Plugin, error)
Register plugin in PocketBase app.
func (p *Plugin) AuthByTelegramData(tgData forms.TelegramData) (*core.Record, *auth.AuthUser, error)
AuthByTelegramData returns auth record and auth user by Telegram data.
func (p *Plugin) GetCollection() (*core.Collection, error)
GetCollection returns PocketBase collection object for collection with name or id from options.CollectionKey.
func (p *Plugin) GetForm(optAuthRecord *core.Record) (*forms.RecordTelegramLogin, error)
GetForm returns Telegram login form for collection with name or id from options.CollectionKey.
func (p *Plugin) Validate() error
Validate plugin options. Return error if some option is invalid.
Generated by gomarkdoc
This pocketbase plugin is free and open source project licensed under the MIT License. You are free to do whatever you want with it, even offering it as a paid service.
mise install.prek hooks with prek install -t commit-msg -t pre-commitmise run test to run testsmise run test-report to run tests and get coverage report in ./coverage.htmlmise run run-test-server
test@test.testtestpassword./test/test_pb_datamise run lint to run lintersmise run docs to update docs in README (it will also install gomarkdoc)Go
100.0%
This plugin implements Telegram WebApp Auth and Telegram Login Widget for the pocketbase
go get github.com/iamelevich/pocketbase-plugin-telegram-auth
Next fields will be filled from telegram in auth collection record if exists:
name - stringfirst_name - stringlast_name - stringtelegram_username - stringtelegram_id - stringlanguage_code - stringYou can check examples in examples folder
package main
import (
tgAuthPlugin "github.com/iamelevich/pocketbase-plugin-telegram-auth"
"log"
"github.com/pocketbase/pocketbase"
)
func main() {
app := pocketbase.New()
// Setup tg auth for users collection
tgAuthPlugin.MustRegister(app, &tgAuthPlugin.Options{
BotToken: "YOUR_SUPER_SECRET_BOT_TOKEN", // Better to use ENV variable for that
CollectionKey: "users",
})
if err := app.Start(); err != nil {
log.Fatal(err)
}
}
After that new route POST /api/collections/users/auth-with-telegram will be available.
Simple usage with js. You can check react example here
const pb = new PocketBase('http://127.0.0.1:8090');
pb.send('/api/collections/users/auth-with-telegram', {
method: 'POST',
body: {
data: window.Telegram.WebApp.initData
}
}).then(res => {
pb.authStore.save(res.token, res.record);
});
import "github.com/iamelevich/pocketbase-plugin-telegram-auth"
Options defines optional struct to customize the default plugin behavior.
type Options struct {
// BotToken is a Telegram bot token.
// You can get it from @BotFather.
BotToken string
// CollectionKey is a collection key (name or id) for PocketBase auth collection.
CollectionKey string
}
type Plugin struct {
// contains filtered or unexported fields
}
func MustRegister(app core.App, options *Options) *Plugin
MustRegister is a helper function to register plugin and panic if error occurred.
func Register(app core.App, options *Options) (*Plugin, error)
Register plugin in PocketBase app.
func (p *Plugin) AuthByTelegramData(tgData forms.TelegramData) (*core.Record, *auth.AuthUser, error)
AuthByTelegramData returns auth record and auth user by Telegram data.
func (p *Plugin) GetCollection() (*core.Collection, error)
GetCollection returns PocketBase collection object for collection with name or id from options.CollectionKey.
func (p *Plugin) GetForm(optAuthRecord *core.Record) (*forms.RecordTelegramLogin, error)
GetForm returns Telegram login form for collection with name or id from options.CollectionKey.
func (p *Plugin) Validate() error
Validate plugin options. Return error if some option is invalid.
Generated by gomarkdoc
This pocketbase plugin is free and open source project licensed under the MIT License. You are free to do whatever you want with it, even offering it as a paid service.
mise install.prek hooks with prek install -t commit-msg -t pre-commitmise run test to run testsmise run test-report to run tests and get coverage report in ./coverage.htmlmise run run-test-server
test@test.testtestpassword./test/test_pb_datamise run lint to run lintersmise run docs to update docs in README (it will also install gomarkdoc)Go
100.0%