Documentation ¶
Overview ¶
Package login2 provides sign in and sign up by oauth2 and email and password. Inspired in omniauth and devise gem
Index ¶
- func NewUserToken() string
- type Builder
- func (b *Builder) CurrentUser(r *http.Request) (id string, ok bool)
- func (b *Builder) Login(r *http.Request, userId string) *sessions.Session
- func (b *Builder) Logout(r *http.Request) *sessions.Session
- func (b *Builder) NewProvider(p *Provider)
- func (b *Builder) NewProviders(providers []*Provider)
- func (b *Builder) OAuthAuthorize(provider string) func(http.ResponseWriter, *http.Request)
- func (b *Builder) OAuthCallback(provider string, r *http.Request) (int64, error)
- func (b *Builder) OAuthLogin(provider string) func(http.ResponseWriter, *http.Request)
- func (b *Builder) Protected(fn func(string, http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)
- func (b *Builder) ResetPassword() func(http.ResponseWriter, *http.Request)
- func (b *Builder) SetReturnTo(w http.ResponseWriter, r *http.Request, url string)
- func (b *Builder) SignIn() func(http.ResponseWriter, *http.Request)
- func (b *Builder) SignOut() func(http.ResponseWriter, *http.Request)
- func (b *Builder) SignUp() func(http.ResponseWriter, *http.Request)
- type Provider
- type URLS
- type User
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewUserToken ¶
func NewUserToken() string
Types ¶
type Builder ¶
type Builder struct { Providers map[string]*builderConfig UserSetupFn func(provider string, user *User, rawResponde *http.Response) (int64, error) UserCreateFn func(email string, password string, token string, request *http.Request) (int64, error) UserResetPasswordFn func(token string, email string) UserIdByEmail func(email string) (int64, error) // TODO: use bool as second params UserIdByToken func(token string) (int64, bool) UserPasswordByEmail func(email string) (string, bool) LoginFn func(userId string) URLS URLS }
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). To sign in user the method expect the user id as int64
UserCreateFn func(email string, password string, token string, request *http.Request) (int64, error)
Called when user sign up by email/password, the method will send email and password as string, password // is encrypted hash, and expect the user id as int64
UserIdByEmail func(email string) (int64, error)
Called when user sign in by email/password to get the user id by email after check the password with ```UserPasswordByEmail```, the method will send the user email as string and expect the user id as int64
UserPasswordByEmail func(email string) (string, bool)
Called when user sign in by email/password to get user password and check with inputed password, the method will send user email as string and expect the user password as string
UserResetPasswordFn func(token string, email string)
TODO
func NewBuilder ¶
func NewBuilder() *Builder
func (*Builder) CurrentUser ¶
CurrentUser func expect you send the request(```http.Request```) and return the user id as string and bool true if is OK
func (*Builder) NewProvider ¶
func (*Builder) NewProviders ¶
func (*Builder) OAuthAuthorize ¶
OAuthAuthorize To authorize user on defined provider. Send provider name as params and method will return http handle
``` GET /auth/google loginBuilder.OAuthAuthorize("google") GET /auth/facebook loginBuilder.OAuthAuthorize("facebook") ```
func (*Builder) OAuthCallback ¶
OAuthCallback receive code params from provider and get user information
func (*Builder) OAuthLogin ¶
OAuthLogin The oauth endpoint callback, configured on provider, Send provider name as params and method will return http handle
``` GET /auth/callback/google loginBuilder.OAuthLogin("google") GET /auth/callback/facebook loginBuilder.OAuthLogin("facebook") ```
func (*Builder) Protected ¶
func (b *Builder) Protected(fn func(string, http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request)
Protected to be used on protected path, send the original http handle as params and if user is logged Protected will pass user to original handler else Protected will save URL and send user to Sign In. Protected send as first params the user id.
``` GET /dashboard Protected(DashboardHandle) ```
func (*Builder) ResetPassword ¶
func (b *Builder) ResetPassword() func(http.ResponseWriter, *http.Request)
func (*Builder) SetReturnTo ¶
func (*Builder) SignIn ¶
func (b *Builder) SignIn() func(http.ResponseWriter, *http.Request)
SignIn Handler to sign in user, send a http POST with email and password params on body
``` POST /users/sign_in SignIn ```
type Provider ¶
type Provider struct { Name string Key string Secret string RedirectURL string TokenURL string AuthURL string UserInfoURL string Scope string }
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. Key is oauth2 key Secret is oauth2 secret key RedirectURL is a url will config on provider TokenURL is a URL to get the token on provider AuthURL is a URL to auth user on provider UserInfoURL is a URL to get User Information on provider Scope is whats the scope your app wants