Categorygithub.com/dukex/go-auth
modulepackage
1.0.0
Repository: https://github.com/dukex/go-auth.git
Documentation: pkg.go.dev

# README

Login2

Easy way to sign in and sign up users using oauth and email/password

$ go get gopkg.in/dukex/login2.v1
import "gopkg.in/dukex/login2.v1"
var loginBuilder *login2.Builder
loginBuilder = login2.NewBuilder()

Config

To config your oauth provider use NewProvider func

provider := &login2.Provider{
  RedirectURL: os.Getenv("GOOGLE_CALLBACK_URL"),
  AuthURL:     "https://accounts.google.com/o/oauth2/auth",
  TokenURL:    "https://accounts.google.com/o/oauth2/token",
  Name:        "google",
  Key:         os.Getenv("GOOGLE_CLIENT_ID"),
  Secret:      os.Getenv("GOOGLE_CLIENT_SECRET"),
  Scope:       "https://www.googleapis.com/auth/userinfo.email",
  UserInfoURL: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
}

loginBuilder.NewProvider(provider)

The func NewProviders accept a Provider array

providers := make([]*login2.Provider, 0)

providers = append(providers, &auth.Provider{
  RedirectURL: os.Getenv("GOOGLE_CALLBACK_URL"),
  AuthURL:     "https://accounts.google.com/o/oauth2/auth",
  TokenURL:    "https://accounts.google.com/o/oauth2/token",
  Name:        "google",
  Key:         os.Getenv("GOOGLE_CLIENT_ID"),
  Secret:      os.Getenv("GOOGLE_CLIENT_SECRET"),
  Scope:       "https://www.googleapis.com/auth/userinfo.email",
  UserInfoURL: "https://www.googleapis.com/oauth2/v1/userinfo?alt=json",
})

loginBuilder.NewProviders(providers)

Login2 works with callback to be a agnostic way to sign in and sign up users, login2.Builder accept 4 callbacks

loginBuilder.UserSetupFn = func(provider string, user *auth.User, rawResponde *http.Response) (int64, error)  {
}

loginBuilder.UserCreateFn = func(email string, password string, request *http.Request) (int64, error) {
}

loginBuilder.UserIdByEmail = func(email string) (int64, error) {
}

loginBuilder.UserPasswordByEmail = func(email string) (string, error) {
}

loginBuilder.UserResetPasswordFn = func(token string, email string) {
}

To http handlers works you need config your URLs, login2 has URL type:

type URLS struct {
  Redirect                string
  SignIn                  string
  SignUp                  string
  ResetPasswordSuccess    string
}

And Builder has URLS field

loginBuilder.URLS = login2.URLS{
  Redirect: "/dashbaord",
  SignIn:    "/login",
  SignUp:  "/register",
  ResetPasswordSuccess: "/reset_password_success"
}

After your sign or sign up login2 will send user to Redirect url.

When login2 need sign in user, e.g User trying access protected path, login2 will send user to SignIn url.

When login2 need send up user, login2 will send user to SignUp url.

TODO: ResetPasswordSuccess

See Doc

# Functions

No description provided by the author
No description provided by the author

# Structs

Builder is the app configuration, store providers and callbacks Follow the callbacks documentation: UserSetupFn func(provider string, user *login2.User, rawResponse *http.Response) (int64, error) Called when user return from oauth provider, this method will send a provider origin as string, some user information as ```login2.User``` and the raw response from origin(login2 will make a request to ``` UserInfoURL``` configured on provider config).
Provider is a oauth2 provider, like facebook or google Name is provider name, it's like a key, will can be use it after, the package only use it as a index.
No description provided by the author
No description provided by the author