Skip to content

Commit

Permalink
add the missing auth status and enhance web auth example
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 22, 2024
1 parent 9a89b92 commit 58f355b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion authHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func authFlow(ctx context.Context, client *auth.Client, conversator AuthConversa
if errors.As(signInErr, &signUpRequired) {
return f.handleSignUp(ctx, client, phone, hash, signUpRequired)
}

if signInErr != nil {
SendAuthStatus(conversator, AuthStatusPhoneCodeFailed)
return errors.Wrap(signInErr, "sign in")
}
SendAuthStatus(conversator, AuthStatusSuccess)
case *tg.AuthSentCodeSuccess:
switch a := s.Authorization.(type) {
case *tg.AuthAuthorization:
Expand Down
25 changes: 18 additions & 7 deletions examples/auth-using-api-base/web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package web
import (
"fmt"
"net/http"

"github.com/celestix/gotgproto"
)

// Start a web server and wait
Expand All @@ -13,7 +15,7 @@ func Start() {
}

func getAuthStatus(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, authStatus)
fmt.Fprint(w, authStatus.Event)
}

// setInfo handle user info, set phone, code or passwd
Expand All @@ -23,20 +25,29 @@ func setInfo(w http.ResponseWriter, req *http.Request) {
switch action {

case "phone":
fmt.Println("Rec phone")
num := req.URL.Query().Get("phone")
phone := "+" + num
ReceivePhone(phone)
fmt.Fprintf(w, "phone received: %s", phone)

for authStatus.Event == gotgproto.AuthStatusPhoneAsked ||
authStatus.Event == gotgproto.AuthStatusPhoneRetrial {
continue
}
case "code":
fmt.Println("Rec code")
code := req.URL.Query().Get("code")
ReceiveCode(code)
fmt.Fprintf(w, "code received: %s", code)

for authStatus.Event == gotgproto.AuthStatusPhoneCodeAsked ||
authStatus.Event == gotgproto.AuthStatusPhoneCodeRetrial {
continue
}
case "passwd":
passwd := req.URL.Query().Get("passwd")
ReceivePasswd(passwd)
fmt.Fprintf(w, "passwd received: %s", passwd)

for authStatus.Event == gotgproto.AuthStatusPasswordAsked ||
authStatus.Event == gotgproto.AuthStatusPasswordRetrial {
continue
}
}
w.Write([]byte(authStatus.Event))
}

0 comments on commit 58f355b

Please sign in to comment.