mirror of
https://github.com/rjNemo/rentease.git
synced 2026-06-06 02:36:49 +00:00
add google login
This commit is contained in:
parent
8f4f1638a4
commit
0924417a81
1 changed files with 48 additions and 0 deletions
48
internal/auth/service.go
Normal file
48
internal/auth/service.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package auth
|
||||
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/gorilla/sessions"
|
||||
"github.com/markbates/goth"
|
||||
"github.com/markbates/goth/gothic"
|
||||
"github.com/markbates/goth/providers/google"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
secret string
|
||||
googleClientId string
|
||||
googleSecret string
|
||||
googleRedirectUrl string
|
||||
}
|
||||
|
||||
type ProviderIndex struct {
|
||||
ProvidersMap map[string]string
|
||||
Providers []string
|
||||
}
|
||||
|
||||
func NewService(secret, googleClientId, googleSecret, googleRedirectUrl string) *Service {
|
||||
return &Service{
|
||||
secret,
|
||||
googleClientId,
|
||||
googleSecret,
|
||||
googleRedirectUrl,
|
||||
}
|
||||
}
|
||||
|
||||
func (as Service) GetProviderIndex() *ProviderIndex {
|
||||
goth.UseProviders(google.New(as.googleClientId, as.googleSecret, as.googleRedirectUrl))
|
||||
|
||||
m := map[string]string{
|
||||
"google": "Google",
|
||||
}
|
||||
var keys []string
|
||||
for k := range m {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
gothic.Store = sessions.NewCookieStore([]byte(as.secret))
|
||||
|
||||
return &ProviderIndex{Providers: keys, ProvidersMap: m}
|
||||
}
|
||||
Loading…
Reference in a new issue