userreg

package module
v0.0.0-...-9a9a8db Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2020 License: MIT Imports: 11 Imported by: 3

README

GoDoc

About

The user-registration module is a module that uses Golang on user registration and authentication. This is created to simplify the codes to sign up and login users. By integrating the module, it takes out the pain of development by easing common tasks used in majority of web projects, such as:

  • User registration
  • User authentication
  • User forget password
  • User reset password
  • User activation

Example

  1. Database setup
dbConfig := userreg.DBConfig{
  Driver:   os.Getenv("db_type"),
  Username: os.Getenv("db_user"),
  Password: os.Getenv("db_pass"),
  Host:     os.Getenv("db_host"),
  DBName:   os.Getenv("db_name"),
}

tableName := "tests"
userreg.Initialize(dbConfig)
userreg.Config(userreg.TableName(tableName))
  1. User registration
in := &userreg.User{}
in.Name = input.Name
in.Email = input.Email
in.Password = input.Password

// Signup the account
user, err := userreg.Signup(in)
  1. User login
in := &userreg.User{}
in.Email = input.Email
in.Password = input.Password

// Login the account
user, err := userreg.Login(in)
  1. User forget password
in := &userreg.User{}
in.Email = input.Email
user, err := userreg.ForgetPassword(in)
  1. User reset password
in := &userreg.User{}
in.ResetPasswordCode = &input.ResetPasswordCode
in.Password = input.Password
user, err := userreg.ResetPassword(in)
  1. User activate account
in := &userreg.User{}
in.ActivationCode = &input.ActivationCode
user, err := userreg.ActivateAccount(in)

License

The user-registration module is open-sourced software licensed under the MIT license.

Documentation

Index

Constants

View Source
const (
	Mysql    = "mysql"
	Postgres = "postgres"
	Sqlite3  = "sqlite3"
	Mssql    = "mssql"
)

Variables

This section is empty.

Functions

func Config

func Config(opts ...func())

Optional configuration to the database / model

func Initialize

func Initialize(dbConfig DBConfig)

Setup the configuration to the DB connection

func JWTSetKey

func JWTSetKey(key string) func()

Configuration: Set the flag of user activation

func MigrateDatabase

func MigrateDatabase() func()

Configuration: Run migration scripts on the database

func TableName

func TableName(name string) func()

Configuration: Set the table name

func UserActivation

func UserActivation(on bool) func()

Configuration: Set the flag of user activation

Types

type BaseModel

type BaseModel struct {
	ID        uuid.UUID `sql:"type:char(36);primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time
}

Base model containing the common columns for all tables

func (*BaseModel) BeforeCreate

func (bm *BaseModel) BeforeCreate(scope *gorm.Scope) error

Before storing into the DB, mutate the ID to UUID

type DBConfig

type DBConfig struct {
	Driver                 string
	Username               string
	Password               string
	Host                   string
	Port                   string
	DBName                 string
	Path                   string
	InstanceConnectionName string
}

The configuration for the database connection

type Token

type Token struct {
	UserID uuid.UUID
	Name   string
	Email  string
	Expiry time.Time
	jwt.StandardClaims
}

func Authenticate

func Authenticate(jwtToken string) (*Token, error)

Authenticate the user by token

type User

type User struct {
	BaseModel
	Name                   string `gorm:"not null"`
	Email                  string `gorm:"unique;not null"`
	Password               string `gorm:"not null"`
	ActivationCode         *string
	ResetPasswordCode      *string
	ResetPasswordExpiredAt *time.Time
	Token                  string `gorm:"-"`
}

func ActivateAccount

func ActivateAccount(input *User) (*User, error)

Activate user account

func ForgetPassword

func ForgetPassword(input *User) (*User, error)

Set forgotten password code

func GetActivationCode

func GetActivationCode(input *User) (*User, error)

Get user activation code by email

func Login

func Login(input *User) (*User, error)

Login of the user

func ResetPassword

func ResetPassword(input *User) (*User, error)

Reset user password

func Signup

func Signup(input *User) (*User, error)

Sign up of the user

func (User) TableName

func (User) TableName() string

Setting the table name

Jump to

Keyboard shortcuts

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