Skip to content

Commit

Permalink
Add react example (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamelevich authored Feb 6, 2023
1 parent 2133247 commit 741fe79
Show file tree
Hide file tree
Showing 20 changed files with 3,728 additions and 2 deletions.
8 changes: 7 additions & 1 deletion .idea/pocketbase-plugin-telegram-auth.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,18 @@ After that new route `POST /api/collections/users/auth-with-telegram` will be av

### Usage

TODO
Simple usage with js. You can check react example [here](./examples/webapp-react)
```js
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);
});
```

## Contributing

Expand Down
1 change: 1 addition & 0 deletions examples/webapp-react/.envrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
use asdf;
7 changes: 7 additions & 0 deletions examples/webapp-react/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ignore everything
/*

# exclude from the ignore filter
!.gitignore
!tg_webapp/
!main.go
1 change: 1 addition & 0 deletions examples/webapp-react/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nodejs 18.13.0
32 changes: 32 additions & 0 deletions examples/webapp-react/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
tgAuthPlugin "github.com/iamelevich/pocketbase-plugin-telegram-auth"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"log"
"os"

"github.com/pocketbase/pocketbase"
)

func main() {
app := pocketbase.New()

// Setup tg auth for users collection
tgAuthPlugin.MustRegister(app, &tgAuthPlugin.Options{
BotToken: "6086153355:AAHQR0dRvmboEIfbsPT93GHczOnT355gmUE", // Better to use ENV variable for that
CollectionKey: "users",
})

// Setup serving bundled react app
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// serves static files from the provided public dir (if exists)
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./tg_webapp/dist"), true))
return nil
})

if err := app.Start(); err != nil {
log.Fatal(err)
}
}
24 changes: 24 additions & 0 deletions examples/webapp-react/tg_webapp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
15 changes: 15 additions & 0 deletions examples/webapp-react/tg_webapp/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>

<script src="https://telegram.org/js/telegram-web-app.js"></script>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
Loading

0 comments on commit 741fe79

Please sign in to comment.