1
+ import { engine } from 'express-handlebars' ;
2
+ import express from 'express' ;
3
+ import path from 'path' ;
4
+ import cors from 'cors' ;
5
+ import { TonLoginServer } from '@tonapps/tonlogin-server' ;
6
+ import { getLocalIPAddress } from './utils' ;
7
+
8
+ // use generateServerSecret();
9
+ const staticSecret = 'C0n0Tm4x4ACU9f0mQNEs0LPYMXIpwkKaRQYQsrc9Hx8=' ;
10
+ const port = 8080 ;
11
+
12
+ function init ( ) {
13
+ const host = getLocalIPAddress ( ) ;
14
+ const hostname = `${ host } :${ port } ` ;
15
+ const app = express ( ) ;
16
+
17
+ app . use ( cors ( ) ) ;
18
+ app . engine ( "handlebars" , engine ( ) ) ;
19
+ app . set ( "view engine" , "handlebars" ) ;
20
+ app . set ( "views" , path . resolve ( __dirname , "./views" ) ) ;
21
+
22
+ const tonlogin = new TonLoginServer ( { staticSecret } ) ;
23
+
24
+ app . get ( '/authRequest' , ( req , res ) => {
25
+ const request = tonlogin . generateAuthRequest ( {
26
+ image_url : 'https://ddejfvww7sqtk.cloudfront.net/images/landing/ton-nft-tegro-dog/avatar/image_d0315e1461.jpg' ,
27
+ return_url : `${ hostname } /tonlogin` ,
28
+ items : [ {
29
+ type : 'ton-address' ,
30
+ require : true
31
+ } ] ,
32
+ } ) ;
33
+
34
+ res . send ( request ) ;
35
+ } ) ;
36
+
37
+ app . get ( '/tonlogin' , ( req , res ) => {
38
+ try {
39
+ const encodedResponse = req . query . tonlogin as string ;
40
+ const decodedResponse = tonlogin . decodeAuthResponse ( encodedResponse ) ;
41
+
42
+ console . log ( decodedResponse . client_id , decodedResponse . payload ) ;
43
+
44
+ res . send ( decodedResponse ) ;
45
+ } catch ( err ) {
46
+ console . log ( err ) ;
47
+ res . status ( 400 ) . send ( { error : true } ) ;
48
+ }
49
+ } ) ;
50
+
51
+ app . get ( '/' , ( req , res ) => {
52
+ res . render ( 'home' , {
53
+ layout : false ,
54
+ requestEndpoint : `${ hostname } /authRequest`
55
+ } ) ;
56
+ } ) ;
57
+
58
+ app . listen ( port , host , ( ) => {
59
+ console . log ( `Server running at http://${ hostname } /` ) ;
60
+ } ) ;
61
+ }
62
+
63
+ init ( ) ;
0 commit comments