users

package
v0.0.0-...-9431910 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2021 License: MPL-2.0 Imports: 39 Imported by: 0

Documentation

Overview

Package users implements an application for registering and authenticating users, including social sign ins.

Index

Constants

View Source
const (
	JSSignInHandlerName         = "users-js-sign-in"
	JSSignUpHandlerName         = "users-js-sign-up"
	JSSignInFacebookHandlerName = "users-js-sign-in-facebook"
	JSSignInGoogleHandlerName   = "users-js-sign-in-google"

	SignInFacebookHandlerName = "users-sign-in-facebook"
	SignInGoogleHandlerName   = "users-sign-in-google"
	SignInTwitterHandlerName  = "users-sign-in-twitter"
	SignInGithubHandlerName   = "users-sign-in-github"
	SignUpHandlerName         = "users-sign-up"
	SignOutHandlerName        = "users-sign-out"
	ForgotHandlerName         = "users-forgot"
	ResetHandlerName          = "users-reset"

	FacebookChannelHandlerName = "users-facebook-channel"
	ImageHandlerName           = "users-image-handler"

	SignInTemplateName      = "sign-in.html"
	SignInModalTemplateName = "sign-in-modal.html"
	SignUpTemplateName      = "sign-up.html"
	ForgotTemplateName      = "forgot.html"
	ResetTemplateName       = "reset.html"
)

Variables

View Source
var (
	ErrNoPassword      = i18n.NewError("this user has no password")
	ErrInvalidPassword = i18n.NewError("invalid password")
	ErrNoUser          = i18n.NewError("unknown username or email")
)
View Source
var (
	Salt                = []byte("gondola/apps/users")
	PasswordResetExpiry = 24 * time.Hour
	SignInHandlerName   = app.SignInHandlerName

	SignInHandler           = app.Anonymous(signInHandler)
	SignUpHandler           = app.Anonymous(signUpHandler)
	SignOutHandler          = app.SignOutHandler
	ForgotHandler           = app.Anonymous(forgotHandler)
	JSSignInHandler         = app.Anonymous(jsSignInHandler)
	JSSignInFacebookHandler = app.Anonymous(jsSignInFacebookHandler)
	JSSignInGoogleHandler   = app.Anonymous(jsSignInGoogleHandler)
	JSSignUpHandler         = app.Anonymous(jsSignUpHandler)
)
View Source
var (
	ImageHandler app.Handler
	ImageFetcher func(ctx *app.Context, url string) (id string, format string, err error)
)

Functions

func Authenticate

func Authenticate(ctx *app.Context, usernameOrEmail string, password string) (app.User, error)

Authenticate authenticates an user using her username (or email) and password. This is a conveniency function for checking the values received by a custom handler (e.g. http auth). Otherwise, it's more convenient to use the builtin sign in handler in this app, which also takes care of cookies.

func ByEmail

func ByEmail(email string) query.Q

ByEmail returns a query.Q which finds a user given its email.

func ById

func ById(id int64) query.Q

ById returns a query.Q which finds a user given its id.

func ByUsername

func ByUsername(username string) query.Q

ByUsername returns a query.Q which finds a user given its username.

func Create

func Create(ctx *app.Context, username string, email string, pw string) (app.User, error)

Create creates a new user user with the given parameters, previously checking that the given username and email aren't already in use. Note that the user is only created, no implicit sign in is performed.

func Current

func Current(ctx *app.Context) app.User

Current returns the currently authenticated user, if any.

func FacebookChannelHandler

func FacebookChannelHandler(ctx *app.Context)

func FindFreeUsername

func FindFreeUsername(ctx *app.Context, username string) string

func FormErrors

func FormErrors(ctx *app.Context, frm *form.Form)

func Get

func Get(ctx *app.Context, id int64) (app.User, error)

func Image

func Image(ctx *app.Context, user interface{}) (string, error)

func JSONEncode

func JSONEncode(ctx *app.Context, user interface{}) ([]byte, error)

func Normalize

func Normalize(s string) string

func ResetHandler

func ResetHandler(ctx *app.Context)

func SignUpForm

func SignUpForm(ctx *app.Context, user reflect.Value) *form.Form

func UserImageHandler

func UserImageHandler(ctx *app.Context)

Types

type AcceptForm

type AcceptForm struct {
	Accept bool `form:",label=I accept the Terms of Service and the Privacy Policy"`
}

func (*AcceptForm) ValidateAccept

func (f *AcceptForm) ValidateAccept() error

type App

type App struct {
	reusableapp.App
}

func New

func New(opts Options) *App

func (*App) Attach

func (a *App) Attach(parent *app.App)

type Facebook

type Facebook struct {
	Id          string    `form:"-" sql:",unique" json:"id"`
	Username    string    `form:"-" json:"username"`
	Name        string    `form:"-" json:"name"`
	FirstName   string    `form:"-" json:"first_name"`
	LastName    string    `form:"-" json:"last_name"`
	Email       string    `form:"-" json:"email"`
	Image       string    `form:"-" json:"-"`
	ImageFormat string    `form:"-" json:"-"`
	ImageURL    string    `form:"-" json:"-"`
	Token       string    `form:"-" json:"-"`
	Expires     time.Time `form:"-" json:"-"`
}

type Github

type Github struct {
	Id          int64     `form:"-" json:"id" orm:",index,unique"`
	Username    string    `form:"-" json:"username"`
	Name        string    `form:"-" json:"name"`
	Company     string    `form:"-" json:"-"`
	Location    string    `form:"-" json:"-"`
	Email       string    `form:"-" json:"email"`
	Image       string    `form:"-" json:"-"`
	ImageFormat string    `form:"-" json:"-"`
	ImageURL    string    `form:"-" json:"-"`
	Token       string    `form:"-" json:"-"`
	Expires     time.Time `form:"-" json:"-"`
}

type Google

type Google struct {
	Id          string    `form:"-" orm:",unique" json:"id"`
	Email       string    `form:"-" orm:",unique" json:"email"`
	Name        string    `form:"-" json:"name"`
	LastName    string    `form:"-" json:"last_name"`
	Image       string    `form:"-" json:"-"`
	ImageFormat string    `form:"-" json:"-"`
	ImageURL    string    `form:"-" json:"-"`
	Token       string    `form:"-" json:"-"`
	Expires     time.Time `form:"-" json:"-"`
	Refresh     string    `form:"-" json:"-"`
}

type Options

type Options struct {
	// The name of the site which will appear in user visible messages
	// e.g. "Sign In to SiteName".
	SiteName string
	// The user type, represented by a instance of the type.
	// e.g.
	//
	//  users{Options{UserType: MyUser{}}}
	UserType            interface{}
	FacebookApp         *facebook.App
	FacebookPermissions []string
	GoogleApp           *google.App
	GoogleScopes        []string
	TwitterApp          *twitter.App
	GithubApp           *github.App
	GithubScopes        []string
	// SocialAccountsOrder can be used to set the order in which
	// the enabled social accounts appear on the screen, from top
	// to bottom. If empty, it defaults to:
	//
	//  []string{SocialTypeFacebook, SocialTypeTwitter, SocialTypeGoogle, SocialTypeGithub}
	SocialAccountsOrder []SocialAccountType
	// DisableDirectSignIn can be used to disable non-social sign ins. If it's set to
	// true, users will only be able to sign in using the social sign in options.
	// Note that setting this variable to true also disables user registration, regardless
	// of the value of the DisableRegistration field.
	DisableDirectSignIn bool
	// DisableRegistration can be used to disable user registration. Only existing users
	// and social accounts will be able to log in.
	DisableRegistration bool
}

Options specifies the options for a *UsersApp. See each field for further explanations.

type PasswordForm

type PasswordForm struct {
	Password        password.Password `form:",min_length=6,label=Password" json:"-"`
	ConfirmPassword password.Password `form:",optional,label=Confirm Password"`
	User            reflect.Value     `form:"-"`
}

func (*PasswordForm) ValidateConfirmPassword

func (f *PasswordForm) ValidateConfirmPassword() error

type SignIn

type SignIn struct {
	Username string      `form:",singleline,label=Username or Email"`
	Password string      `form:",password,label=Password"`
	From     string      `form:",optional,hidden"`
	User     interface{} `form:"-"`
}

func (*SignIn) ValidatePassword

func (s *SignIn) ValidatePassword(ctx *app.Context) error

func (*SignIn) ValidateUsername

func (s *SignIn) ValidateUsername(ctx *app.Context) error

type SocialAccountType

type SocialAccountType string
const (
	SocialAccountTypeFacebook SocialAccountType = "Facebook"
	SocialAccountTypeTwitter  SocialAccountType = "Twitter"
	SocialAccountTypeGoogle   SocialAccountType = "Google"
	SocialAccountTypeGithub   SocialAccountType = "Github"
)

func (SocialAccountType) String

func (s SocialAccountType) String() string

type Twitter

type Twitter struct {
	Id          string `form:"-" sql:",unique" json:"id"`
	Username    string `form:"-" json:"username"`
	Image       string `form:"-" json:"-"`
	ImageFormat string `form:"-" json:"-"`
	ImageURL    string `form:"-" json:"-"`
	Token       string `form:"-" json:"-"`
	Secret      string `form:"-" json:"-"`
}

type User

type User struct {
	UserId             int64             `form:"-" orm:"id,primary_key,auto_increment" json:"id"`
	Username           string            `form:",max_length=16,min_length=4,alphanumeric,label=Username" json:"username"`
	NormalizedUsername string            `form:"-" orm:",unique" json:"-"`
	Email              string            `form:",max_length=50,label=Email" json:"-"`
	NormalizedEmail    string            `form:"-" orm:",unique" json:"-"`
	Password           password.Password `form:"-,min_length=6,label=Password" json:"-"`
	Created            time.Time         `json:"-" form:"-"`
	AutomaticUsername  bool              `form:"-" json:"-"`
	Admin              bool              `form:"-" orm:",default=false" json:"admin"`
	Image              string            `form:"-" orm:",omitempty,nullempty" json:"-"`
	ImageFormat        string            `form:"-" orm:",omitempty,nullempty" json:"-"`
}

func (*User) Id

func (u *User) Id() int64

func (*User) IsAdmin

func (u *User) IsAdmin() bool

func (*User) Save

func (u *User) Save()

func (*User) ValidateEmail

func (u *User) ValidateEmail(ctx *app.Context) error

func (*User) ValidateUsername

func (u *User) ValidateUsername(ctx *app.Context) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL