Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
feat: add method to login without sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
mainawycliffe committed Dec 5, 2019
1 parent 557c487 commit 2ae8725
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions oauth/oauth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package oauth

import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
Expand Down Expand Up @@ -144,3 +145,25 @@ func LoginWithLocalhost() {
log.Fatal(err)
}
}

// LoginWithoutLocalhost login without localhost localhost server, suitable in
// environments without a GUI. The user enters the authorization code manually
func LoginWithoutLocalhost() {
googleOauthConfig := getGoogleOAuthConfig("")
oauthStateTracker := generateOauthStateTracker()
u := googleOauthConfig.AuthCodeURL(oauthStateTracker)
fmt.Printf("Visit this URL on any device to log in:\n\n%s\n\nWaiting for authentication...", u)
// open browser now
_ = browser.OpenURL(u)
reader := bufio.NewReader(os.Stdin)
fmt.Print("\n\nEnter the authorization code here: ")
code, _ := reader.ReadString('\n')
data, err := getUserDataFromGoogle(googleOauthConfig, code)
if err != nil {
log.Fatalln(err.Error())
return
}
// todo: save credentials
// todo: return success message to the user
fmt.Fprintf(os.Stdout, "\n\nSuccess! Logged in as %s\n\n", data.Email)
}

0 comments on commit 2ae8725

Please sign in to comment.