Skip to content
An Open source user management library
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig
.gitignore
.travis.yml
LICENSE
README.md
go.mod
go.sum
persona.go
persona_test.go

README.md

persona

GoDoc GoReport License GoLang TravisCi

User management library written in Go

What is Persona?

Persona is a simple, functional service to let you create, verify and update user profiles.

Persona is not for everyone; if your login system is too complex and relies on many factors, Persona is not for you. However, persona works great for most use cases.

What does it do?

  1. Helps you register new users.
  2. Validates credentials on login.
  3. Allows changing passwords.
  4. Allows recovering forgotten passwords.
  5. Create sessions

What does it NOT do?

  1. Does not verify or send email.

How to use it

go get -u gitote.in/dimensi0n/persona

Use this model template and add what you want

type User struct {
	persona.User
}

this is equivalent to :

type User struct {
	gorm.Model        // REQUIRED
	Username   string // REQUIRED
	Password   string // REQUIRED
	Mail       string `gorm:"not null;unique"` // REQUIRED
	Loggedin   bool   `gorm:"default:true"`    // REQUIRED
}

Config

You'll need to configure Persona. This is an example

db, err := gorm.Open("sqlite3", "gorm.db")
if err != nil {
	// ERROR
}
defer db.Close()

db.AutoMigrate(&User{})

// If uid field is "username"
// If your users connect them with their username
persona.Config(db, "username")

// If uid field is "email"
// If your users connect them with their email
persona.Config(db, "email")

Signup

user := User{"Username", persona.HashPassword("Pasword"), "mail@mail.com"}
err := persona.Signup(&user, user.Username, w) // &user is the struct to save && w is the response writer
if err := nil {
    // There is an error while attempting to signup the user 
}

Login

// Username/Password
user := User{Username: "Username", Password: "Password"}
err := persona.Login(user.Username, user.Password, w) // &user is the struct to save username is the UID field && w is the response writer
if err := nil {
    // User credentials are false
}

// Email/Password
user := User{Email: "mail@mail.com", Password: "Password"}
err := persona.Login(user.Mail, user.Password, w) // email is the UID field && w is the response writer
if err := nil {
    // User credentials are false 
}

Logout

// Username/Password
user := User{Username: "Username"}
err := persona.Logout(user.Username, w) // w is the response writer
if err := nil {
    // There is an error while attempting to logout the user 
}

user := User{Mail: "mail@mail.com"}
err := persona.Logout(user.Mail, w) // w is the response writer
if err := nil {
    // There is an error while attempting to logout the user 
}

Get current user

username, err := personna.CurrentUser(r) // r is the request pointer
if err != nil {
	// No user is logged in
}

Recover password

// Username/Password
user := User{Username: "Username", Password: "Password"}
err := persona.RecoverPassword(user.Username, user.Password, "new password")
if err != nil {
    // There is an error while attemting to change user password
}

// Email/Password
user := User{Mail: "mail@mail.com", Password: "Password"}
err := persona.RecoverPassword(user.Mail, user.Password, "new password")
if err != nil {
    // There is an error while attemting to change user password
}
You can’t perform that action at this time.