Git Product home page Git Product logo

pocketbase-plugin-telegram-auth's Introduction

Test codecov

Overview

This plugin implements Telegram WebApp Auth and Telegram Login Widget for the pocketbase

Requirements

Installation

go get github.com/iamelevich/pocketbase-plugin-telegram-auth

Autofill fields

Next fields will be filled from telegram in auth collection record if exists:

  • name - string
  • first_name - string
  • last_name - string
  • telegram_username - string
  • telegram_id - string
  • language_code - string

Example

You 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.

Usage

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);
});

pocketbase_plugin_telegram_auth

import "github.com/iamelevich/pocketbase-plugin-telegram-auth"

Index

type Options

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

type Plugin struct {
    // contains filtered or unexported fields
}

func MustRegister

func MustRegister(app core.App, options *Options) *Plugin

MustRegister is a helper function to register plugin and panic if error occurred.

func Register

func Register(app core.App, options *Options) (*Plugin, error)

Register plugin in PocketBase app.

func (*Plugin) AuthByTelegramData

func (p *Plugin) AuthByTelegramData(tgData forms.TelegramData) (*models.Record, *auth.AuthUser, error)

AuthByTelegramData returns auth record and auth user by Telegram data.

func (*Plugin) GetCollection

func (p *Plugin) GetCollection() (*models.Collection, error)

GetCollection returns PocketBase collection object for collection with name or id from options.CollectionKey.

func (*Plugin) GetForm

func (p *Plugin) GetForm(optAuthRecord *models.Record) (*forms.RecordTelegramLogin, error)

GetForm returns Telegram login form for collection with name or id from options.CollectionKey.

func (*Plugin) Validate

func (p *Plugin) Validate() error

Validate plugin options. Return error if some option is invalid.

Generated by gomarkdoc

Contributing

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.

Process

  • Fork the repo
  • Create a new branch
  • Make your changes
  • Create a pull request
  • Wait for review
  • Make changes if needed
  • Merge
  • Celebrate :)

Development setup

  • Install asdf and plugins for tools listed in .tool-versions file.
  • This repo also uses asdf-direnv. Install it and run direnv allow in the repo root.
  • Setup pre-commit hooks with pre-commit install -t commit-msg -t pre-commit

Testing

  • Run task test to run tests
  • Run task test:report to run tests and get coverage report in ./coverage.html

Writing tests

Linting

  • Run task lint to run linters

Docs update in README

  • Run task docs to update docs in README (it will also install gomarkdoc)

pocketbase-plugin-telegram-auth's People

Contributors

dependabot[bot] avatar github-actions[bot] avatar iamelevich avatar mergify[bot] avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pocketbase-plugin-telegram-auth's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

asdf
.tool-versions
  • golang 1.22.5
  • golangci-lint 1.59.1
  • pre-commit 3.7.1
github-actions
.github/workflows/commitlint.yml
  • actions/checkout v4
  • wagoid/commitlint-github-action v6
.github/workflows/release-please.yml
  • googleapis/release-please-action v4
.github/workflows/test.yml
  • actions/checkout v4
  • magnetikonline/action-golang-cache v5
  • codecov/codecov-action v4
gomod
go.mod
  • go 1.22
  • github.com/go-ozzo/ozzo-validation/v4 v4.3.0
  • github.com/labstack/echo/v5 v5.0.0-20230722203903-ec5b858dab61@ec5b858dab61
  • github.com/pocketbase/dbx v1.10.1
  • github.com/pocketbase/pocketbase v0.22.17

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.