mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-11-02 15:15:51 +00:00
feat: google oauth
This commit is contained in:
39
internal/providers/google.go
Normal file
39
internal/providers/google.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package providers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GoogleUserinfoResponse struct {
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
func GoogleScopes() []string {
|
||||
return []string{"https://www.googleapis.com/auth/userinfo.email"}
|
||||
}
|
||||
|
||||
func GetGoogleEmail(client *http.Client) (string, error) {
|
||||
res, resErr := client.Get("https://www.googleapis.com/userinfo/v2/me")
|
||||
|
||||
if resErr != nil {
|
||||
return "", resErr
|
||||
}
|
||||
|
||||
body, bodyErr := io.ReadAll(res.Body)
|
||||
|
||||
if bodyErr != nil {
|
||||
return "", bodyErr
|
||||
}
|
||||
|
||||
var user GoogleUserinfoResponse
|
||||
|
||||
jsonErr := json.Unmarshal(body, &user)
|
||||
|
||||
if jsonErr != nil {
|
||||
return "", jsonErr
|
||||
}
|
||||
|
||||
return user.Email, nil
|
||||
}
|
||||
Reference in New Issue
Block a user